Untitled
unknown
plain_text
3 years ago
2.1 kB
4
Indexable
import java.util.Scanner;
public class MyClass {
final static int maxNumber=50;
static Scanner scanner = new Scanner(System.in);
static int numberGuess = 0;
static String option = "Y";
static int numberPlayGames = 0;
public static void initGame(){
int numberToGuess = (int) (Math.random()*maxNumber+1);
System.out.println("This program allows you to play a guessing game.\n"+
"It will think of a number between 1 and 100\n "+
"and will allow you to guess until you get the number.\n" +
"For each guess, it will tell you whether you have\n"+
"guessed too high or too low\n");
playGame(numberToGuess);
numberPlayGames +=1;
System.out.println("dadada?");
System.out.println(numberPlayGames);
}
public static void playGame(int numberToGuess){
System.out.println("I'm thinking of a number between 1 and 100...");
do {
System.out.println(numberToGuess);
System.out.println("Your guess?");
int mynumber = scanner.nextInt();
numberGuess += 1;
if ( numberToGuess == mynumber){
if (numberGuess == 1)
System.out.println("You got it right on the first guess!");
else
System.out.printf("You got it right in %d guesses\n",numberGuess);
System.out.print("Do you want to play again?");
option = scanner.next();
if (option.charAt(0).lowercase() == "y"){
initGame();
}
}else {
if (mynumber>numberToGuess){
System.out.println("Too high");
}else{
System.out.println("Too low");
}
}
}while((option.charAt(0) == 'y') || (option.charAt(0) == 'Y'));
}
public static void main(String args[]) {
initGame();
}
}Editor is loading...