[英]Java program. How do I loop my "isLucky method through the array? and how do I print the result in the main method?
How do I print the result in the main method? 如何在main方法中打印结果? And how do I loop the "isLucky" method to the array?
以及如何将“ isLucky”方法循环到数组? I posted a little bit ago and was told I should post again with less questions.
我前几天发布了,并被告知我应该以较少的问题再次发布。
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();
boolean b = isLucky(a);
int result;
if(b){
result = sum(a);
}
else{
result = sumOfEvens(a);
}
}
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
} }
Try this, 尝试这个,
public static boolean isLucky(int[] array)
{
for (int i = 0; i < array.length; i++)
{
if (array[i] == 7 || array[i] == 13 || array[i] == 18)
{
System.out.println("Going to return true");
return true;
}
}
return false;
}
printing the result by using System.out.println("Result : "+result);
通过使用
System.out.println("Result : "+result);
打印System.out.println("Result : "+result);
in your main method 在你的主要方法中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.