如何使一个for循环扫描器代码在一定数量下运行?例如,输入30,然后输入3,然后它就像30,27,24,21,等等。
这就是我到目前为止所拥有的。我想让int y表示一次取下的数量。
import java.util.Scanner; // ...
public class b
{
public static void main(String[] args) {
int x,y;
Scanner scannerObject = new Scanner(System.in);
System.out.println("how many bottles hanging on the wall");
x = scannerObject.nextInt();
System.out.println("how many bottles are taken down the wall");
y = scannerObject.nextInt();
for (int counter = x; counter > 0; counter--)
{
if (counter == 1)
{
System.out.println(" " + counter + " bottle hanging on the wall");
}
else
{
System.out.println(" " + counter + " bottles hanging on the wall");
}
System.out.println("And if one bottle should accidently fall, ");
}
System.out.println("No bottles hanging on the wall");
}
}发布于 2013-02-18 03:06:49
将x = x - y;放入for循环中。这将使x表示当前挂在墙上的瓶子的数量,也就是说,x将在每次迭代时递减3。
发布于 2013-02-18 03:13:14
将for循环替换为:
for (int counter = x; counter > 0; counter-=y)这将为您提供所需的输出。
counter-=y更改等同于counter=counter-y
https://stackoverflow.com/questions/14924794
复制相似问题