奇怪的字符串错误,java 的新手,请谅解
Wierd String error, please be gracious, new to java
所以我的问题是它一直说我已经在顶部定义了 String spaces
但是如果我把循环外的定义拿走并尝试只在循环中定义 spaces
,它告诉我它还没有被实例化...
public class NestedLoop {
public static void main (String [] args) {
int userNum = 0;
int i = 0;
int j = 1;
String spaces = "";
Scanner scnr = new Scanner(System.in);
System.out.print("Enter userNum:");
userNum = scnr.nextInt();
for (i = 0; i <= userNum; i++) {
while (j == i){
spaces = spaces.concat(" ");
System.out.print(spaces);
j++;}
System.out.println(i);}
错误:main.java:244: 空格已在 main(java.lang.String[])
中定义
我只是 运行 这段代码在我的 IDE 中并且没有错误,编译时间或 运行 时间。不确定我能否重现您报告的错误。我唯一能想到的可能性是您的 JDK 出错,但同时我不知道如何或为什么会发生这种情况。
所以我的问题是它一直说我已经在顶部定义了 String spaces
但是如果我把循环外的定义拿走并尝试只在循环中定义 spaces
,它告诉我它还没有被实例化...
public class NestedLoop {
public static void main (String [] args) {
int userNum = 0;
int i = 0;
int j = 1;
String spaces = "";
Scanner scnr = new Scanner(System.in);
System.out.print("Enter userNum:");
userNum = scnr.nextInt();
for (i = 0; i <= userNum; i++) {
while (j == i){
spaces = spaces.concat(" ");
System.out.print(spaces);
j++;}
System.out.println(i);}
错误:main.java:244: 空格已在 main(java.lang.String[])
中定义我只是 运行 这段代码在我的 IDE 中并且没有错误,编译时间或 运行 时间。不确定我能否重现您报告的错误。我唯一能想到的可能性是您的 JDK 出错,但同时我不知道如何或为什么会发生这种情况。