2015-07-02 46 views
0

我對Java有點新,最近我學到了很多方法(太酷了!)。我想知道是否有可能在我的主要方法中聲明一個變量並將其用於其他方法。讓一個變量可以通過各種方法訪問

我想要做的是創建一個計算器使用方法(只是爲了實踐這個新的概念),但我不想每次在每個方法中聲明變量。

這裏是代碼的骨骼結構:

class GS1{ 



public static void main (String[]args){ 
    Scanner input = new Scanner(System.in); 
    System.out.println("Enter the math operation to be completed: "); 
    String opt = input.nextLine(); 
    int x,y; // I tried declaring variables here 
    switch(opt){ 

    case "addition" : 
    // addition method goes here 
    break; 
    case "subtraction": 
    //subtraction method goes here 
    break; 
    case "multiplication": 
    //multiplication method goes here 
    break; 
    case "division": 
    //division method goes here 
    break; 
    } 

} 

static void addition(){ 
    System.out.println("Enter first value for addition"); 
    x=input.nextint(); // i get error stating both "x" and "input" cannot be resolved as a variable 


} 

static void subtration(){ 


} 

static void Multiplication(){ 

} 

static void Division(){ 



} 

}

+1

您應該將值作爲*參數*傳遞給方法。然後這些方法應該*返回結果*。 – RealSkeptic

+1

建議接下來學什麼:嘗試創建一個計算器類,因爲使用'全局變量'當然不是好的做法 – Marged

回答

5

你應該把變量的所有方法之外,但在類中,創建全局訪問。

public class ClassName 
{ 
    public int x; 
    public int y; 

    public void method1() 
    { 
     x = 3; 
    } 

    public void method2() 
    { 
     y = 1; 
    } 
} 
+1

但原始帖子有靜態方法。 –

+0

@faheemfarhan這是一個通用的例子。這是無關緊要的。 –

+1

我認爲最好還是在問題的背景下回答。 –

3

在類級別移動變量,使其成爲類中的字段。

由於您正在學習,最好不要使用static字段,也不要使用除main方法外的方法。

1

整理好你的代碼,使類似下面的代碼:

class Operation { 

    public double addition(double... value) { 
     double result = 0; 
     for (double i : value) { 
      result += i; 
     } 
     return result; 
    } 

    public double subtration(.....) { 
     // ......................... 
     return 0.0; 
    } 

    public double multiplication(.....) { 
     // ......................... 
     return 0.0; 
    } 

    public double division(.....) { 
     // ......................... 
     return 0.0; 
    } 
} 

public class GS1 { 

    public static void main(String[] args) { 
     Operation operations=new Operation(); 


     //read value code 
     double result=operations.addition(value); 

     //print result code 

    } 
} 
1

你需要的東西是這樣的:

class GS1 { 

     public static int x; 
     public static Scanner input; 

     public static void main(String[] args) { 
      input = new Scanner(System.in); 
      System.out.println("Enter the math operation to be completed: "); 
      String opt = input.nextLine(); 
      int x, y; // I tried declaring variables here 
      switch(opt){ 

      case "addition" : 
      // addition method goes here 
      break; 
      case "subtraction": 
      //subtraction method goes here 
      break; 
      case "multiplication": 
      //multiplication method goes here 
      break; 
      case "division": 
      //division method goes here 
      break; 
      } 
     } 

     static void addition() { 
      System.out.println("Enter first value for addition"); 
      x=input.nextint(); // i get error stating both "x" and "input" cannot 
      // be resolved as a variable 

     } 

     static void subtration() { 

     } 

     static void Multiplication() { 

     } 

     static void Division() { 

     } 

    } 

請記住,在你的字段聲明使用「靜態」修飾符(x和輸入),您無法對非靜態字段進行靜態引用。

更好的方法是使用對象而不是將所有的方法放在一個類中(GS1)。例如,在您的評論中創建一個類似Marged的Calculator類

相關問題