In this exercise I have to do this:
Modify the program so that no matter what number the user thinks of (1-100) the computer can guess it in 7 or less guesses.
I'm just a beginner in Java and I'd like to know what you think about my code:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Random rand = new Random();
int randNum = 0;
int upperLimit = 100;
int lowerLimit = 1;
String myAnswer = "";
do {
randNum = rand.nextInt(upperLimit - lowerLimit + 1) + lowerLimit;
System.out.println("I think it's " + randNum);
myAnswer = in.nextLine();
if(myAnswer.equals("tl")) { //too low
lowerLimit = randNum + 1;
}
else if(myAnswer.equals("th")) { // too high
upperLimit = randNum - 1;
}
}while(!myAnswer.equals("y"));
in.close();
System.out.println("YAAAY! :D");
}
I think it's too slow, because sometimes it takes the computer more than 7 times to guess a number.
In this exercise I have to do this:
Modify the program so that no matter what number the user thinks of (1-100) the computer can guess it in 7 or less guesses.
I'm just a beginner in Java and I'd like to know what you think about my code:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Random rand = new Random();
int randNum = 0;
int upperLimit = 100;
int lowerLimit = 1;
String myAnswer = "";
do {
randNum = rand.nextInt(upperLimit - lowerLimit + 1) + lowerLimit;
System.out.println("I think it's " + randNum);
myAnswer = in.nextLine();
if(myAnswer.equals("tl")) { //too low
lowerLimit = randNum + 1;
}
else if(myAnswer.equals("th")) { // too high
upperLimit = randNum - 1;
}
}while(!myAnswer.equals("y"));
in.close();
System.out.println("YAAAY! :D");
}
I think it's too slow, because sometimes it takes the computer more than 7 times to guess a number.
In this exercise I have to do this:
Modify the program so that no matter what number the user thinks of (1-100) the computer can guess it in 7 or less guesses.
I'm just a beginner in Java and I'd like to know what you think about my code:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Random rand = new Random();
int randNum = 0;
int upperLimit = 100;
int lowerLimit = 1;
String myAnswer = "";
do {
randNum = rand.nextInt(upperLimit - lowerLimit + 1) + lowerLimit;
System.out.println("I think it's " + randNum);
myAnswer = in.nextLine();
if(myAnswer.equals("tl")) { //too low
lowerLimit = randNum + 1;
}
else if(myAnswer.equals("th")) { // too high
upperLimit = randNum - 1;
}
}while(!myAnswer.equals("y"));
in.close();
System.out.println("YAAAY! :D");
}
I think it's too slow, because sometimes it takes the computer more than 7 times to guess a number.
Thanks everyone! Now it works perfectly.
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int upperLimit = 100;
int lowerLimit = 1;
String myAnswer;
do {
int randNum = (upperLimit + lowerLimit + 1) / 2;
System.out.println("I think it's " + randNum);
myAnswer = in.nextLine();
if(myAnswer.equals("tl")) {
lowerLimit = randNum + 1;
}
else if(myAnswer.equals("th")) {
upperLimit = randNum - 1;
}
}while(!myAnswer.equals("y"));
in.close();
}
Thanks everyone! Now it works perfectly.
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int upperLimit = 100;
int lowerLimit = 1;
String myAnswer;
do {
int randNum = (upperLimit + lowerLimit + 1) / 2;
System.out.println("I think it's " + randNum);
myAnswer = in.nextLine();
if(myAnswer.equals("tl")) {
lowerLimit = randNum + 1;
}
else if(myAnswer.equals("th")) {
upperLimit = randNum - 1;
}
}while(!myAnswer.equals("y"));
in.close();
}
Thanks everyone! Now it works perfectly.
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int upperLimit = 100;
int lowerLimit = 1;
String myAnswer;
do {
int randNum = (upperLimit + lowerLimit + 1) / 2;
System.out.println("I think it's " + randNum);
myAnswer = in.nextLine();
if(myAnswer.equals("tl")) {
lowerLimit = randNum + 1;
}
else if(myAnswer.equals("th")) {
upperLimit = randNum - 1;
}
}while(!myAnswer.equals("y"));
in.close();
}
Thanks everyone! Now it works perfectly.
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int upperLimit = 100;
int lowerLimit = 1;
String myAnswer;
do {
int randNum = (upperLimit + lowerLimit + 1) / 2;
System.out.println("I think it's " + randNum);
myAnswer = in.nextLine();
if(myAnswer.equals("tl")) {
lowerLimit = randNum + 1;
}
else if(myAnswer.equals("th")) {
upperLimit = randNum - 1;
}
}while(!myAnswer.equals("y"));
in.close();
}