Instructions: This is what I have by far... Note: The book forces me to use Simp
ID: 3551913 • Letter: I
Question
Instructions:
This is what I have by far...
Note: The book forces me to use SimpleIO in order to get user's input and Convert.toDouble in order to convert the input into a double data type.
Any help is appreciated.
public static void main(String[] args) {
int [] num = new int[100]; //an array of 100 integers
int i,j,k; //used as an array index
//Generates 100 random numbers for the array
for( i= 0; i < 100; i++) {
num[i] = (int) (Math.random()*(1000));
}
while(true){
//Collect numeric input from user
SimpleIO.prompt("Enter a number:");
String Input = SimpleIO.readLine();
int userNumber = (int) Convert.toDouble(Input);
if (userNumber == num[i]){
System.out.print("Congratulations! You have guessed a number within the secret list");
}
if (num[i] != userNumber)
System.out.print("Fuck This!!!");
}
for (int x: num){
System.out.print(x+",");
}
Explanation / Answer
import java.util.Scanner;
public class RandomNos {
public static void main(String[] args) {
int[] num = new int[100]; // an array of 100 integers
int i, j, k; // used as an array index
// Generates 100 random numbers for the array
for (i = 0; i < 100; i++) {
num[i] = (int) (Math.random() * (1000));
}
Scanner userInput = new Scanner(System.in);
String wantToTryAgain = "Y";
while (wantToTryAgain.equalsIgnoreCase("Y")) {
// Collect numeric input from user
System.out.println("Enter a number:");
boolean flag = false;
String Input = userInput.next();
Double userNumber = Double.parseDouble(Input);
for (j = 0; j < num.length; j++) {
if (userNumber == num[j]) {
flag = true;
}
}
if (flag) {
System.out.println("Congratulations! You have guessed a number within the secret list");
} else {
System.out.println("The no does not match");
}
System.out.println("All the random nos are: ");
for (int x : num) {
System.out.print(x + ",");
}
System.out.println();
System.out.println(" Do you want to try again? Enter 'Y' for yes and 'N' for NO: ");
wantToTryAgain = userInput.next();
}
}
}