本文目录一览:
- 1、JAVA数组元素求和
- 2、java,如何实现定义一个整形数组并实现求和和求平均的程序
- 3、求教java中求数组中数的总和是调用哪个方法?
- 4、java中如何求一个数组中元素的和.
- 5、Java中编程求一个整数数组的最大值、最小值、平均值和所有数组元素的和
- 6、java里一个int数组求和问题
JAVA数组元素求和
static int[] sum(int[] arr)
{
int[] s=new int[2];
for(int i=0;iarr.length;i+=2)
s[0]+=arr[i];
for(int i=1;iarr.length;i+=2)
s[1]+=arr[i];
return s;
}
java,如何实现定义一个整形数组并实现求和和求平均的程序
不知道你的平均数要不要取整 下面是比较懒得做法:
public static void main(String[] args) throws Exception {
int a[] = {1,2,3,4,5,6,7,112};
int tmp = 0;
for(int b : a){
tmp += b;
}
System.out.println("和:"+tmp);
System.out.println("平均:"+tmp/a.length);
}
再给你一个看上去好点的:
public class MyMath{
int[] array;
int sum;
float average;
public MyMath(){
//懒得写了
}
public MyMath(int...a){
array = new int[a.length];
int index = 0;
sum = 0;
for(int b:a){
array[index++]=b;
sum += b;
}
average = (float)sum/a.length;
}
public int[] getArray() {
return array;
}
public int getSum() {
return sum;
}
public float getAverage() {
return average;
}
public static void main(String[] args){
MyMath m =new MyMath(1,2,3,4,5,6,7,8);
int[] result = m.getArray();
System.out.println(m.getSum());
System.out.println(m.getAverage());
}
}
求教java中求数组中数的总和是调用哪个方法?
遍历循环,累加求和即可:
int sum=0;
int [] a={100,200,300};
for(int i=0;ia.length ;i++){
sum=sum+a[i];
}
java中如何求一个数组中元素的和.
import java.util.Scanner;
public class XiTi464 {
public static void main(String[] args) {
Scanner sr = new Scanner(System.in);
System.out.print("输入数组元素个数:");
int a = sr.nextInt();
int score[] = new int[a];
for (int i = 0; i a; i++) {
System.out.print("输入第" + (i + 1) + "个值:");
score[i] = sr.nextInt();
}
int sum = arrSum(score);
int max = arrMax(score);
int min = arrMin(score);
System.out.println("数组元素之和:" + sum);
System.out.println("数组元素中最大值:" + max);
System.out.println("数组元素中最小值:" + min);
sr.close();
}
public static int arrSum(int arr[]) {
int temp = 0;
for (int i = 0; i arr.length; i++) {
temp += arr[i];
}
return temp;
}
public static int arrMax(int arr[]) {
int temp = arr[0];
for (int i = 1; i arr.length; i++) {
if (temp arr[i])
temp = arr[i];
}
return temp;
}
public static int arrMin(int arr[]) {
int temp = arr[0];
for (int i = 1; i arr.length; i++) {
if (temp arr[i])
temp = arr[i];
}
return temp;
}
}
Java中编程求一个整数数组的最大值、最小值、平均值和所有数组元素的和
package
com.work20081209.main;
/**
*
Java中编程求一个整数数组的最大值、最小值、平均值和所有数组元素的和
*
*/
public
class
TestArray
{
private
int
maxValue;
private
int
minValue;
private
double
aveValue;
private
int
sumValue;
public
TestArray(int[]
nums){
maxValue
=
nums[0];
minValue
=
nums[0];
aveValue
=
nums[0];
sumValue
=
nums[0];
for
(int
i
=
1;
i
nums.length;
i++)
{
if(nums[i]
maxValue){
maxValue
=
nums[i];
}
if(nums[i]
minValue){
minValue
=
nums[i];
}
sumValue
=
sumValue
+
nums[i];
}
aveValue
=
sumValue/nums.length;
}
public
void
print(){
System.out.println("最大值:"+maxValue);
System.out.println("最小值:"+minValue);
System.out.println("平均值:"+aveValue);
System.out.println("和:"+sumValue);
}
public
static
void
main(String[]
args)
{
int[]
index
=
new
int[]{4,1,5,3,2};
TestArray
ta
=
new
TestArray(index);
ta.print();
}
}
java里一个int数组求和问题
自己求一下不就行了:
int myArray[] = new int[100];
......
int sum = 0;
for (int i=0; i100; i++)
sum += myArray[i];
return sum; // 返回总和