嵌套 For 循环解释
Nested For-Loop Explanation
代码写在java
我试图在同一行打印用户输入的“*”数量。
这是我的资料:
if (((input / 2) + 1) == ir) {
for (int ij = 1 ; ij <= input; ij++) {
System.out.print("*");
}
}
if 语句正在测试我们是否处于我要制作的形状(领结)的中间点。
我觉得我的逻辑和代码是正确的,但是对于输入 5,
这一行看起来像这样:***
而不是:*****
有人可以向我解释为什么会这样吗?
完整代码如下:
import java.util.Scanner;
public class BowTie {
public static void main(String [] args) {
Scanner scnr = new Scanner(System.in);
int input = scnr.nextInt();
int stars = 1;
int spaces = input - 2;
if ((input % 2 == 1) && (input >= 1)) {
for (int ir = 1; ir <= input; ir++) {
for (int ic = 1; ic <= stars; ic++) {
System.out.print("*");
}
for (int ic = 1; ic <= spaces; ic++) {
if (((input / 2) + 1) == ir) {
for (int ij = 1; ij <= input; ij++) {
System.out.print("*");
}
} else {
System.out.print(" ");
}
}
if (((input + 1) / 2) != ir) {
for (int ic = 1; ic <= stars; ic++) {
System.out.print("*");
}
}
if ((input / 2) < ir) {
stars--;
spaces += 2;
} else {
stars++;
spaces -= 2;
}
System.out.println();
}
} else {
return;
}
scnr.close();
}
}
这是因为您使用的支票。
简而言之,检查这个修改后的脚本:
import java.util.Scanner;
public class BowTie {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int input = scnr.nextInt();
int stars = 1;
int spaces = input - 2;
if ((input % 2 == 1) && (input >= 1)) {
for (int ir = 1; ir <= input; ir++) {
if (((input + 1) / 2) != ir) { // add this check
for (int ic = 1; ic <= stars; ic++) {
System.out.print("*");
}
}
// change this logic: ---
if (((input + 1) / 2) == ir) {
for(int ic = 1; ic <= input; ic++) {
System.out.print("*");
}
} else {
for (int ic = 1; ic <= spaces; ic++) {
System.out.print(" ");
}
}
// ---
if (((input + 1) / 2) != ir) {
for (int ic = 1; ic <= stars; ic++) {
System.out.print("*");
}
}
if ((input / 2) < ir) {
stars--;
spaces += 2;
} else {
stars++;
spaces -= 2;
}
System.out.println();
}
}
scnr.close();
}
}
(注释行已更改)
for (int ir = 1; ir <= input; ir++)
中的第一个 for
正在打印您看到的星星。
此外,中间部分已更改为打印整行或空格(就像您所做的那样)。
您缺少 if (((input + 1) / 2) != ir)
上的 else 条件,如下所示:
import java.util.Scanner;
public class BowTie {
public static void main(String [] args) {
Scanner scnr = new Scanner(System.in);
int input = scnr.nextInt();
int stars = 1;
int spaces = input - 2;
if ((input % 2 == 1) && (input >= 1)) {
for (int ir = 1; ir <= input; ir++) {
for (int ic = 1; ic <= stars; ic++) {
System.out.print("*");
}
for (int ic = 1; ic <= spaces; ic++) {
if (((input / 2) + 1) == ir) {
for (int ij = 1; ij <= input; ij++) {
System.out.print("*");
}
} else {
System.out.print(" ");
}
}
if (((input + 1) / 2) != ir) {
for (int ic = 1; ic <= stars; ic++) {
System.out.print("*");
}
// Added else
} else {
for (int ic = 1; ic <= (input - ir); ic++) {
System.out.print("*");
}
}
if ((input / 2) < ir) {
stars--;
spaces += 2;
} else {
stars++;
spaces -= 2;
}
System.out.println();
}
} else {
return;
}
scnr.close();
}
}
不过,您的代码有点难以阅读和理解。您可能需要稍微重构一下。
代码写在java
我试图在同一行打印用户输入的“*”数量。
这是我的资料:
if (((input / 2) + 1) == ir) {
for (int ij = 1 ; ij <= input; ij++) {
System.out.print("*");
}
}
if 语句正在测试我们是否处于我要制作的形状(领结)的中间点。
我觉得我的逻辑和代码是正确的,但是对于输入 5,
这一行看起来像这样:***
而不是:*****
有人可以向我解释为什么会这样吗?
完整代码如下:
import java.util.Scanner;
public class BowTie {
public static void main(String [] args) {
Scanner scnr = new Scanner(System.in);
int input = scnr.nextInt();
int stars = 1;
int spaces = input - 2;
if ((input % 2 == 1) && (input >= 1)) {
for (int ir = 1; ir <= input; ir++) {
for (int ic = 1; ic <= stars; ic++) {
System.out.print("*");
}
for (int ic = 1; ic <= spaces; ic++) {
if (((input / 2) + 1) == ir) {
for (int ij = 1; ij <= input; ij++) {
System.out.print("*");
}
} else {
System.out.print(" ");
}
}
if (((input + 1) / 2) != ir) {
for (int ic = 1; ic <= stars; ic++) {
System.out.print("*");
}
}
if ((input / 2) < ir) {
stars--;
spaces += 2;
} else {
stars++;
spaces -= 2;
}
System.out.println();
}
} else {
return;
}
scnr.close();
}
}
这是因为您使用的支票。 简而言之,检查这个修改后的脚本:
import java.util.Scanner;
public class BowTie {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int input = scnr.nextInt();
int stars = 1;
int spaces = input - 2;
if ((input % 2 == 1) && (input >= 1)) {
for (int ir = 1; ir <= input; ir++) {
if (((input + 1) / 2) != ir) { // add this check
for (int ic = 1; ic <= stars; ic++) {
System.out.print("*");
}
}
// change this logic: ---
if (((input + 1) / 2) == ir) {
for(int ic = 1; ic <= input; ic++) {
System.out.print("*");
}
} else {
for (int ic = 1; ic <= spaces; ic++) {
System.out.print(" ");
}
}
// ---
if (((input + 1) / 2) != ir) {
for (int ic = 1; ic <= stars; ic++) {
System.out.print("*");
}
}
if ((input / 2) < ir) {
stars--;
spaces += 2;
} else {
stars++;
spaces -= 2;
}
System.out.println();
}
}
scnr.close();
}
}
(注释行已更改)
for (int ir = 1; ir <= input; ir++)
中的第一个 for
正在打印您看到的星星。
此外,中间部分已更改为打印整行或空格(就像您所做的那样)。
您缺少 if (((input + 1) / 2) != ir)
上的 else 条件,如下所示:
import java.util.Scanner;
public class BowTie {
public static void main(String [] args) {
Scanner scnr = new Scanner(System.in);
int input = scnr.nextInt();
int stars = 1;
int spaces = input - 2;
if ((input % 2 == 1) && (input >= 1)) {
for (int ir = 1; ir <= input; ir++) {
for (int ic = 1; ic <= stars; ic++) {
System.out.print("*");
}
for (int ic = 1; ic <= spaces; ic++) {
if (((input / 2) + 1) == ir) {
for (int ij = 1; ij <= input; ij++) {
System.out.print("*");
}
} else {
System.out.print(" ");
}
}
if (((input + 1) / 2) != ir) {
for (int ic = 1; ic <= stars; ic++) {
System.out.print("*");
}
// Added else
} else {
for (int ic = 1; ic <= (input - ir); ic++) {
System.out.print("*");
}
}
if ((input / 2) < ir) {
stars--;
spaces += 2;
} else {
stars++;
spaces -= 2;
}
System.out.println();
}
} else {
return;
}
scnr.close();
}
}
不过,您的代码有点难以阅读和理解。您可能需要稍微重构一下。