【问题标题】:for loop for inputting variables into array not working?用于将变量输入数组的for循环不起作用?
【发布时间】:2012-07-10 03:55:11
【问题描述】:

我正在尝试将 3 个不同的变量输入到 while 循环内的数组中,只要我没有为任何变量输入停止。如果第一个变量没有停止,while循环只假设让我输入第二个变量值,同样输入第三个变量值

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

  1. 姓名:afasdf

  2. 额外信息:afdsaf

  3. 单价:123123214

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

  5. 单价:123217

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

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

非常感谢

这是我的代码

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);
}

【问题讨论】:

  • 在 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 循环
  • 好吧,在这种情况下,至少您是在 Input 本身上调用 equals,这可能是 Scanner,而不是 String

标签: java arrays


【解决方案1】:

关于代码的一些事情:"Name:" + "\t" 可以简化为 "Name:\t"。其余代码也是如此。在 Java 中,习惯上在第一个单词是小写的情况下使用驼峰式。例如,s.SetMoney 将是 s.setMoney。此外,变量遵循相同的规则,其中MoneymoneyIDid。如果你的老师教你的不是其他方式,那么就跟随他们的风格吧。

循环也应该是一个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;
}

其中有很多概念,但它们应该会随着您的进步而有所帮助。我会让你把碎片拼凑起来。

此外,您确实需要修复括号,但这不是唯一的问题。因为Moneydouble,所以您必须将值读取为String。我怀疑Input 是一个Scanner 对象,所以你可以检查Input.hasNextDouble() 如果不是,那么你可以有条件地检查String 的值,看看它是否是“停止”(注意:你正在检查“停止" 和 "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

【讨论】:

  • 谢谢,现在程序在我设置 Stop 时停止在 Money,但是 while 循环停止工作;d
  • 为什么要在 getString() 中检查“停止”?这是一种从控制台读取输入的可重用方法。
  • @sreehari 因为如果我不检查它,那么该方法将没有任何用途。如果您不需要检查该方法,您应该使用 input.nextLine() 代替它。
【解决方案2】:
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
}

【讨论】:

  • 你未来有一个NumberFormatException
  • 我试过这个,但是当我尝试将 Stop 放入双变量时出现错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多