我一直在尝试创建一个for loop打印任意两个输入之间的所有整数integers.
例如:
如果我输入1和4,它应该打印1,2,3,4.
但是,我当前的代码只打印最终输入.(标记为"结束"的那个)
什么可以用我的代码?请注意,if语句会检查以确保输入有效.
{
int start = Integer.parseInt(startingInput.getText());
int end = Integer.parseInt(endingInput.getText());
if (start >= end) {
hintLabel.setText("Please input valid numbers");
}
else {
for (int count = start; count <= end; count++) {
outputArea.setText(count + ", ");
}
}
}
Run Code Online (Sandbox Code Playgroud) 我一直在搜索stackoverflow,我发现了一些关于将字符串转换为布尔值的其他问题,但我无法使其工作.也许这只是我尝试使用它的方式是不正确的.
无论如何,我试图将两个不同的输入字符串"M"或"I"转换为布尔值,以便在if语句中使用.什么基本上是希望功能是这样的:
// the text that is retrieved is assumed to be either"M" or "I"
M=Input.getText
I=Input.getText
If M shows the value "M",
do stuff here
else if I shows the value "I",
do stuff here
else if neither above are true,
throw an exception here
Run Code Online (Sandbox Code Playgroud)
我已经尝试了任何数量的"toBoolean"和"Boolean.valueof",但我尝试的都没有.
PS,很抱歉没有真正的代码可以使用,这是我的第一步,因此我没有围绕这篇文章构建任何东西.