Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Help with this Simple Java Program?

Here is what I am trying to get

Here is a sample interaction between a user and the program you will write in the Lab4Part2 class (user inputs are in bold):

Do you want to play a game? y

I am thinking of a number between 1 and 100. Try to guess it.

What's your guess? 50

50 is too small

What's your guess? 75

75 is too big

What's your guess? 62

62 is too small

What's your guess? 69

69 is too big

What's your guess? 65

65 is too big

What's your guess? 63

63 is too small

What's your guess? 64

You've got it in 7 guesses. That was ok!

Do you want to play another game? y

I am thinking of a number between 1 and 100. Try to guess it.

What's your guess? 50

50 is too big

What's your guess? 25

25 is too small

What's your guess? 37

37 is too small

What's your guess? 43

You've got it in 4 guesses. That was amazing!

Do you want to play another game? n

Goodbye!

The difference between the programs in Part 1 and Part 2 is that now the program repeatedly asks the user if he/she wants to play a game; if the user enters anything but 'y', the program terminates; otherwise the program allows the user to play a game before asking again if the user wants to play another game.

The Problem i am running into is once it gets to the end of the program it doesn't let me enter yes to restart the entire thing

Here is the code i have so far

import java.util.Scanner;

public class Lab4Part2

{

public static void main(String[] args)

{

Scanner keyboard = new Scanner(System.in);

String doYouWantToPlay;

System.out.print("Do you want to play a game? ");

doYouWantToPlay = keyboard.nextLine();

while (doYouWantToPlay.equals ("y") || doYouWantToPlay.equals ("yes")) {

System.out.println();

System.out.println("I am thinking of a number between 1 and 100. Try to guess it.");

System.out.println();

int x = (int)(100 * Math.random ()) + 1;

int guessCount = 0,guess;

System.out.print("What's your guess? ");

guess = keyboard.nextInt();

while (guess != x) {

if (guess > x) {

System.out.println(guess + " is too big!");

guessCount = guessCount + 1;

System.out.print("What's your guess? ");

guess = keyboard.nextInt();

}

else if (guess < x) {

System.out.println(guess + " is too small!");

guessCount = guessCount + 1;

System.out.print("What's your guess? ");

guess = keyboard.nextInt();

}

}

if (guess == x) {

guessCount = guessCount + 1;

if (guessCount == 1) {

System.out.print("You've got it in " + guessCount + " guesses! That was lucky!");

System.out.println("Do you want to play another game? ");

doYouWantToPlay = keyboard.nextLine();

}

else if (guessCount > 1 && guessCount < 5) {

System.out.print("You've got it in " + guessCount + " guesses! That was amazing!");

System.out.println("Do you want to play another game? ");

doYouWantToPlay = keyboard.nextLine();

}

else if (guessCount > 6 && guessCount < 8) {

System.out.print("You've got it in " + guessCount + " guesses! That was ok!");

System.out.println("Do you want to play another game? ");

doYouWantToPlay = keyboard.nextLine();

}

else if (guessCount > 7 && guessCount < 10) {

System.out.print("You've got it in " + guessCount + " guesses! That was pretty bad!");

System.out.println("Do you want to play another game? ");

doYouWantToPlay = keyboard.nextLine();

}

else if (guessCount > 10) {

System.out.print("You've got it in " + guessCount + " guesses! This is not your game!");

System.out.println("Do you want to play another game? ");

doYouWantToPlay = keyboard.nextLine();

}

}

System.out.println("Goodbye!");

}

}

}

Update:

What are you saying i need to change the entire loop structure

3 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    Do you want to play a game should be an if test.

    Do you want to play another game should be a do while loop (the test is at the end of the loop).

    Have fun.

  • Sayee
    Lv 4
    1 decade ago

    hmm.. code looks okay. Try using separate scanner for getting y or yes string.

    Scanner getString = new Scanner(System.in);

    use doYouWantToPlay = getString.nextLine(); ( instead of doYouWantToPlay = keyboard.nextLine(); )

    May be scanner doesn't like mix and match!! i.e. getting int and string using same scanner.

    Give it a try. Good luck!

  • kull
    Lv 4
    4 years ago

    you have many typos. i might advise installation the NetBeans IDE. NetBeans will flag obvious blunders and furnish code final touch for you. style what you're analyzing on your e book as a code workout. you would be taught plenty quicker. for( int i = 0; i< a.length; i++ ) { // is the workhorse of programming

Still have questions? Get your answers by asking now.