- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图在 while 循环内将 3 个不同的变量输入到数组中,只要我不为任何变量输入 stop 即可。 while 循环仅在第一个变量未停止时让我输入第二个变量值,同样输入第三个变量值
现在,第一个循环运行良好,我可以输入所有 3 个变量,但是第二次和第三次,for 循环输出第一个变量,但不允许我在跳到第二个变量之前输入值。我的意思是:
名称:afasdf
额外信息:afdsaf
单位成本:123123214
名称:额外信息: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 中,习惯上使用驼峰命名法,其中第一个单词是小写的。例如,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
是 double
,因此您必须将该值读取为 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/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!