Testing out a simple while loop but I'm confused, if bottle
is 1
,name is "bottle"
.
But even when bottle
is greater that 1
, it still prints "bottle"
and not "bottles"
public class ex {
public static void main(String[] arg) {
int bottle = 0;
String name = "bottles";
while (bottle < 100) {
if (bottle == 1) {
name = "bottle";
}
System.out.println(bottle + " " + name);
++bottle;
}
}
}
Variable name
keeps the value in singular as is never reasigned with the "bottles" value. Try this:
public static void main(String[] arg) {
int bottle = 0;
String name = "bottles";
while( bottle < 100) {
name = "bottles"
if(bottle == 1) {
name = "bottle";
}
System.out.println(bottle + " " + name);
++bottle;
}
}
Once you set name = "bottle"; then name will always be "bottle" unless you change it back. After the if statement add:
else {
name = "bottles";
}
if (bottle == 1) {
name = "bottle";
}else{
name = "bottles";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.