如何将缓冲作家与我的随机猜谜游戏结合起来?

时间:2018-11-19 21:15:35

标签: java bufferedwriter

我有一个任务需要将缓冲的编写器合并到我的Java代码中,以在程序崩溃时恢复猜测。例如,程序将询问:“您是否要恢复以前的猜测?”。

我在做什么方面迷失了方向。任何建议或帮助将不胜感激。

import java.util.*;

public class Guess{

public static void main(String[] args){

    Scanner scan = new Scanner(System.in);
    String playAgain = "";


    do { 
    int randomTarget = randomNumberGenerator();
    int userInput = 0;
    System.out.println("Welcome to the random number generator game!");
    System.out.println("Enter a number between 1 through 10:");
    int count = 0;


    while (userInput != randomTarget){    
        try{
        userInput = scan.nextInt();
        }catch(Exception e){
            System.out.println("Invalid type.");
            count++;
            String s = scan.next();  
            System.out.println(s + " is an invalid input. Try again");
            continue;
        } 
        if (userInput > randomTarget){
            count++;
            System.out.println ("Number too high!");          
        }else if (userInput < randomTarget){
            count++;
            System.out.println("Number too low");   
        }else{  
        System.out.println("Congratulations, you guessed correctly!");
        count++;
        System.out.println("It took you: " + count + " tries!");
        playAgainQ();
        try {
        playAgain = scan.next();
        } catch (Exception e) {
            System.out.println(playAgain + " is an invalid input. Try again");
            continue;
        }

        }


    }
    } while (playAgain.equalsIgnoreCase("y"));
    System.out.println("Thanks for playing. Goodbye!");

}
static void playAgainQ() {
      System.out.println("Do you want to play again? Type y for yes.");
}
static int randomNumberGenerator() {
    Random randomNumber = new Random();
    int randomTarget = (randomNumber.nextInt(10) + 1);
    return randomTarget;
 }

 }

0 个答案:

没有答案