扫描仪在if语句中不起作用

时间:2016-10-27 19:53:37

标签: java if-statement

**好的,所以我正在尝试创建俄罗斯轮盘游戏。这段代码的一半应该是单向播放,另一种是另一种方式(如果有意义的话)。无论如何,我把它们分成两个单独的类,然后组合起来。我现在的问题是每次我尝试创建一个if语句来决定播放它的方式不起作用。我想,我想要一些帮助或暗示为什么会这样。有人能指出我正确的方向吗? (我删除了我在if语句中的尝试,我应该把它们放回去吗?)

P.S我是这个东西的菜鸟。我真的不明白。而且我很确定我的OddsEvenChoice if语句是否错误,但我稍后会担心。**

RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',
            max_depth=None, max_features='auto', max_leaf_nodes=None,
            min_impurity_split=1e-07, min_samples_leaf=1,
            min_samples_split=2, min_weight_fraction_leaf=0.0,
            n_estimators=100, n_jobs=1, oob_score=False, random_state=42,
            verbose=0, warm_start=False)

2 个答案:

答案 0 :(得分:1)

无需使用多个Scanner实例。像在开始时一样只实例化一次:Scanner scanner = new Scanner (System.in);并且只在每次需要时重复使用它。

这是一个基于您的代码的示例:

public static void main(String[] args) {


        Scanner scanner = new Scanner (System.in);
        int Mode;

        System.out.println("Please choose how you'd like to play.");
        System.out.println("To play by Odds & Evens, enter '1'");
        System.out.println("To play by Numbers, enter '2'");
        System.out.println("Enter game mode here: ");

        Mode = scanner.nextInt();

        System.out.println("You chose game mode " + Mode);

        System.out.print("Enter roulette number here: ");
        int Number = scanner.nextInt();

        RouletteWheel One = new RouletteWheel();

        One.spin();
        int r = One.getWinningNumber();

        System.out.println(r);

        if (r == Number)
            System.out.println("Congratulations, The" + Number + " is a winning number!");
        else                
            System.out.println("Sorry, " + Number + " is not a winning number.");

        if (r == 0)
            System.out.println("Congratulations, 0 is an automatic win!");


        System.out.println("Odds or Evens? Type '1' for odds"
                + " or '2' for evens: "); 
        int Numberr = scanner.nextInt(); 

        if (Numberr == 1)
            System.out.println("You choose Odds.");
        else 
            System.out.println("You choose Evens.");


        One.spin();
        int winningNumber = One.getWinningNumber();
        boolean n = One.OddEvenChoice();
        System.out.println("The wheel landed on...");
        System.out.println(winningNumber);


        if ((n == true) && (winningNumber % 2 == 0))
            System.out.println("You Win! E");
        else if ((n == false) && (winningNumber %2 == 1))
            System.out.println("You Win! O");
        else 
            System.out.println("You Lose.");
    }

它有效:

enter image description here

答案 1 :(得分:0)

我怀疑你的问题是你在同一个流上有多个扫描仪(扫描仪和商店都从System.in读取)我认为第一个扫描仪将消耗所有的输入,没有任何东西会进入第二个,它可能不是那么简单。

摆脱旅店,一直使用扫描仪,这可能会解决它。