2015-10-26 58 views
0

我希望能夠從一個實例方法內的scanner類獲取輸入,並將其傳遞給另一個實例方法,我將在其中顯示它。如何將輸入從一個實例方法中的scanner類移動到另一個實例方法

這裏是我的問題

import java.io.*; 
    import java.util.*; 

    class TestScanner 
    { 
     public static void main (String args[]) 
     { 
      TestScanner disInput = new TestScanner(); 
      TestScanner gInput = new TestScanner(); 
      gInput.grabInput(); 
      disInput.displayInput(); 


     } 

     void displayInput() 
     { 
      System.out.println("Scanner Input here: "); 
      System.out.println("What is my age: "); 
      // How do I get age here 
      System.out.println("What is 2 +2: "); 
      // How do I get math here 
     } 

     void grabInput() 
     { 
     int age, math; 
     Scanner stdin = new Scanner(System.in); 

     System.out.println("What is my age: "); 
     age = stdin.nextInt(); 
     System.out.println("What is 2 +2: "); 
     math = stdin.nextInt(); 

     } 
    } 
+1

檢查[將信息傳遞給方法或構造函數](https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html)。另外,檢查[爲什麼靜態變量被認爲是邪惡的?](http://stackoverflow.com/questions/7026507/why-are-static-variables-considered-evil)如果有人發佈一個答案,使用靜態 – sam

+0

@sam有人回答使用'static'爲*解決方案* –

+0

@LuiggiMendoza好了,我的努力張貼鏈接已見成效:)使用'static'領域的解決方案 – sam

回答

-1

你可以使你的TestScanner類的靜態變量的例子,你的類的每個實例將有機會獲得該變量。請注意,該變量對於您班級的每個實例都具有相同的值。

import java.io.*; 
import java.util.*; 

class TestScanner 
{ 
    public static Integer age; 
    public static Integer math; 
    public static void main (String args[]) 
    { 
     TestScanner disInput = new TestScanner(); 
     TestScanner gInput = new TestScanner(); 
     gInput.grabInput(); 
     disInput.displayInput(); 


    } 

    void displayInput() 
    { 
     System.out.println("Scanner Input here: "); 
     System.out.println("What is my age: "); 
     // How do I get age here 
     System.out.pritln(age); 
     System.out.println("What is 2 +2: "); 
     // How do I get math here 
     System.out.pritln(math); 
    } 

    void grabInput() 
    { 
    Scanner stdin = new Scanner(System.in); 

    System.out.println("What is my age: "); 
    age = stdin.nextInt(); 
    System.out.println("What is 2 +2: "); 
    math = stdin.nextInt(); 

    } 
} 

你可以看看java here中的靜態變量是什麼。

+0

避免。 –

+0

這個工程謝謝:) – Jets

+0

如果你不想使用靜態字段,你可以查看[Singleton design pattern](http://www.tutorialspoint.com/design_pattern/singleton_pattern.htm) – Theo

0

BAD避免同時LEARNING解決方案:移動agemath局部變量在類static領域。

的類將是這樣的:

public class TestScanner { 
    static int age; 
    static int math; 
    //rest of your class... 
    //in method grabInput 
    static void grabInput() { 
     //comment the variables 
     //int age, math; 
    } 
} 

上面的代碼將工作,由於static領域的使用。首先了解what static meansbe careful when using it


你應該問自己以下問題:

  • 我應該有顯示同一類中的TestScanner領域的方法?
  • 我應該創建一個新的TestScanner實例來顯示另一個TestScanner實例的值嗎?

如果您對兩個問題都回答「是」,那麼您可以創建一個方法來接收TestScanner的實例並打印其內容。這是你的類將如何看起來像:

public class TestScanner { 
    //Note: they're not static anymore (yay!) 
    int age; 
    int math; 

    public static void main (String args[]) { 
     TestScanner disInput = new TestScanner(); 
     TestScanner gInput = new TestScanner(); 
     gInput.grabInput(); 
     //note that now we need to pass the instance of TestScanner 
     //in order to print its contents 
     disInput.displayInput(gInput); 
    } 

    //method for display purposes 
    void displayInput(TestScanner testScanner) { 
     System.out.println("Scanner Input here: "); 
     System.out.println("What is my age: "); 
     System.out.println(testScanner.age); 
     System.out.println("What is 2 +2: "); 
     System.out.println(testScanner.math); 
    } 

    void grabInput() { 
     Scanner stdin = new Scanner(System.in); 
     System.out.println("What is my age: "); 
     this.age = stdin.nextInt(); 
     System.out.println("What is 2 +2: "); 
     this.math = stdin.nextInt(); 
    } 
} 

現在問自己:我需要這一切?這是一個很好的設計嗎?我是否可以輕鬆執行更改,或者是否需要更多時間和精力來添加新變量,填充並顯示它?

這裏主要的問題叫做cohesion。這是一個班級的功能程度。正如你所看到的,TestScanner本身有許多責任:

  • 商店agemath
  • 從用戶輸入讀吧
  • 顯示的數據傳送給用戶

更好的方法將是在兩類分離這些功能:

  1. Person(或其他名稱)將存儲來自用戶輸入的數據。基於agemath字段的名稱,我想這個類的一個適當的名稱可能是Person。這門課將負責存儲數據並提供不同的用途,例如存儲用戶輸入,然後將其用於顯示目的。
  2. TestScanner類,其中將測試的Scanner使用以從用戶輸入和顯示數據中的數據讀出到用戶。

的設計看起來就像這樣:

class Person { 
    private int age; 
    private int math; 
    public Person(int age, int math) { 
     this.age = age; 
     this.math = math; 
    } 
    public int getAge() { 
     return this.age; 
    } 
    public int getMath() { 
     return this.math; 
    } 
    //setters avoided for this case 
} 

class TestScanner { 
    Scanner scanner; 
    public TestScanner(Scanner scanner) { 
     this.scanner = scanner; 
    } 
    public Person grabInput() { 
     System.out.println("What is my age: "); 
     int age = stdin.nextInt(); 
     System.out.println("What is 2 +2: "); 
     int math = stdin.nextInt(); 
     return new Person(age, math); 
    } 
    public void displayInput(Person person) { 
     System.out.print("My age: "); 
     System.out.println(person.getAge()); 
     System.out.print("My value of 2 +2: "); 
     System.out.println(person.getMath()); 
    } 
    public static void main(String[] args) { 
     TestScanner testScanner = new TestScanner(new Scanner(System.in)); 
     Person person = testScanner.grabInput(); 
     testScanner.displayInput(person); 
    } 
} 

注意:問題而提供的鏈接是不是用來裝飾的目的。

+0

如果使變量靜態的,那麼'grabInput()'也需要是靜態的。 – Rainbolt

+0

@Rainbolt沒有必要。您可以訪問來自非靜態方法的'static'字段。這是一個糟糕的做法,但它的工作原理。我正在編輯。 –

+0

對不起,我自己填補了空白('//你的班級其他......'),並認定它不起作用,但僅僅是因爲我實施了錯誤。我明白現在它是如何工作的。我反對建議創建一個Person類,因爲OP似乎還不知道類和實例。我假設他仍然在學習方法和輸入/輸出,並試圖避免創建任何事物(除掃描器外),我寫了答案。 – Rainbolt

0

你只需要一臺掃描儀以獲取輸入,你需要關閉掃描儀,當你完成:

int grabInput(String question) 
{ 
    // Display a question to user 
    System.out.println(question); 

    // Read a response from user 
    Scanner stdin = new Scanner(System.in); 
    int input = stdin.nextInt(); 
    stdin.close(); 

    // Return the response 
    return input; 
} 

你不需要任何掃描儀在所有顯示輸出:

int displayOutput(String message, int response) 
{ 
    System.out.println(message); 
    System.out.println(response); 
} 

這裏是你的主要看起來像這些變化:

public static void main (String args[]) 
{ 
    String ageQuestion = "What is my age?"; 
    int ageAnswer = grabInput(ageQuestion); 

    String mathQuestion = "What is 2 + 2?"; 
    int mathAnswer = grabInput(mathQuestion); 

    displayOutput(ageQuestion, ageAnswer); 
    displayOutput(mathQuestion, mathAnswer); 
} 

關鍵是要返回主響應因爲你需要訪問他們在其他功能,並埋葬掃描邏輯功能於一體,使您不必擔心其他地方。

相關問題