首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我如何停止在循环过程中重新掷骰子?

我如何停止在循环过程中重新掷骰子?
EN

Stack Overflow用户
提问于 2013-11-18 20:42:30
回答 1查看 274关注 0票数 1

我如何让骰子不会在菜单每次出现时都重新滚动:

当我按下菜单1选项时,循环发生了,我如何才能使值保持不变,直到我选择要重新掷骰子:

代码语言:javascript
复制
import java.io.InputStream;
import java.util.Scanner;

class RecordDice {
    public static void main(String[] args) {

        int dSides, Sides, Choice;
        int max = 0, min = 0;
        Scanner s = new Scanner(System. in );
        Scanner c = new Scanner(System. in );

        boolean keepgoing = true;

        System.out.println("How many sides should the dice have?");
        Sides = s.nextInt();
        if (Sides == 4 || Sides == 6 || Sides == 12 || Sides == 20 || Sides == 100) {

            while (keepgoing) {

                System.out.println("Please make a choice:\n" +
                    "1 - reroll the dice\n" +
                    "2 - get the value\n" +
                    "3 - show the maximum\n" +
                    "4 - show the minimum");

                Dice2 d = new Dice2(Sides);
                Choice = c.nextInt();
                int Value = d.getValue();

                if (Value > max) {
                    max = Value;
                }
                if (Value < min) {
                    min = Value;
                }


                if (min > max) {
                    max = min;
                }

                switch (Choice) {
                    case 1:

                        d.reroll();
                        break;
                    case 2:
                        System.out.println("The current value is " + Value);
                        break;
                    case 3:
                        System.out.println("The maximum is " + max);
                        break;
                    case 4:
                        System.out.println("The minimun is " + min);
                        break;
                }
            }
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2013-11-18 20:55:53

像这样修改你的代码:

代码语言:javascript
复制
System.out.println("How many sides should the dice have?");
Sides = s.nextInt();

if(Sides == 4 || Sides == 6 || Sides == 12  || Sides == 20 ||  Sides == 100){
   Dice2 d = new Dice2(Sides);

    while (keepgoing) {
        System.out.println("Please make a choice:\n" +
            "1 - reroll the dice\n" +
            "2 - get the value\n" +
            "3 - show the maximum\n" +
            "4 - show the minimum");

           Choice = c.nextInt();
           int Value = d.getValue();

           if(Value > max){
              max = Value;
           }
           if(Value < min){
              min = Value;
           }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20048095

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档