因此,我需要訪問我在最後一條if語句中的if語句中分配的變量。我是一個初學者在Java和任何幫助表示讚賞:)如何在同一級別if語句中分配/訪問變量
這是我的編譯錯誤。
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The local variable a may not have been initialized
The local variable b may not have been initialized
The local variable c may not have been initialized
The local variable d may not have been initialized
The local variable e may not have been initialized
這是我的代碼。我需要根據迭代的次數分配一個特定的變量,然後在最後一次迭代分配變量後訪問這些變量。
public static void main(String[] args){
String a;
String b;
String c;
String d;
String e;
Scanner s = new Scanner(System.in);
for(int loop = 1; loop <= 6; loop++){
if(loop == 1){
System.out.println("Please input a grade.");
a = s.nextLine();
System.out.println("You said,"+a);
continue;
}
if(loop == 2){
System.out.println("Please input a grade.");;
b = s.nextLine();
System.out.println("You said,"+b);
continue;
}
if(loop == 3){
System.out.println("Please input a grade.");
c = s.nextLine();
System.out.println("You said," +c);
continue;
}
if(loop == 4){
System.out.println("Please input a grade.");
d = s.nextLine();
System.out.println("You said," +d);
continue;
}
if(loop == 5){
System.out.println("Please input a grade.");
e = s.nextLine();
System.out.println("You said," +e);
continue;
}
if(loop == 6){
System.out.println("Grade 1."+a);
System.out.println("Grade 1."+b);
System.out.println("Grade 1."+c);
System.out.println("Grade 1."+d);
System.out.println("Grade 1."+e);
}
}
}
'String a =「」;'應該這樣做。 – pzaenger