我想讓一個程序根據小時變量將字符串s1等於某些文本。問題是當我運行程序s1沒有找到。我剛開始使用Java,所以我不確定這是否真的效率低下,或者如果它簡單,我錯過了。變量沒有被if語句定義
代碼:
public class Main {
public static void main(String[] args) {
// String Change Test
int[] arr;
arr = new int[2];
arr[0] = 1;
boolean b1 = arr[0] > 1;
boolean b2 = arr[0] < 1;
boolean b4 = 0 > arr[0];
boolean b3 = b4 && b2;
boolean b5 = b1 || b3;
if (b5) {
String s1 = "You have played for " + arr[0] + " hours!";
}
else if (arr[0] == 1) {
String s1 = "You have played for 1 hour!";
}
else if (arr[0] == 5) {
String s1 = "You have not played at all!";
}
else {
String s1 = "Memory Error in arr[0], Are the hours negative? Is it there?";
}
System.out.print (s1);
}
}
您需要聲明的條件範圍之外S1。 –
請首先在外部聲明變量if語句並在任何地方使用相同的變量。請閱讀並理解變量的範圍。 –