How do I print the result in the main method? And how do I loop the "isLucky" method to the array? 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);
in your main method
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.