[英]Loop inside Loop in Java (simple)
I am doing an exercise where I have to print 'x' (is an input) rows of numbers incrementing from 0 to 10. If I input 3, the output should look like this我正在做一个练习,我必须打印从 0 到 10 递增的数字行“x”(是输入)。如果我输入 3,则 output 应该如下所示
012
012
345345
678678
012
012
345345
678678
012
012
345345
678678
but instead, I get 3 rows of a 0 to 10 count.但相反,我得到 3 行的 0 到 10 计数。
I know it might be easy to code, but I am stuck in that!我知道它可能很容易编码,但我被困在其中!
I think I am not undestanding well the nested loops:(我想我并没有很好地理解嵌套循环:(
public class quadrats {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int q = in.nextInt();
for (int j = 0; j < q; j++) {
for (int i = 0; i <= 10; i++) {
System.out.print(i);
}
System.out.println();
}
}
}
You don't need two loops for this.为此,您不需要两个循环。 All you need is to print a newline after every 3rd letter and an extra newline after every 3rd line.
您只需要在每 3 个字母后打印一个换行符,并在每 3 行后打印一个额外的换行符。 Your code can be like:
您的代码可以是:
public class quadrats {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int q = in.nextInt();
int lines = 0;
int letters = 0;
while (lines < q) {
System.out.print(i);
if (letters && letters % q == 0) {
System.out.println();
lines++;
}
if (lines && lines % q == 0) {
System.out.println();
letters = 0;
continue;
}
letters++;
}
}
PS: I haven't tried this code myself. PS:我自己没有尝试过这段代码。 But concept would be the same.
但概念是一样的。
You can try with below code您可以尝试使用以下代码
public class quadrats {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int q = in.nextInt();
for (int j = 0; j < q; j++) {
for (int i = 0; i < 9; i++) {
if(i%3 == 0)
System.out.println();
System.out.print(i);
}
System.out.println();
}
}
}
The answer above should solve your problem so I will try to explain what your code does.上面的答案应该可以解决您的问题,因此我将尝试解释您的代码的作用。
Let's start with code inside first for loop:让我们从第一个 for 循环中的代码开始:
for (int i = 0; i <= 10; i++) {
System.out.print(i);
}
System.out.println();
First we have a loop iterating through numbers from 0 to 10 and the output is:首先,我们有一个循环遍历从 0 到 10 的数字,output 是:
012345678910
012345678910
and a new line after that.以及之后的新行。
That means that output of your program will print above mentioned output q times.这意味着您的程序的 output 将打印上述 output q次。
012345678910
012345678910
012345678910
012345678910
012345678910
012345678910
Print X quandrants of X rows and X columns each分别打印 X 行和 X 列的 X 个象限
Scanner in = new Scanner(System.in);
int q = in.nextInt();
// q quadrants
for (int iQuadrat = 0; iQuadrat < q; iQuadrat++) {
// count will keep track of the last number you print
int count = 0;
// q rows
for (int iRow = 0; iRow < q; iRow++) {
// q cols
for (int iCol = 0; iCol < q; iCol++) {
System.out.print(count);
// increment the count and take its modulo 10 so it stays between 0 and 9
count = (count+1)%10;
}
// line return at the end of the row
System.out.println();
}
// line return between quadrants
System.out.println();
}
For an input of 12, it will print 12 times this quadrant对于 12 的输入,它将打印该象限的 12 次
012345678901
234567890123
456789012345
678901234567
890123456789
012345678901
234567890123
456789012345
678901234567
890123456789
012345678901
234567890123
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.