java - 如何随时按 e 退出游戏?

标签 java

如果有人按 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;
                    }
                }
            }
        }
    }
} 

最佳答案

而不仅仅是“int theAnswer = input.nextInt();”写这个:

String nextIn = input.next();
int theAnswer = 0;

if (nextIn.equal("e")) {
    System.out.println("Exiting the game...")
    break;
}
else {
    theAnswer = Integer.parseInt(nextIn);
}

我显然没有考虑到异常(exception)情况,但如果你愿意的话也可以。

总的来说,它看起来像这样:


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 + " = ? ");

            //new part:
            String nextIn = input.next();
            int theAnswer = 0;
            if (nextIn.equals("e")) {
                System.out.println("Exiting the game...")
                break;
            } else {
                theAnswer = Integer.parseInt(nextIn);
            }

            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;

                    }

                }
            }
        }

    }
}

关于java - 如何随时按 e 退出游戏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58635218/

相关文章:

java - 在 Java 中使用套接字解析和发送 HTTP 请求的正确方法是什么?

java - BitmapFactory.decodeStream 返回 null

java - mvn install -U 和 mvn install 有什么区别?

java - IBM RAD/RSA : "Generate Java bean skeleton" Does Not Display When Right-Clicking WSDL File

java - 单击列表底部获取列表顶部的元素

java - 如何使用 xml 在 spring security 中禁用注销确认?

java - Java 编译器和 JVM 是用哪种语言编写的?

java - Android 使用 AsyncTask 连接 MySql 并以 JSON 或 String 的形式返回数据

java - 如何覆盖 Lombok Setter 方法

java - Eclipse:保存服务器响应并在服务器(数据源)出现故障时返回它们