2016-12-07 30 views
-3

我做了一堆方法和遇到的問題,這部分一個方法:JAVA - 使讀取列表

「測試你的方法,程序和包括讀取列表的方法,通過終止-999,放入數組中。「

我不知道該做什麼,希望有人能告訴我,並解釋我可以如何做到這一點。這是我的代碼:

(注:顯示「4端接方法」部分是空的評論這就是我想要做的代碼)

import java.util.Scanner; 
public class Problem3 { 
    //1-MAXIMUM METHOD// 
    public static int max(int[] arr) 
    { 
     int tmpMax = arr[0]; 
      for(int i = 1; i < arr.length; i++) 
      { 
       if(arr[i] > tmpMax) 
       { 
        tmpMax = arr[i]; 
       } 
      } 
      return tmpMax; 
    } 
    //2-MINIMUM METHOD// 
    public static int min(int[] arr) 
    { 
     int tmpMin = arr[0]; 
      for(int i = 1; i < arr.length; i++) 
      { 
       if(arr[i] < tmpMin) 
       { 
        tmpMin = arr[i]; 
       } 
      } 
      return tmpMin; 
    } 
    //3-MIN-MAX METHOD// 
    public static int[] maxMin(int[] arr) 
    { 
     int[] myArray = new int [2]; 
     myArray[0] = min(arr); 
     myArray[1] = max(arr); 
     return myArray; 
    } 
    //4-TERMINATION METHOD// 
    public static int termination(int[] arr) 
    { 

    } 
    //5-MAIN-METHOD// 
    public static void main(String[] args) 
    { 
     Scanner input = new Scanner(System.in); 
     int a, b, c, d, e; 
     System.out.println("Input the Numbers: "); 
     a = input.nextInt(); 
     b = input.nextInt(); 
     c = input.nextInt(); 
     d = input.nextInt(); 
     e = input.nextInt(); 
     int[] test = {a, b, c, d, e}; 
     System.out.println("The Maximum Number is: " + min(test)); 
     System.out.println("The Minimum Number is: " + max(test)); 
     int [] x = maxMin(test); 
     System.out.println("Min: " + x[0]); 
     System.out.println("Max: " + x[1]); 
    } 
} 
+0

這並不完全清楚需求是什麼,但我猜想它希望你從'System.in'重複讀取並將每個讀取的整數值放入一個數組中,直到遇到值-999。我也猜測-999不應該放在數組中,因爲它是終止符。 –

+0

至於「在程序中測試你的方法」,這意味着你應該寫一些代碼來調用你的其他代碼並驗證它是否正常工作。例如,你可能有一個'testMin'方法,它調用'min'並根據你的期望檢查它的輸出。 –

+0

所以從這裏我應該使用If-Statements,對吧?要檢查是否有任何輸入數字遇到-999? – apreciado

回答

0

編輯#1 :

至今已經近24小時,使我會給你有關的要求更多一些提示,以「包括讀取列表的方法......」

你想用一個循環,EN當用戶輸入值-999時。您還必須考慮要求將值讀入數組中,這意味着您必須每次通過循環調整數組的大小。

所以,你的邏輯會去是這樣的:

  1. System.in獲得的第一個整數,並將其存儲在一個變量
  2. 初始化一個數組來保存輸入值,初始長度爲0
  3. 開始循環,直到來自#1的變量等於-999

    a)從#2調整數組的大小

    b)該整數添加到調整大小後的數組

    b)讀取從System.in在相同的變量的下一個整數並存儲爲#1

END EDIT#1

我不我不想太早離開 - 請參閱how to ask homework questions。所以請看我的評論關於要求「包括讀取列表的方法...」。

我會給你如何測試你的「程序中的」代碼很短的例子:

public class TestProblem3{ 
    public static void main(String[] args){ 
     testMin(); 
    } 

    public static void testMin(){ 
     int[] data = {1,2,3,4,5}; 
     int minValue = Problem3.min(data); 
     if(minValue != 1) 
      System.err.println("FAILURE expected 1, but actual value is "+minValue); 

     data = new int[]{-1,2,3,4,5}; 
     minValue = Problem3.min(data); 
     if(minValue != -1) 
      System.err.println("FAILURE expected -1, but actual value is "+minValue); 

     //More tests here! 
    } 

    //More test methods here! 
} 

如果你真的雄心勃勃,你可以瞭解JUnit和/或其他測試框架(如TestNG