在循环中跳过键盘

时间:2016-11-19 00:41:35

标签: java

我正在创建一个代码,只是创建一个篮子来放置它。这是一项家庭作业,我通过在类中显示继承并将变量扩展到子类来完成。最简单的事就是抓住我。我的while循环只会在while的第一个循环中从键盘请求水果类型的信息,此后循环跳过它并移动到多少。我究竟做错了什么;请记住,我是一个新手,这个代码可能没有设置为专业级别,没关系。

    while((makeBasket != 'n') && (count <= 10)){

        System.out.print("What fruit would you like? ");
        fruitRequest[count] = keyboard.nextLine();

        fruitColor = getColor(fruitRequest[count]);

        System.out.print("How many " + fruitRequest[count] + " would you like? ");
        fruitCount = keyboard.nextInt();


        Fruit fruitBasket = new Fruit(fruitRequest[count], fruitColor, fruitCount);

        fruitBasket.print();
        for(int i = 0; i< 3; i++){
            switch(i){
                case 0:
                    basketRecipt[i][count] = fruitCount; 
                    break;
                case 1:
                    basketRecipt[i][count] = fruitCount * fruitBasket.getPrice(fruitRequest[count], fruitColor, fruitCount);
                    break;
                case 2:
                    basketRecipt[i][count] = fruitCount * fruitBasket.getWeight(fruitRequest[count], fruitColor, fruitCount);
                    break;
            }
            }
        count +=1;
    }

1 个答案:

答案 0 :(得分:0)

我的直觉是你需要在fruitCount任务之后加入keyboard.nextLine();

stem.out.print("How many " + fruitRequest[count] + " would you like? ");
fruitCount = keyboard.nextInt();
keyboard.nextLine();

nextInt()只沿当前行前进,所以当while循环再次运行时它会keyboard.nextLine()并在你输入的int之后得到一个空字符串的空字符串。