gpt4 book ai didi

java - 用于将变量输入数组的 for 循环不起作用?

转载 作者:行者123 更新时间:2023-12-01 15:17:42 24 4
gpt4 key购买 nike

我试图在 while 循环内将 3 个不同的变量输入到数组中,只要我不为任何变量输入 stop 即可。 while 循环仅在第一个变量未停止时让我输入第二个变量值,同样输入第三个变量值

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

  1. 名称:afasdf

  2. 额外信息:afdsaf

  3. 单位成本:123123214

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

  5. 单位成本: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 中,习惯上使用驼峰命名法,其中第一个单词是小写的。例如,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”(注意:您正在检查“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

关于java - 用于将变量输入数组的 for 循环不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11406371/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com