为了账号安全,请及时绑定邮箱和手机立即绑定

如何随时按 e 退出游戏?

如何随时按 e 退出游戏?

不负相思意 2024-01-17 17:12:12
如果有人按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;                    }                }            }        }    }} 
查看完整描述

1 回答

?
侃侃无极

TA贡献2051条经验 获得超10个赞

而不仅仅是“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);

}

显然我没有考虑到例外情况,但如果你愿意的话也可以。


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



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;


                    }


                }

            }

        }


    }

}



查看完整回答
反对 回复 2024-01-17
  • 1 回答
  • 0 关注
  • 44 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信