Skip to main content
Code Review

Return to Question

Question Protected by Community Bot
deleted 3 characters in body
Source Link
JCob
  • 85
  • 1
  • 1
  • 5

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.

Rollback to Revision 4
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

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();
}
added 566 characters in body
Source Link
JCob
  • 85
  • 1
  • 1
  • 5

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();
}
edited tags
Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
added 7 characters in body; edited tags
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
edited tags
Link
JCob
  • 85
  • 1
  • 1
  • 5
Loading
Source Link
JCob
  • 85
  • 1
  • 1
  • 5
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /