程序跳过while循环中的一行代码,Java

时间:2017-01-07 14:57:29

标签: java

我是Java编程的初学者,我遇到了一个问题。如果你看一下我的代码:`package whileloops;

import java.util.Scanner;

public class Empty_The_Piles {

    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);

        int pileA, pileB, pileC;
        pileA = 3;
        pileB = 3;
        pileC = 3;
        int piletotal = pileA + pileB + pileC;


        while(piletotal >= 0){

            String pile = "";
            System.out.println("A: " + pileA + "\tB: " + pileB + "\tC: " + pileC);
            System.out.println("\nChoose a pile: "); 
            pile = keyboard.nextLine(); //After 1 loop it jumps over this line or something..

            System.out.println("How many fishes do you want to remove from pile " + pile + "?");
            int amount = keyboard.nextInt();

            if(pile.equals("A"))
                pileA = pileA - amount;
            else if(pile.equals("B"))
                pileB = pileB - amount;
            else if(pile.equals("C"))
                pileC = pileC - amount;
            else
                System.out.println("ERROR. NO SUCH PILE!");
        }
        System.out.println("All piles are empty! Nice work!");
    }

}

编辑: 当代码在它的第二个循环上时,它突然跳过它。当我第一次选择代码时它运行代码很好,但当它循环回到开始它跳过 pile = keyboard.nextLine();

它会直接跳到下一行......它不会等待输入

如果我尝试你所说的另一个帖子是答案,我需要输入两次。

猜猜这对大多数人来说很容易:P

  • 感谢您的帮助:)

0 个答案:

没有答案