Java 用于将变量输入数组的for循环不工作?

Java 用于将变量输入数组的for循环不工作?,java,arrays,Java,Arrays,我试图将3个不同的变量输入while循环中的一个数组,只要我没有为任何变量输入stop。while循环只允许我在第一个变量没有停止时输入第二个变量值,同样,输入第三个变量值 现在,第一个循环运行良好,我可以输入所有3个变量,但第二次和第三次,for循环输出第一个变量,但不允许我在跳到第二个变量之前输入值。 我的意思是: 姓名:afasdf 额外信息:afdsaf 单位成本:123214 名称:额外信息:adflskjflk 此外,输入Stop也不会结束循环 单位成本:123217 我知道这个循环

我试图将3个不同的变量输入while循环中的一个数组,只要我没有为任何变量输入stop。while循环只允许我在第一个变量没有停止时输入第二个变量值,同样,输入第三个变量值

现在,第一个循环运行良好,我可以输入所有3个变量,但第二次和第三次,for循环输出第一个变量,但不允许我在跳到第二个变量之前输入值。 我的意思是:

  • 姓名:afasdf

  • 额外信息:afdsaf

  • 单位成本:123214

  • 名称:额外信息:adflskjflk 此外,输入Stop也不会结束循环

  • 单位成本:123217

  • 我知道这个循环在只有一个变量的情况下有效,我尝试过使用for循环而不是while循环,并添加了大量的else语句,但它似乎保持不变

    我设置断路器的方式有问题吗? 我设置最后一个断路器的方式(即使我为一个双变量设置了“停止”也会停止)是否会弄乱循环的其余部分

    多谢各位

    这是我的密码

    ArrayItem s = new ArrayItem(); 
    
    String Name = null, ID = null;  
    double Money = 0;
    
    boolean breaker = false;
    while(breaker ==false)
    {
       System.out.print("Name:" + "\t");
       Name = Input.nextLine();
    
       if(Name.equals("Stop")) //see if the program should stop
           breaker = true;
    
       System.out.print("Extra Info:" + "\t");
       Details = Input.nextLine();
    
       if(ID.equals("Stop")) 
           breaker = true;
    
       System.out.print("Unit Cost:" + "\t");
       Money = Input.nextDouble();
    
       // suppose to let me stop even if i input stop
       //  when the variable is suppose to be a double
       if(Input.equals("stop") || Input.equals("stop"))
          breaker = true;
       else
          s.SetNames(Name);
    
       s.SetInfo(Details);
       s.SetCost(Money);
    }
    

    关于代码的一些事情:
    “Name:”+“\t”
    可以简化为
    “Name:\t”
    。这对于代码的其余部分是正确的。在Java中,习惯上使用camelcase,其中第一个单词是小写的。例如,
    s.SetMoney
    将是
    s.SetMoney
    。此外,变量遵循相同的规则,
    Money
    将是
    Money
    ID
    将是
    ID
    。如果你的老师教你其他的方法,那就按照他们的方式去做

    该循环也应该是do while循环:

    do
    {
        // read each value in sequence, and then check to see if you should stop
        // you can/should simplify this into a function that returns the object
        // that returns null if the value should stop (requiring a capital D
        // double for the return type)
    
        if ( /* reason to stop */)
        {
            break;
        }
    
        s.setNames(name);
        s.setId(id);
        s.setMoney(money);
    
    } while (true);
    
    private String getString(Scanner input)
    {
        String result = input.nextLine();
    
        // look for STOP
        if (result.equalsIgnoreCase("stop"))
        {
            result = null;
        }
    
        return result;
    }
    
    private Double getDouble(Scanner input)
    {
        Double result = null;
        // read the line is a string looking for STOP
        String line = getString(input);
    
        // null if it's STOP
        if (line != null)
        {
            try
            {
                result = Double.parseDouble(line);
            }
            catch (NumberFormatException e)
            {
                // not a valid number, but not STOP either!
            }
        }
    
        return result;
    }
    
    这里面有很多概念,但它们会随着你的进步而有所帮助。我让你把这些碎片拼起来

    此外,您确实需要修复括号,但这不是唯一的问题。因为
    Money
    是一个
    双精度的
    ,所以您必须以
    字符串的形式读取该值。我怀疑
    Input
    是一个
    Scanner
    对象,因此您可以检查
    Input.hasNextDouble()
    如果不是,那么您可以有条件地检查
    字符串
    值以查看它是否为“stop”(注意:您正在检查“stop”和“stop”,这两个值不相等)。您最后一次的无机会检查将扫描仪与“停止”进行比较,这永远不会是真的。检查

    System.out.print("Unit Cost:\t");
    
    if (Input.hasNextDouble())
    {
        Money = Input.nextDouble();
    
        // you can now set your object
        // ...
    }
    // it's not a double; look for "stop"
    else if (Input.nextLine().equalsIgnoreCase("stop"))
    {
        // exit loop
        break;
    }
    
    // NOTE: if it's NOT a double or stop, then you have NOT exited
    //       and you have not set money
    

    在if和else主体周围使用括号(
    {}
    )。例如,底部的else语句只执行
    s.SetNames(Name)
    ,它总是执行
    s.SetInfo(Details)
    s.SetCost(Money)
    。最后,查看
    break
    关键字,它将以最基本的形式立即退出其包含循环
    if(Input…{breaker=true;}else{s.SetNames(Name);s.SetInfo(Details);s.SetCost(Money)}
    (在代码中使用空行分隔)。我添加了括号,但我仍然遇到相同的问题
    if(Input.equals(“stop”)| Input.equals(“stop”))
    我明白了,你不会冒险。我的妄想症不起作用了=.=输入Stop仍然不能阻止while loopWell在这种情况下至少你在
    输入上调用
    equals
    ,它可能是一个字符串,而不是
    字符串。你将来有一个
    NumberFormatException
    。我试过这个,但是当我尝试在双变量中设置Stop时,我遇到了一个错误谢谢,现在当我设置Stop时,程序在Money处停止,但是while循环停止工作;为什么要在getString()中检查“停止”?它是一个可重用的方法,用于从控制台读取输入。@sreehari,因为如果我不检查它,那么该方法就没有任何用途。如果不需要在方法中进行检查,则只需使用
    input.nextLine()
    breaker = true;
    while(breaker){
       Name = readInput("Name");
       Details = readInput("Details");
       Money = Double.parseDouble(readInput("Money"));
    
       if(Name.equals("stop") || Details.equals("stop"))
           breaker = false;
       else {
          // set ArrayItem
       }
    }
    
    private static String readInput(String title){
       System.out.println(title+":");
       //... read input
       // return value
    }