2012-09-20 64 views
-1

這裏的問題是,我不知道在if-else塊中使用變量wage之前將它定義爲字段變量的位置,因此它可以被Eclipse識別和使用。如何讓這個變量成爲全局變量?

下面的代碼會給我錯誤代碼的最後一行:wage無法解析爲變量。但是當我把它放在Scanner控制檯線下的另一行代碼中(從頂部向下4行)時,它會在所有代碼行中出現錯誤,並在其下面顯示變量wage,並顯示「重複的局部變量」不知道把它放在哪裏使它成爲一個字段變量。任何想法的人?

import java.util.Scanner; 

public class Java3 { 
    public static void main(String[] args) { 
     Scanner console = new Scanner(System.in); 
     System.out.println("*** Basic Wage Calculator ***"); 
     System.out.printf("%n"); 
     System.out.println("Enter start time in 24:00 format"); 
     String startTime = console.nextLine(); 
     String[] tokens = startTime.split(":"); 
     double starttimeHours = Double.parseDouble(tokens[0]); 
     double startMinutes = Double.parseDouble(tokens[1]); 
     if (starttimeHours >= 6 && starttimeHours <= 8 
       || starttimeHours >= 9 && starttimeHours <= 19) { 
      double wage = 1.6; 
     } else if (starttimeHours >= 9 && starttimeHours >= 10 && startMinutes >= 01) { 
      double wage = 43; 
     } else { 
      double wage = 987; 
     } 
     System.out.println(wage); 
    } 
} 
+1

你知道一個字段是什麼嗎? –

回答

1

當你寫雙wage = 1.6;要定義的if報表範圍的變量。這意味着在if語句結束}之後,我們無法訪問該變量。定義if語句之外的工資變量。你在哪裏定義了startMinutes。而不是指定

`double wage=1.6;` 

更改爲

`wage=1.6;` 
+0

是的,謝謝你我已經回覆了你的雙重和雙重工資點。我不知道它是這樣工作的。謝謝 – user1319219

0

的需要,因爲您是通過System.out.println(wage)訪問其移動wageifelse的範圍。

import java.util.Scanner; 

public class Java3 { 
    public static void main(String[] args) { 
     Scanner console = new Scanner(System.in); 
     System.out.println("*** Basic Wage Calculator ***"); 
     System.out.printf("%n"); 
     System.out.println("Enter start time in 24:00 format"); 
     String startTime = console.nextLine(); 
     String[] tokens = startTime.split(":"); 
     double starttimeHours = Double.parseDouble(tokens[0]); 
     double startMinutes = Double.parseDouble(tokens[1]); 
     double wage = 0; 
     if (starttimeHours >= 6 && starttimeHours <= 8 
       || starttimeHours >= 9 && starttimeHours <= 19) { 
      wage = 1.6; 
     } else if (starttimeHours >= 9 && starttimeHours >= 10 && startMinutes >= 01) { 
      wage = 43; 
     } else { 
      wage = 987; 
     } 
     System.out.println(wage); 
    } 
} 
+0

好,非常感謝你的代碼工作正常。我注意到你將範圍內的代碼改爲工資,而不是雙重工資。所以它必須已經是雙重工資了(正如你在幾分鐘之內定義的那樣)並且不需要再被定義爲雙倍工資呢?所以如果我把它稱爲雙重工資在if塊內,它實際上會注意到if塊之外的變量呢?有意思......謝謝 – user1319219

0

變量可用/可用於定義其範圍({})的範圍中。如果你需要增加變量的作用域你應該把它移動{}之外,因此您可以通過以下方式做到這一點:

double wage = 0.00; // Moved wage outside the if-scope 
if (starttimeHours >= 6 && starttimeHours <= 8 
      || starttimeHours >= 9 && starttimeHours <= 19) { 
    wage = 1.6; 
} else if (starttimeHours >= 9 && starttimeHours >= 10 && startMinutes >= 01) { 
    wage = 43; 
} else { 
    wage = 987; 
} 
0

的問題是範圍之一。有三種不同類型的示波器:迴路,方法/塊。這些列表從最不可見到最可見。

你可以認爲它有點像一個Matryoshka娃娃 - 最小的娃娃是循環的可見性,包含它們的一切都是類可見度。更好地說明,它看起來像這樣:

(Class Level 
    (Method/Block Level 
     (Loop Level) 
    ) 
) 

您可以在某種程度上混合使用這些範圍,但適用相同的一般範圍規則。

這與您的問題有何關係?正如我所說,你的變量聲明的問題是範圍之一。將變量wage移動到適當的作用域級別,並僅將其實例化一次


變量在一個循環的範圍:在報頭或所述循環體聲明的任何變量是排他性到環路;試圖訪問它們將導致編譯錯誤。

for(int i = 0; i < 100; i++) { 
    Double aValue = new Double(10.0); 
    System.out.println(i); 
} 
aValue = 6; // What's this variable and why is it out here? 
i = 0; // Same as above 

變量在方法(或塊)的範圍:的方法中的主體或在代碼塊中聲明的任何變量(if語句包括)是互斥到該塊。該塊內部的循環和if語句可以操縱變量,但不能聲明和實例化它們自己的內容,以便在較高的作用域級別上看到。在整個類的範圍

public static void main(String[] args) { 
    // args is a parameter that is also available in this scope! 
    // the next two variables can be used anywhere in main 
    // including the if-else and loops 

    String name = "Makoto"; 
    Integer numPhones = new Integer(1); 

    if(name.length() == 6) { 
     String newWord = "Wow!"; //can't be used anywhere but here 
     System.out.println(name + " " + newWord); 
    } else { 
     String oldWord = "bummer."; //can't be used anywhere but here 
     System.out.println(name + " " + oldWord); 
    } 

    for(int i = 0; i < args.length; i++) { 
     String word = "hello"; // can't be used in main() 
     System.out.println(word + args[i]); 
    } 
} 

變量(通常被稱爲場變量):這些是可以由類中使用的變量,無論它是在什麼方法的唯一例外是。靜態方法;如果您正在引用一個靜態字段,則它必須從靜態方法完成必須

public class Dummy { 
    private int weight; 
    private int height; 
    private int age; 
    private int shape; 
    private String name; 
    private static boolean isSmart; 

    public String getName() { 
     return name; 
    } 

    public int getHeight() { 
     return height; 
    } 

    public int getAge() { 
     return age; 
    } 

    public int getShape() { 
     return shape; 
    } 

    public static boolean getIsSmart() { 
     return isSmart; 
    } 
}