我有几个字符串变量,并且在我将它们转换为整数之后,我试图将它们放入while子句中。
以下实际代码:
public void setUp()
{
String HText = "";
int CorrectAnswer = Integer.parseInt(Levels.CorrectAnswer);
int Question1 = Integer.parseInt(Levels.Question1);
int Remaining = CorrectAnswer - Question1;
TextView TVHint = (TextView) findViewById(R.id.hint);
//HintText.setText(String.valueOf(Remaining));
/*while (Remaining != 0)
{*/
if (Remaining >= 100)
{
Remaining = Remaining - 100;
if (HText != "")
{
HText = HText + " + " + "100";
}
else
{
HText = "100";
}
}
else
{
if (Remaining >= 10)
{
Remaining = Remaining - 10;
if (HText != "")
{
HText = HText + " + " + "10";
}
else
{
HText = "10";
}
}
else
{
if (Remaining >= 5)
{
Remaining = Remaining - 5;
if (HText != "")
{
HText = HText + " + " + "5";
}
else
{
HText = "5";
}
}
else
{
if (Remaining >= 1)
{
Remaining--;
if (HText != "")
{
HText = HText + " + " + "1";
}
else
{
HText = "1";
}
}
}
}
}
//}
TVHint.setText(HText);
}
此代码可以使用,但我希望它重复,直到剩余为0并且当时注释掉的内容目前无效。
答案 0 :(得分:0)
请尝试Integer.parseInt(var1)
。
答案 1 :(得分:0)
你的程序进入无限循环。因为您将var1Int
的值设置为等于1,并且在while
循环中条件为
while (var1Int != 0)
即。当var1Int
变为等于0时,它将从循环中出来,因为循环变量var1Int
的值没有改变,所以它永远不会发生。因此循环继续无限执行。您应该编写代码来更改var1Int
的值,以便满足退出条件。
例如,您可以尝试这个
String var1 = "1";
int var1Int = Integer.valueOf(var1);
while (var1Int != 0)
{
var1Int --;
}
这将使var1Int
值等于0,这是终止条件,它将退出循环。这只是一个示例,您可以根据您的要求编写逻辑。
问题不在于将String
解析为int
。如果是这种情况,它只会在下一行给出错误。
int var1Int = Integer.valueOf(var1);
答案 2 :(得分:0)
它没有失败,循环工作正常,因为它是无限循环它将继续“永远”,尝试打印出来的东西,你会看到。
如果你想突破它只是减少你的价值,最终它会达到0:
while(a != 0){
System.out.println(var1Int);//so you can check the difference
var1Int--;
}
答案 3 :(得分:0)
确保Levels.CorrectAnswer
和Levels.Question1
包含integer
值的有效字符串。
例如:
Levels.CorrectAnswer
值为"110"
和Levels.Question1
值 是"1"
使用Math.abs(CorrectAnswer - Question1)
获取绝对Remaining
值。
int Remaining = Math.abs(CorrectAnswer - Question1);
添加一些logs
以检查Remaining
和HText
的值。
以下是工作代码:
public void setUp()
{
Levels Levels = new Levels();
String HText = "";
int CorrectAnswer = Integer.parseInt(Levels.CorrectAnswer);
int Question1 = Integer.parseInt(Levels.Question1);
int Remaining = CorrectAnswer - Question1;
Log.d("setUp", "CorrectAnswer: " + CorrectAnswer + "\n"
+ "Question1: " + Question1 + "\n" + "Remaining: " + Remaining);
TextView TVHint = (TextView) findViewById(R.id.hint);
while (Remaining != 0)
{
Log.d("setUp", "Remaining: " + Remaining);
if (Remaining >= 100)
{
Remaining = Remaining - 100;
Log.d("setUp", "Remaining - 100: " + Remaining);
if (HText != "")
{
HText = HText + " + " + "100";
}
else
{
HText = "100";
}
}
else
{
if (Remaining >= 10)
{
Remaining = Remaining - 10;
Log.d("setUp", "Remaining - 10: " + Remaining);
if (HText != "")
{
HText = HText + " + " + "10";
}
else
{
HText = "10";
}
}
else
{
if (Remaining >= 5)
{
Remaining = Remaining - 5;
Log.d("setUp", "Remaining - 5: " + Remaining);
if (HText != "")
{
HText = HText + " + " + "5";
}
else
{
HText = "5";
}
}
else
{
if (Remaining >= 1)
{
Remaining--;
Log.d("setUp", "Remaining - 1: " + Remaining);
if (HText != "")
{
HText = HText + " + " + "1";
}
else
{
HText = "1";
}
}
}
}
}
Log.d("setUp", "HText: " + HText);
}
TVHint.setText(HText);
}
输出日志:
04-18 20:39:52.634 23432-23432/com.ferdous.stackoverflowanswer D/setUp: CorrectAnswer: 110
Question1: 1
Remaining: 109
04-18 20:39:52.634 23432-23432/com.ferdous.stackoverflowanswer D/setUp: Remaining: 109
04-18 20:39:52.634 23432-23432/com.ferdous.stackoverflowanswer D/setUp: Remaining - 100: 9
04-18 20:39:52.634 23432-23432/com.ferdous.stackoverflowanswer D/setUp: HText: 100
04-18 20:39:52.634 23432-23432/com.ferdous.stackoverflowanswer D/setUp: Remaining: 9
04-18 20:39:52.634 23432-23432/com.ferdous.stackoverflowanswer D/setUp: Remaining - 5: 4
04-18 20:39:52.634 23432-23432/com.ferdous.stackoverflowanswer D/setUp: HText: 100 + 5
04-18 20:39:52.634 23432-23432/com.ferdous.stackoverflowanswer D/setUp: Remaining: 4
04-18 20:39:52.634 23432-23432/com.ferdous.stackoverflowanswer D/setUp: Remaining - 1: 3
04-18 20:39:52.634 23432-23432/com.ferdous.stackoverflowanswer D/setUp: HText: 100 + 5 + 1
04-18 20:39:52.634 23432-23432/com.ferdous.stackoverflowanswer D/setUp: Remaining: 3
04-18 20:39:52.634 23432-23432/com.ferdous.stackoverflowanswer D/setUp: Remaining - 1: 2
04-18 20:39:52.634 23432-23432/com.ferdous.stackoverflowanswer D/setUp: HText: 100 + 5 + 1 + 1
04-18 20:39:52.634 23432-23432/com.ferdous.stackoverflowanswer D/setUp: Remaining: 2
04-18 20:39:52.634 23432-23432/com.ferdous.stackoverflowanswer D/setUp: Remaining - 1: 1
04-18 20:39:52.634 23432-23432/com.ferdous.stackoverflowanswer D/setUp: HText: 100 + 5 + 1 + 1 + 1
04-18 20:39:52.634 23432-23432/com.ferdous.stackoverflowanswer D/setUp: Remaining: 1
04-18 20:39:52.634 23432-23432/com.ferdous.stackoverflowanswer D/setUp: Remaining - 1: 0
04-18 20:39:52.634 23432-23432/com.ferdous.stackoverflowanswer D/setUp: HText: 100 + 5 + 1 + 1 + 1 + 1
希望这会有所帮助〜