Please help in JAVA The input consists of an integer the num

Please help in JAVA:

The input consists of an integer (the number of elements in the array), followed by the elements of the array. Method Add will add the elements of the array and return that value (the total) to main. Method printResult will take that total and print the following: if the total is divisible by 5, it will print \"You lose!\" If the total is exactly 21, it will print \"You win!\" Otherwise it will print \"Try again.\" In each case use a println.

This is the code given:

import java.util.Scanner;

public class Lab1Num1 {

public static void main(String[] args) {

//get the input

int[] myArray=getInput();

//add the integers

int total=Add(myArray);

//determine win or lose or try again.

printResult(total);

}

  

public static int[] getInput()

{ Scanner keyboard = new Scanner(System.in);

//find the length of the array

int num = keyboard.nextInt();

//create the array

int[] myA= new int[num];

//fill the array

for (int i=0; i<num; i++)

myA[i]=keyboard.nextInt();

return myA;

  

}

public static int Add(int[]myA)

{ //your code goes here

}

public static void printResult(int t)

{ //your code goes here   

}

}

Thank you!!

Solution

Lab1Num1.java

import java.util.Scanner;
public class Lab1Num1 {

   public static void main(String[] args) {
       //get the input
int[] myArray=getInput();
//add the integers
int total=Add(myArray);
//determine win or lose or try again.
printResult(total);  

   }


public static int[] getInput()
{
   //Scanner class object is used to read the numbers entered by the user
   Scanner keyboard = new Scanner(System.in);
  
//find the length of the array
   System.out.print(\"Enter the length of the array :\");
int num = keyboard.nextInt();

//create the array
int[] myA= new int[num];
  
//fill the array
for (int i=0; i<num; i++)
{
   System.out.print(\"Enter a number \"+(i+1)+\":\");
  
   if(keyboard.hasNextInt() )
myA[i]=keyboard.nextInt();
   else
   myA[i]=0;
}
  
keyboard.close();
return myA;

}

//This method will calculate the sum of the array
public static int Add(int[] myA)
{
   //Declaring the variable
   int sum=0;
     
   //This for loop will calculate the sum of the elements in the array
   for(int i=0;i<myA.length;i++)
   sum+=myA[i];
     
   return sum;
}

//This method will display the total(Sum of elements in the array)
public static void printResult(int t)
{
   if(t%5==0)
       System.out.println(\"You lose!\");
   else if(t==21)
       System.out.println(\"You win!\");
   else
       System.out.println(\"Try again.\");
}
}

___________________

Output1:

Enter the length of the array :5
Enter a number 1:4
Enter a number 2:6
Enter a number 3:9
Enter a number 4:8
Enter a number 5:4
Try again.

Output2:

Enter the length of the array :5
Enter a number 1:10
Enter a number 2:15
Enter a number 3:20
Enter a number 4:5
Enter a number 5:25
You lose!

_____Thank You

Please help in JAVA: The input consists of an integer (the number of elements in the array), followed by the elements of the array. Method Add will add the elem
Please help in JAVA: The input consists of an integer (the number of elements in the array), followed by the elements of the array. Method Add will add the elem
Please help in JAVA: The input consists of an integer (the number of elements in the array), followed by the elements of the array. Method Add will add the elem

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site