Java For循环按顺序打印数字并将其反转
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20737798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
For loop to print the number in sequence and reverse it
提问by spartans
How to print the following output with only one for-loop in java?
如何在java中仅用一个for循环打印以下输出?
1 2 3 4 5 6 7 8 9 1 0 9 8 7 6 5 4 3 2 1
Code snippet:
代码片段:
class Series{
public static void main(String args[]){
for(int i=1; i<=10; i++){
System.out.println(i);
}
System.out.println(i);
for(int j=9; j>=0; j--){
System.out.println(j);
}
}
My program's in the following manner. Can anyone correct it?
我的程序如下。任何人都可以纠正它吗?
回答by bcorso
public static void main(String...strings ){
int dir = 1;
for(int i=1; i>0; i+=dir){
if(i == 10)
dir = -1;
System.out.print(i+" ");
}
}
Output:
输出:
1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1
回答by Myth
try this
尝试这个
int j = 10;
for (int i = 1; i <= 10; i++) {
if(i<10)
System.out.print(" " +i);
if(i==10){
i--;
System.out.print(" " +j);
if(j==1){
i++;
}
j--;
}
}
OutPut
输出
1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1
回答by ug_
Something like this?
像这样的东西?
for(int i=0;i<20;i++) {
if((i/10)%2 == 0)
System.out.print(i%10 + " ");
else
System.out.print((10-(i%10)) + " ");
}
回答by zain27
The series in the question is wrong.
问题中的系列是错误的。
It should be: 1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1
应该是: 1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1
The code, in one loop, is as follows:
一个循环中的代码如下:
int ctr = 1;
for(int i = 1; i > 0; i += ctr)
{
if(i == 10)
{
ctr = -1;
}
System.out.print(i + " ");
}
回答by eatSleepCode
Try this code, You just need a if condition in for loop.
试试这个代码,你只需要在 for 循环中使用 if 条件。
int i = 1;
for(int j=1; j<=20; j++)
{
if(j<11)
System.out.print(j+" ");
else
{
System.out.print((j - i == 10 ?" ": (j-i + " ")));
i = i+2;
}
}
回答by Rajivgandhi
public class forLoopTest {
public static void main(String[] args) {
for (int i = 1; i < 10; i++) {
System.out.print(i + " ");
}
for (int j = 10; j >= 1; j--) {
System.out.print(j + " ");
}
}
}
回答by Prashant Negi
Every sequence follows a pattern, Let's try finding one in this.
每个序列都遵循一个模式,让我们尝试在其中找到一个。
To work with this code, analyze What loop would print with the variablethat you incrementand What you want in the output?
要使用此代码,请分析哪个循环将使用您递增的变量打印以及您想要的输出是什么?
In your problem, assuming that the number you are entering is entered by user i.e. n, you want 2*n - 1 numbers in your sequence. Hence we now have the limits of our loop
在您的问题中,假设您输入的数字是由用户输入的,即n,您希望序列中有 2*n - 1 个数字。因此我们现在有了循环的限制
For n=5, Under no Conditions the loop would simply print a sequence like this
对于 n=5,在没有条件的情况下,循环将简单地打印这样的序列
1 2 3 4 5 6 7 8 9provided you are starting your loop from 1.
1 2 3 4 5 6 7 8 9如果您从 1 开始循环。
The sequence you want is 1 2 3 4 5 4 3 2 1.
你想要的序列是1 2 3 4 5 4 3 2 1。
Now looking at both the sequences you can see that the sequence is same till the mid point that is till the value of n is reached. Now if you observe the pattern further if you subtract 2 from 6 you get 4that is the number you want in your sequence. Similarly when you subtract 4 from 7 you get 3which is the next number in the sequence you required.
现在查看这两个序列,您可以看到序列在中点之前都是相同的,直到达到 n 的值。现在,如果你进一步观察这个模式,如果你从 6 中减去2,你会得到 4,这就是你想要的序列中的数字。同样,当您从 7 中减去 4 时,您会得到 3,这是您需要的序列中的下一个数字。
Hence the pattern this sequence follows is that after the loop reaches the value provided by the user you need to subtract (2 * k) from the next number where k starts from 1 and increases with every iteration
因此,此序列遵循的模式是,在循环达到用户提供的值后,您需要从下一个数字中减去 (2 * k),其中k 从 1 开始并随着每次迭代而增加
Now you know how to achieve the pattern which would be easy to achieve using conditional statements.
现在您知道如何实现使用条件语句很容易实现的模式。
PS: let's assume an added constraint of using no conditional statements then we have to write an arithmetic expression to solve our problem.
PS:让我们假设不使用条件语句的附加约束,那么我们必须编写一个算术表达式来解决我们的问题。
Following the pattern again the expression must display iwhere i is the variable incremented in the loop
再次遵循模式,表达式必须显示i,其中 i 是在循环中递增的变量
so our code looks like
所以我们的代码看起来像
for (i = 1; i<=2*n - 1;i++)
{
System.out.print(i);
}
Now to get the pattern we need to subtract multiples of 2 after the user provided integer nis reached. But whatever we subtract should also not affect out first n integers.
现在要获得模式,我们需要在达到用户提供的整数n后减去 2 的倍数。但是无论我们减去什么,也不应该影响前 n 个整数。
Since we know we have to subtract multiples of 2 we know the expression we have to subtract would look like 2 * (____). As we want a sequence of multiples we can obtain that using %. As soon as the number goes over n the %operator on i would give us back sequence from 0 to n-1 hence generating multiples of 2.
因为我们知道我们必须减去 2 的倍数,所以我们知道我们必须减去的表达式看起来像2 * (____)。因为我们想要一个倍数序列,我们可以使用%获得它。一旦数字超过 n ,i 上的%运算符就会返回从 0 到 n-1 的序列,从而生成 2 的倍数。
Now our expression comes to 2 * (i % n). But the problem is that it would also subtract from the first 4 integers which we don't want so we have to make changes such that this expression will work only after loop reaches the value provided by the user.
现在我们的表达式变成了2 * (i % n)。但问题是它还会从我们不想要的前 4 个整数中减去,因此我们必须进行更改,以便此表达式仅在循环达到用户提供的值后才能工作。
As we know the division /operator provides us with the quotient. Hence it would yield us 0 till we reach the value of user defined number and 1 for the rest of the sequence as we run our loop till 2*n -1. Hence multiplying this expression to our previous expression yields 2*(i%n)*(i/n)
众所周知,除法/运算符为我们提供了商。因此,当我们运行循环直到 2*n -1 时,它会产生 0 直到我们达到用户定义的数字的值和序列的其余部分的 1。因此,将这个表达式与我们之前的表达式相乘产生2*(i%n)*(i/n)
And there we have it our final code to generate the sequence would be
我们有了它生成序列的最终代码是
for (int i = 1;i<2*r;i++)
{
System.out.print(i - 2 * (i%r)*(i/r));
}
Observe the above code for the first n-1 integers i/r would make subtracted expression 0 and for i = n, i % rwould make the expression 0. For the rest of the sequence i / rwould generate value 1 and hence we will get multiples of 2 from 2 *( i % r)to provide us with the sequence
观察上面的代码,对于前 n-1 个整数 i/r 会使表达式减去 0,对于 i = n,i % r会使表达式为 0。对于序列的其余部分,i / r将生成值 1,因此我们将从2 *( i % r)得到 2 的倍数来为我们提供序列