Write a Java program that will play a number guessing game. The program should a
ID: 3916536 • Letter: W
Question
Write a Java program that will play a number guessing game. The program should allow a user to enter 3 different numbers between 1 10 (inclusive). You must store the 3 numbers in a 1-Dimensional array. Your program must use one class and include several class methods to implement several tasks. Each task should be implemented by a separate method. Display a simple menu screen showing the following options (each must be implemented in a separate method) 1. Enter 3 different numbers between 1-10 (inclusive). 2. Display the contents of the 1-D array containing the 3 numbers that you entered. 3. Sort the contents of the array in increasing order (i.e. 1st element smallest number, 2nd element- 2nd smallest number, etc.). Compare your 3 numbers in the 1-D array with the following numbers to see if they are the same and display the result 4. 2, 4, 6 e.g., your program might display: You entered 1, 2, 3 You match one number. It is 2 5. Display the most common number entered during the program execution 6. End program Note: After a method has completed, your program should return to the main menu and allow the user to select another option. Note: Extra marks will be awarded for good comments, indentation and layout of code. *Program must keep running until user choose to terminate it and there must be at least 5 different methodsExplanation / Answer
Solution:
Please find below the complete code :
NumberGame.java
package aug1;
import java.util.Arrays;
import java.util.Scanner;
public class NumberGame {
private static int[] arrayOfNos = new int[3];
public static void main(String[] args) {
while (true) {
System.out.println("1. Enter 3 different nos between 1-10 (inclusive): ");
System.out.println("2. Display the nos");
System.out.println("3. Sort the nos");
System.out.println("4. Compare if they match with 2,4,6 ");
System.out.println("5. Display most common no");
System.out.println("6. Exit");
Scanner scanner = new Scanner(System.in);
int input = scanner.nextInt();
switch (input) {
case 1:
takeInput();
break;
case 2:
display();
break;
case 3:
sort();
break;
case 4:
compare();
break;
case 5:
displayCommonNos();
break;
case 6:
exitProgram();
break;
default:
break;
}
}
}
private static void exitProgram() {
System.exit(0);
}
private static void displayCommonNos() {
//TODO Please denote the criteria to find common nos.
}
private static void compare() {
boolean flag = false;
int countOfCommons = 0, j = 0;
int[] commonsArray = new int[3];
int[] referenceArray = {2, 4, 6};
for(int i = 0; i< 3; i++) {
if(arrayOfNos[i] == referenceArray[i]) {
flag = true;
countOfCommons++;
commonsArray[i] = arrayOfNos[i];
}
}
System.out.println("You have entered " + Arrays.toString(arrayOfNos));
if(flag) {
System.out.println("You match " + countOfCommons + " number : " + Arrays.toString(commonsArray));
}else {
System.out.println("No match");
}
}
private static void sort() {
Arrays.sort(arrayOfNos);
}
private static void display() {
System.out.println(Arrays.toString(arrayOfNos));
}
private static void takeInput() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter 1st no: ");
arrayOfNos[0] = scanner.nextInt();
System.out.print("Enter 2nd no: ");
arrayOfNos[1] = scanner.nextInt();
System.out.print("Enter 3rd no: ");
arrayOfNos[2] = scanner.nextInt();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.