Java绕过nextLine命令

时间:2017-02-05 01:18:14

标签: java

这是我写过的第一个Java程序,所以请放轻松。

拥有java.util.Scanner命令......

然后命令Scanner sc = new scanner(System.in)

然后我用不同的单词来回答答案,Java绕过其他所有问题。但是,如果我要为所有内容回答N或n,则代码字是完美的。

回答y时,Netbeans的输出窗口中发生了什么

First we have Cambridge Shingles
Would you like some (Y/N)y
How many would you like? 10
Next we have ArmorPlast Classic
Would you like some (Y/N)Next we have 250 Torch applied cap sheet
Would you like some (Y/N)n
Next we have 2.2mm Torch applied base sheet
Would you like some (Y/N)n
Next we have R5 Poly Iso insulation One Inch
Would you like some (Y/N)n

这里当我输入N或n时会发生什么......

First we have Cambridge Shingles.
Would you like some (Y/N)n
Next we have ArmorPlast Classic torch applied cap sheet.
Would you like some (Y/N)n
Next we have 250 Torch applied cap sheet.
Would you like some (Y/N)n
Next we have 2.2mm Torch applied base sheet.
Would you like some (Y/N)n
Next we have R5 Poly Iso insulation One Inch.
Would you like some (Y/N)n

//产品的输入块

        System.out.println("First we have " + shingleName);
        System.out.print("Would you like some " +"(Y/N)");
            String answer = sc.nextLine();
            if (answer.equals("Y") || answer.equals("y")) {
                System.out.print("How many would you like? ");
                int shingleUnits = sc.nextInt();
                costShingleUnits = shingleUnits * shinglePrice;
            } else {
                costShingleUnits = 0;
            }


        System.out.println("Next we have " + appRoll);
        System.out.print("Would you like some " +"(Y/N)");
            String answerOne = sc.nextLine();
            if (answerOne.equals("Y") || answerOne.equals("y")) {
                System.out.print("How many would you like? ");
                int appRollUnits = sc.nextInt();
                costAppRoll = appRollUnits * appRollPrice;
            } else {
                costAppRoll = 0;
            }

        System.out.println("Next we have " + torchIKO);
        System.out.print("Would you like some " +"(Y/N)");
            String answerTwo = sc.nextLine();
            if (answerTwo.equals("Y") || answerTwo.equals("y")) {
                System.out.print("How many would you like? ");
                int torchIKOUnits = sc.nextInt();
                costTorchIKO = torchIKOUnits * torchIKOPrice;
            } else {
                costTorchIKO = 0;
            }

        System.out.println("Next we have " + torchBase);
        System.out.print("Would you like some " +"(Y/N)");
            String answerThree = sc.nextLine();
            if (answerThree.equals("Y") || answerThree.equals("y")) {
                System.out.print("How many would you like? ");
                int torchBaseUnits = sc.nextInt();
                costTorchBase = torchBaseUnits * torchBasePrice;
            } else {
                costTorchBase = 0;
            }

        System.out.println("Next we have " + polyISO);
        System.out.print("Would you like some " +"(Y/N)");
            String answerFour = sc.nextLine();
            if (answerFour.equals("Y") || answerFour.equals("y")) {
                System.out.print("How many would you like? ");
                int polyISOUnits = sc.nextInt();
                costPolyISO = polyISOUnits * polyISOPrice;
            } else {
                costPolyISO = 0;
            }

0 个答案:

没有答案