The game is supposed to ask the user to think of a number. Then add the minimum value and maximum value. The program is then supposed to tell the user that it will guess the number in a specific amount of guesses. This is determined by taking the square root of Maximum and subtracting the Minimum Plus 1. The program is then supposed to guess the number and ask the user if it is too high, too low, or correct. Based on the answer the Minimum or Maximum value should change and the computer should guess again until either the computer guessed the right number or the computer has exceeded its amount of guesses. If the program guesses right it should display, "I Win!" If the computer exceeds it's guesses it should display, "Cheaters Never Win!" I am extremely new to coding and my brain just cannot figure out how to fix all the issues.
The Results Display: Think of an integer value... What is the Lowest value: 5 What is the highest possible value: 30 I will guess your value in 5 guesses. My guess #1 is 9. My guess #2 is 9. My guess #3 is 9. My guess #4 is 9. My guess #5 is 9. Is that too (H)igh, too (L)ow, or (C)orrect? Cheaters Never Win!
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;
public class guess
{
public static void main(String[] args){
Scanner input=new Scanner(System.in); //Created new scanner object
System.out.println("Think of an integer value...");
System.out.println("What is the Lowest value:");
int min=input.nextInt();
System.out.println("What is the highest possible value:");
int max=input.nextInt();
double y=Math.sqrt(max-min+1);
double DoubleValue=y;
int IntValue=(int) DoubleValue;
System.out.println("I will guess your value in " + IntValue + " guesses.");
int randomNum=ThreadLocalRandom.current().nextInt(min, max + 1);
int i=1;
for(i=1; i<y; i++)
System.out.println("My guess #" + i + " is " + randomNum + ".");
System.out.println("Is that too (H)igh, too (L)ow, or (C)orrect?");
String answer=input.nextLine(); {
if(answer.equalsIgnoreCase("H")){
max=(max-randomNum)-max;
System.out.println("My guess #" + i + " is " + randomNum + ".");
System.out.println("Is that too (H)igh, too (L)ow, or (C)orrect?");
answer=input.nextLine();
}
else if(answer.equalsIgnoreCase("L")){
min=(randomNum-min)+min;
System.out.println("My guess #" + i + " is " + randomNum + ".");
System.out.println("Is that too (H)igh, too (L)ow, or (C)orrect?");
answer=input.nextLine();
}
else if(i>y){
System.out.println("Cheaters Never Win!");
}
else{
System.out.println("I Win!");
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire