2012-11-08 224 views
1

您好以下程序有什麼問題?因爲我希望它顯示用戶輸入的整數值範圍爲1-10, 11-20,21-30 ... 191-200java顯示輸入範圍

public class Program 
{ 
    /** 
    * This is the main entry point for the application 
    */ 
    public static void main(String args[]) 
{ 
    int a[] = new int[100]; 
    int i = 0; 
    Scanner in = new Scanner(System.in); 
    while(i<100) 
    { 
    System.out.println("Enter a int"); 
    a[i] = in.nextInt(); 
    displayStatistics(a[i]); 


    } 

} 

    public static void displayStatistics(integer[] a[i]) 
    { 
     if(a[i]>=1 && a[i]<=100) 
     { 
     i++; 
     System.out.println(); ----> need to display in range 1-10, 11-20,21-30 ... 191-200 
     } else { 
     System.out.println("Integer not in range of 1-200"); 
     } 
    } 
} 
+0

您正在將'int'值傳遞給displayStatistics。但方法簽名說,輸入將是整數數組。 –

+2

「有什麼問題?」的簡單答案問題是你的代碼甚至沒有試圖做你說的需要做的事情。 – NPE

回答

1
public static void displayStatistics(int k) 
    { 
     if(k>=1 && k<=200) 
     { 
     int low,high; 
     if(k%10==0) 
     { 
      low=k-9; 
      high=k; 
     } 
     else 
     { 
      low=k-k%10+1; 
      high=k-k%10+10; 
     } 
     System.out.println("value in range " +low+" -"+high); 

     } else { 
     System.out.println("Integer not in range of 1-200"); 
     } 
    } 

記住你是在傳遞一個整數的函數,而不是完整的陣列

0

,必須從上面的代碼獲取編譯器錯誤。更改方法

public static void displayStatistics(int a) { 
    if (a >= 1 && a <= 100) { 
     System.out.println("Input[" + a + "] is within the range 1 to 100"); 
    } else { 
     System.out.println("Integer not in range of 1-200"); 
    } 
} 

同樣可以爲其它範圍內進行檢查添加else if