如何使 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");
}
}
最佳答案
将x = x - y;
放入for循环内。这将使 x
代表当前卡在墙上的瓶子数量,即,在您的示例中,x
每次迭代都会减少 3。
关于java - 如何使for循环扫描器代码下降一定数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14924794/