android 变量不在循环内递增
android variables not incrementing inside loop
我正在尝试编写一个简单的 android 程序,它将按照以下格式将歌词打印到墙上的 99 瓶啤酒。
第 1 行:99 瓶啤酒在墙上...
第 2 行:98 瓶啤酒在墙上...
等等。
我正在尝试创建一个循环来打印第一行歌词,然后 increment/decrement 变量 "lineNumber"、"numOfBottles" 和 "lessNumOfBottles"。
我尝试了几种不同的方法和运算符,但我的变量在编译时从未改变,而且我在每一行歌词中得到相同的重复数字。
我做错了什么? (评论中的底部循环是我尝试的第一个,我只留下它作为自己的参考)
protected void onResume(){
super.onResume();
int lineNumber = 1;
int numOfBottles = 99;
int lessNumOfBottles = 98;
String line = "Line " + lineNumber + ":\n";
String lyrics= "Lyrics to 99 bottles of beer \n\n";
String song =line + numOfBottles + " bottles of beer on the wall," + numOfBottles + " bottles of beer, take one down, pass it around," + lessNumOfBottles + " bottles of beer on the wall.\n\n";
TextView textview = (TextView) findViewById(R.id.myTextView);
textview.setText(lyrics);
for(int i = 99;i >= 1;i--) {
if (numOfBottles >= 1) {
textview.append(song);
--numOfBottles;
--lessNumOfBottles;
++lineNumber;
}
}
/*for (int i=0;i<99;){
lineNumber++;
numOfBottles= --numOfBottles;
lessNumOfBottles= --lessNumOfBottles;
textview.setText(song);
textview.append(song);
i++;
}*/
}
您仅将字符串的值分配给 lineNumber
、numOfBottles
和 lessNumOfBottles
的第一个值一次。 line
和 song
变量永远不会更新。
试试这个:
protected void onResume(){
super.onResume();
int numLines = 100;
int numOfBottles = 99;
String line;
String lyrics= "Lyrics to 99 bottles of beer \n\n";
TextView textview = (TextView) findViewById(R.id.myTextView);
textview.setText(lyrics);
for(int i = numOfBottles;i >= 1;i--) {
if (numOfBottles >= 1) {
line = "Line " + numLines-i + ":\n";
textview.append(line + i + " bottles of beer on the wall," +
i + " bottles of beer, take one down, pass it around," +
i-1 + " bottles of beer on the wall.\n\n");
}
}
}
我正在尝试编写一个简单的 android 程序,它将按照以下格式将歌词打印到墙上的 99 瓶啤酒。
第 1 行:99 瓶啤酒在墙上...
第 2 行:98 瓶啤酒在墙上...
等等。
我正在尝试创建一个循环来打印第一行歌词,然后 increment/decrement 变量 "lineNumber"、"numOfBottles" 和 "lessNumOfBottles"。
我尝试了几种不同的方法和运算符,但我的变量在编译时从未改变,而且我在每一行歌词中得到相同的重复数字。
我做错了什么? (评论中的底部循环是我尝试的第一个,我只留下它作为自己的参考)
protected void onResume(){
super.onResume();
int lineNumber = 1;
int numOfBottles = 99;
int lessNumOfBottles = 98;
String line = "Line " + lineNumber + ":\n";
String lyrics= "Lyrics to 99 bottles of beer \n\n";
String song =line + numOfBottles + " bottles of beer on the wall," + numOfBottles + " bottles of beer, take one down, pass it around," + lessNumOfBottles + " bottles of beer on the wall.\n\n";
TextView textview = (TextView) findViewById(R.id.myTextView);
textview.setText(lyrics);
for(int i = 99;i >= 1;i--) {
if (numOfBottles >= 1) {
textview.append(song);
--numOfBottles;
--lessNumOfBottles;
++lineNumber;
}
}
/*for (int i=0;i<99;){
lineNumber++;
numOfBottles= --numOfBottles;
lessNumOfBottles= --lessNumOfBottles;
textview.setText(song);
textview.append(song);
i++;
}*/
}
您仅将字符串的值分配给 lineNumber
、numOfBottles
和 lessNumOfBottles
的第一个值一次。 line
和 song
变量永远不会更新。
试试这个:
protected void onResume(){
super.onResume();
int numLines = 100;
int numOfBottles = 99;
String line;
String lyrics= "Lyrics to 99 bottles of beer \n\n";
TextView textview = (TextView) findViewById(R.id.myTextView);
textview.setText(lyrics);
for(int i = numOfBottles;i >= 1;i--) {
if (numOfBottles >= 1) {
line = "Line " + numLines-i + ":\n";
textview.append(line + i + " bottles of beer on the wall," +
i + " bottles of beer, take one down, pass it around," +
i-1 + " bottles of beer on the wall.\n\n");
}
}
}