Java:如何在最后一个 运行 之后结束 'do while'' 循环
Java : How to end 'do while'' loop after last run
如何更改我的代码,使最后打印出来的是空打印而不是“+”?
根据runLoop(x)
中给定的x个数,代码打印出一行'+'。
例如 runLoop(5)
打印 '+++++'
,在此之后我想得到一个空的打印,但我以“+”结尾。
编辑:我认为这是因为程序在检查 'while' 之前运行 'do'。第一次程序运行 runLoop(5),所以它期望输出为 ''+++++'',第二次运行 runLoop(0) 所以程序期望收到一个空行,而不是 '' +''。我想要做的是获取空行而不是“+”。
public static void runLoop(int x){
do {
System.out.print("+");
x--;
}while ( x > 0);
System.out.println("");
}
我不确定我是否理解你的问题。但是,如果您希望最后的输出不同,只需将 while 设置为 x=>0
并在
之后打印一些内容
public static void runLoop(int x){
do {
System.out.print("+");
x--;
}while ( x >= 0);
System.out.println("");
//you can print what you need after the while loop
//System.out.println("");
}
我似乎无法用您提供的代码复制您描述的错误。你确定方法外没有打印+吗?
为什么不反过来打印最后一个换行符呢?
private static void loop(int x) {
while ( x > 1) {
System.out.print("+");
x--;
}
System.out.println("+");
}
我不认为它有问题
public static void runLoop(int x){
string token = ""
do {
System.out.print(token);
x--;
token = "+"
}while ( x >= 0);
System.out.println("");
}
public static void runLoop(int x){
if(x < 1){
System.out.println("");
return;
}
do {
System.out.print("+");
x--;
}while ( x > 0);
System.out.println("");
}
如何更改我的代码,使最后打印出来的是空打印而不是“+”?
根据runLoop(x)
中给定的x个数,代码打印出一行'+'。
例如 runLoop(5)
打印 '+++++'
,在此之后我想得到一个空的打印,但我以“+”结尾。
编辑:我认为这是因为程序在检查 'while' 之前运行 'do'。第一次程序运行 runLoop(5),所以它期望输出为 ''+++++'',第二次运行 runLoop(0) 所以程序期望收到一个空行,而不是 '' +''。我想要做的是获取空行而不是“+”。
public static void runLoop(int x){
do {
System.out.print("+");
x--;
}while ( x > 0);
System.out.println("");
}
我不确定我是否理解你的问题。但是,如果您希望最后的输出不同,只需将 while 设置为 x=>0
并在
public static void runLoop(int x){
do {
System.out.print("+");
x--;
}while ( x >= 0);
System.out.println("");
//you can print what you need after the while loop
//System.out.println("");
}
我似乎无法用您提供的代码复制您描述的错误。你确定方法外没有打印+吗?
为什么不反过来打印最后一个换行符呢?
private static void loop(int x) {
while ( x > 1) {
System.out.print("+");
x--;
}
System.out.println("+");
}
我不认为它有问题
public static void runLoop(int x){
string token = ""
do {
System.out.print(token);
x--;
token = "+"
}while ( x >= 0);
System.out.println("");
}
public static void runLoop(int x){
if(x < 1){
System.out.println("");
return;
}
do {
System.out.print("+");
x--;
}while ( x > 0);
System.out.println("");
}