[英]How do I call my methods to my main method?
基本上,我需要將“ sum”和“ sumOfEvens”方法稱為main方法。 “ sum”方法適用於數組包含“ isLucky”方法中顯示的幸運數字的情況。 如果數組不包含幸運數字,則“ sumOfEvens”方法會添加偶數。
因此,“ sum” = true,而“ 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
}
boolean b = isLucky(a);
int result;
if(b)
result = sum(a);
else
result = sumOfEvens(a);
您可以一行完成:
int result = isLucky(a) ? sum(a) : sumOfEvens(a);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.