System.out.println不起作用

时间:2015-10-16 02:26:16

标签: java

我是学习java编程的初学者。基本上,我不能让我的代码的最后一点工作。在我向您展示我的代码之前,我认为展示结果应该是一个好主意。该计划的结果应该是:

Please Enter either S(supply) or R(replenish) followed by ID and quantity.
R     p122.   10
New Stock-level for p122(Chain) is 58
S.    p124.   20
New Stock-level for p125(Pedal) is 18
S.    p905.   20
No part found with ID p905
.....// enter empty string to terminate
//Show final stock levels of all Parts

虽然,我确实能够执行主要计算和所有内容,但我无法打印出所有零件的最终库存水平。我真的不明白为什么。 这是我的代码:

import java.util.Scanner;

public class TestPart {

public static void main(String[] args) {

    // Array of 5 Part objects
    // Part[] part = new Part[5];
    Part[] part = new Part[5];
    part[0] = new Part("p122", "Chain", 48, 12.5);
    part[1] = new Part("p123", "Chain Guard", 73, 22.0);
    part[2] = new Part("p124", "Crank", 400, 11.5);
    part[3] = new Part("p125", "Pedal", 3, 6.5);
    part[4] = new Part("p126", "Handlebar", 123, 9.50);

    ///////// Test Class 2 ////////
    Scanner scanner = new Scanner(System.in);
    System.out.println("Please Enter either S (supply) or R (replenish) followed by ID and quantity.");
    while (scanner.hasNext()) {

        String sOrR = scanner.next();
        String inputId = scanner.next();
        int amount = scanner.nextInt();

        for (int i = 0; i < 5; i++) {
            String id = part[i].getID();
            // Find ID in array
            if (id.equals(inputId)) {
                // S or R
                if (sOrR.equals("R")) {
                    part[i].replenish(amount);
                } else {

                    part[i].supply(amount);

                }
                System.out.println("New Stock-level for " + part[i].getID() + "(" + part[i].getName() + ") is "
                        + part[i].getStockLevel());
            }

        }
        if ((inputId.equals(part[0].getID()) == false) && (inputId.equals(part[1].getID()) == false)
                && (inputId.equals(part[2].getID()) == false) && (inputId.equals(part[3].getID()) == false)
                && (inputId.equals(part[4].getID()) == false)) {
            System.out.println("No part found with ID " + inputId);
        }

    }
    scanner.close();

    System.out.println("Final stock level for all the parts: ");
    for (int i = 0; i < 5; i++) {
        System.out.println("Final Stock-level for " + part[i].getID() + "(" + part[i].getName() + ") is "
                + part[i].getStockLevel());
    }
}
}

我的程序完美地执行计算部分。但是它没有显示最终的库存水平。

 Please Enter either S(supply) or R(replenish) followed by ID and quantity.
    R     p122.   10
    New Stock-level for p122(Chain) is 58
    S.    p124.   20
    New Stock-level for p125(Pedal) is 18
    S.    p905.   20
    No part found with ID p905      

1 个答案:

答案 0 :(得分:0)

每当用户输入空字符串时,您的中止条件(即scanner.hasNext())都不会退出while循环。我不知道您是否已经注意到,但只要用户只点击返回键,就不会发生任何事情,因为Scanner.next仅在返回时不会触发。虽然要注意它存储您的输入。这意味着一旦你输入&#34;有效&#34;输入(例如abc),Scanner将为您提供用户在有效输入之前输入的所有内容。只是一个小例子来证明我的意思:

Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()) {
    System.out.println("\"" + scanner.next() + "\"");
}
scanner.close();
System.out.println("finished");

因此,如果您想在用户输入空行后中止您的程序,则java.util.Scanner.hasNext无法执行此操作。我建议你为你的&#34; S或R&#34;添加另一个角色。允许用户退出程序的选项,如下所示:

if(sOrR.equals("E")) break;

您应将其直接放在String sOrR = scanner.next();

之后