基本上我需要打电话给我的"总和"和" sumOfEvens"方法进入主要方法。 "总和"方法是当数组包含幸运数字时,如#34; isLucky"方法。 " sumOfEvens"如果数组不包含幸运数,则方法会添加偶数。
所以"总和" =真 和" sumOfEvens" = false
所以这是我的代码......
import java.util.Scanner;
public class FunArrays {
public static void main(String[] args) {
luckyNumber1 = 7;
luckyNumber2 = 13;
luckyNumber3 = 18;
int[] a=new int[10];
Scanner sc=new Scanner(System.in);
System.out.println("Please enter numbers...");
for(int j = 0; j < a.length; j++)
a[j] = sc.nextInt();
}
public static int sum(int [ ] value)
{
int i, total = 0;
for(i=0; i<10; i++)
{
total = total + value[ i ];
}
return (total);
}
static int sumOfEvens(int array[])
{
int sum = 0;
for(int i = 0; i < array.length; i++>) {
if(array[i] % 2 == 0)
sum += array[i];
}
return sum;
}
public static boolean isLucky (int[] array)
{
if ( array[i] == 7 || array[i] == 13 || array[i] == 18 )
return true;
else
return false
}
// write the static methods isLucky, sum, and sumOfEvens
}
答案 0 :(得分:0)
boolean b = isLucky(a);
int result;
if(b)
result = sum(a);
else
result = sumOfEvens(a);
答案 1 :(得分:0)
您可以在一行中完成:
int result = isLucky(a) ? sum(a) : sumOfEvens(a);