如何随时按 e 退出游戏?

如果有人按e,我希望我的游戏在游戏中随时停止。


import java.util.Scanner;


public class Game {


    public static void main(String[] args) {


        Scanner input = new Scanner(System.in);


        int points = 0;

        int multiply;


        System.out.println("press e to exit the game at any time! ");


        System.out.println("please enter a number");


        int yourNumber = input.nextInt();


        for (multiply = 0; multiply<= 10; multiply++){

            int yourAnswer = yourNumber * multiply;


            System.out.println(yourNumber + " x " + multiply + " = ? ");


            int theAnswer = input.nextInt();


            for (int tries = 1; tries<= 4; tries++){


                if (theAnswer == yourAnswer){


                    points = points + 5;

                    System.out.println("you have " + points + " points");

                    break;

                }

                else{

                    System.out.println("Your answer : " + theAnswer  + " is wrong, please try again. Attempts : " + tries + " out of 4");

                    theAnswer = input.nextInt();


                    points--;


                    if (tries == 4){

                        System.out.println("sorry maximum attempts!!! moving to the next question");

                        tries++;

                        break;

                    }

                }

            }

        }

    }


不负相思意
浏览 99回答 1
1回答

侃侃无极

而不仅仅是“int theAnswer = input.nextInt();” 写这个:String nextIn = input.next();int theAnswer = 0;if (nextIn.equal("e")) {&nbsp; &nbsp; System.out.println("Exiting the game...")&nbsp; &nbsp; break;}else {&nbsp; &nbsp; theAnswer = Integer.parseInt(nextIn);}显然我没有考虑到例外情况,但如果你愿意的话也可以。总的来说,它看起来像这样:public class Game{&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; Scanner input = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; int points = 0;&nbsp; &nbsp; &nbsp; &nbsp; int multiply;&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("press e to exit the game at any time!");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("please enter a number");&nbsp; &nbsp; &nbsp; &nbsp; int yourNumber = input.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; for (multiply = 0; multiply<= 10; multiply++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int yourAnswer = yourNumber * multiply;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(yourNumber + " x " + multiply + " = ? ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //new part:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String nextIn = input.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int theAnswer = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (nextIn.equals("e")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Exiting the game...")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; theAnswer = Integer.parseInt(nextIn);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int tries = 1; tries<= 4; tries++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (theAnswer == yourAnswer){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; points = points + 5;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("you have " + points + " points");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Your answer : " + theAnswer&nbsp; + " is wrong, please try again. Attempts : " + tries + " out of 4");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; theAnswer = input.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; points--;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (tries == 4){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("sorry maximum attempts!!! moving to the next question");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tries++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java