在 java 中以相反的方式打印值
Printing the values in a reverse way in java
我正在研究一个问题,我需要在 java 中使用 for 循环以相反的方式打印值。我尝试过使用 while 循环和 for 循环,但直到需要时我才能够打印这些值。
请在下面找到我的代码:
假设:y=1000,w=150,z=100;
while(y>=w) {
y-=z;
System.out.println(y);
}
我需要打印如下值:
1000
900
800
700 and so on.
我是 Java 的新手,请多多帮助。
public class reverse {
public static void main(String args[]){
int start=1000, end=150, decrement=100;
while(y>=w){
System.out.println(y);
y-=z;
}
System.out.println(w);
}
}
当我运行时代码输出如下:
1000
900
800
700
600
500
400
300
200
150
自减或自增操作最好使用'for'循环。
如果您想使用条件进行迭代,请使用 'while' 循环。
int y=1000;
int z=100;
int w=150;
//loop to print values
for(int i=y;i>=w;i-=z)
{
//print
System.out.println(i);
}
我正在研究一个问题,我需要在 java 中使用 for 循环以相反的方式打印值。我尝试过使用 while 循环和 for 循环,但直到需要时我才能够打印这些值。
请在下面找到我的代码:
假设:y=1000,w=150,z=100;
while(y>=w) {
y-=z;
System.out.println(y);
}
我需要打印如下值:
1000
900
800
700 and so on.
我是 Java 的新手,请多多帮助。
public class reverse {
public static void main(String args[]){
int start=1000, end=150, decrement=100;
while(y>=w){
System.out.println(y);
y-=z;
}
System.out.println(w);
}
}
当我运行时代码输出如下: 1000 900 800 700 600 500 400 300 200 150
自减或自增操作最好使用'for'循环。 如果您想使用条件进行迭代,请使用 'while' 循环。
int y=1000;
int z=100;
int w=150;
//loop to print values
for(int i=y;i>=w;i-=z)
{
//print
System.out.println(i);
}