我如何让骰子不会在菜单每次出现时都重新滚动:
当我按下菜单1选项时,循环发生了,我如何才能使值保持不变,直到我选择要重新掷骰子:
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;
                }
            }
        }
    }
}发布于 2013-11-18 20:55:53
像这样修改你的代码:
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;
           }https://stackoverflow.com/questions/20048095
复制相似问题