Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The thing with Scanner is that Scanner is very, very strange. I've been using it for years and she hates that I use her for only fast and dirty input. I don't even want to begin to describe the bizarre things that happen, but the explanation is usually around how non-nextLine() methods deal with newline characters (whether they consume/ignore them or not)</p> <p>My advice with scanner is to only ever use its hasNextLine() and nextLine() methods. They are the only methods I found were every human using them can predict the method's behaviour. You can then check if it is a number (matches("[1-9]+[0-9]*")) or just be wild and do Integer.parseInt() directly.</p> <p>Seeing game = new GuessCalc(guess); is a loop looks strange in class 1. That looks like an error since rounds will be reset constantly.</p> <p>Edit 1:</p> <p>If your code is not suppose to reset the random number every single round and reset the 'tries' count every round (the code threw away the current game every round) the below code may help you:</p> <pre><code>import java.util.Scanner; public class GuessRunner { public static void main(String[] args) throws InterruptedException { System.out.print("I will choose a number between 1 and 10" + '\n' + "You have 3 tries to get it right" + '\n'); GuessCalc game = new GuessCalc(); while (game.getRounds() &lt;= 10) { game.playRound(); System.out.println(game.wins()); } } } </code></pre> <p>Second class:</p> <pre><code>import java.util.Scanner; public class GuessCalc { private int wins, rounds = 0, num = (int) (1 + Math.random() * 10); private int tries = 0; private Scanner sc=new Scanner(System.in); /** * Constructs the game * * @param guess * the player's guess */ public GuessCalc() { } /** * Runs the rounds and determines if they win * * @return outcome if they won (true) or lost (false); */ public boolean playRound() { startNewRound(); System.out.printf("Round %d \n\n", this.getRounds()); while(true){ System.out.println("Enter a new guess: "); int guess = Integer.parseInt(sc.nextLine()); printHotOrCold(guess); if (guess == num) { wins++; System.out.println("Jackpot! Setting a new random number"); return true; }else if(tries==3){ System.out.println("Oops, didn't succeed. Better luck next time. The number was "+num); return false;//ran out of tries }else{ //got it wrong but still has guesses left } } } public final void startNewRound() { rounds++; tries = 0; num = (int) (1 + Math.random() * 10);// set a new random number } /** * Tells the player if they are hot or cold */ public void printHotOrCold(int guess) { int offBy = guess - num; if (offBy == 1 || offBy == -1) {// if they are over or under by 1 System.out.println("Hot"); } else if (guess != num) {// if they are further than 1 away System.out.println("Cold"); } tries++; } /** * Returns the number of wins and makes a new header * * @return the String with the number of wins and new header */ public String wins() { String record = String.format("You have won %d out of %d rounds. \n\n",wins,rounds); return record; } /** * Returns the number of rounds played * * @return rounds the number of rounds */ public int getRounds() { return rounds; } } </code></pre> <p>Your code was malformed (couldn't compile) and I don't 100% know your intent (ex unknown if it is 10 rounds to get as many random numbers correct as possible or they have 3 guesses in 10 rounds). Good luck.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload