java - Simple guessing game. "Your first guess is...." -


i creating simple guessing game 3 tries, need adding code can display "first guess" followed integer user puts in, "second guess", etc... have "enter guess" first try. need do? i'm confused on how go doing this. sorry if question confusing.

import java.util.scanner;  public class guess {  public static void main(string[] args) {      int randomn = (int) (math.random() * 10) + 1;      scanner input = new scanner(system.in);     int guess;     system.out.println("enter number between 1 , 10.");     system.out.println();     int attempts = 0;      {         attempts++;          system.out.print("enter guess: ");         guess = input.nextint();          if (guess == randomn) {             system.out.println("you won!");         } else if (guess < 1 || guess > 10) {             system.out.println("out of range");             attempts = +1;         } else if (guess > randomn) {             system.out.println("too high");         } else if (guess < randomn) {             system.out.println("too low");         }      } while (guess != randomn && attempts < 3);      if (guess != randomn && attempts == 3) {         system.out.println("number " + randomn);     }       }  } 

you create static method outside main, can output custom message depending on attempts.

public static string describe(int attempts) {     switch(attempts) {         case 1: return "first guess: ";         case 2: return "second guess: ";         case 3: return "third guess: ";         default: return "enter guess: "; //should not happen     } } 

then in main use so:

... {     attempts++;      system.out.print(describe(attempts)); ... } 

Comments

Popular posts from this blog

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

python - How do I create a list index that loops through integers in another list -

c# - “System.Security.Cryptography.CryptographicException: Keyset does not exist” when reading private key from remote machine -