第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何隨時按 e 退出游戲?

如何隨時按 e 退出游戲?

不負(fù)相思意 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條經(jīng)驗 獲得超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;


                    }


                }

            }

        }


    }

}



查看完整回答
反對 回復(fù) 2024-01-17
  • 1 回答
  • 0 關(guān)注
  • 149 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號