2011-09-30 22 views
2

我有一個接收數組的程序。我想讓它將結果組織到範圍內(如60-69)。它還需要知道它收到的數字中有多少可以放入哪個範圍,以便相應地放入。我看到在表格中創建制表符的方式是使用Java將數組輸入組織到表中?

System.out.print("Column1/tColumn2"); 

如何將數組數據組織到一個範圍內?你是否用一堆IF語句指定它?我假設你使用一個計數器來計算有多少數字適合哪些範圍?

編輯:這裏是目前的代碼。

import java.util.Scanner; 
import java.lang.Integer; 
import java.lang.String; 

public class Salary { 
int salary; 
int sales; 

public static void main(String[] args) { 
    Scanner input = new Scanner (System.in); 
    int Salary; 
    Salary salary = new Salary(); 

    System.out.print("Please enter the amount each employee brought in:"); 
    String sales = input.nextLine(); 
    String salesArray[] = sales.split(" "); 


    for(int i=0; i<salesArray.length; i++){ 
    Integer myInt = Integer.parseInt(salesArray[i]); 

    System.out.print(salesArray[i] + " "); 
    if (Integer.parseInt(salesArray[i]) <= 0) { 
    System.out.println("Please enter a value greater than zero");} 
    else { 
    Salary = ((myInt/100) * 9) + 200; 
    System.out.print(Salary); } 
    } 

    int two = 0; //declared a variable, starting it at 0 and intending to increment it 
       //with input from the array 

    System.out.print("Range/t#ofEmployees"); 
    for(int i=0; i<salesArray.length; i++) { 
    if (salesArray[i] <= 200){} //Error on the if statement, using the wrong operand 
    else{ two++;}  //Attempt at the table and increment. 

    } 

} 

} 
+1

如果你找不到方法,請隨機抽取一些虛擬數據,然後用這個虛擬數據在一張紙上手工解決這個問題......當你這樣做時,寫下你做的事......然後嘗試正式化那些步驟 – DarkSquirrel42

+2

「我想讓它把結果組織到範圍內(比如60-69)」你能解釋一下嗎? –

+0

我認爲任務是將標量值聚合到組計數器中... 5個事件,其中值在60和69之間... 16個事件,其中值在70和79之間......等 – DarkSquirrel42

回答

2

我不會發表的實現,因爲這是家庭作業,但想到這:

您的陣列可以被分類...

可以itterate該數組,和計數的東西了,如果當前值小於一定的邊界......一旦它是更大的您可以打印一個範圍,startover下一個範圍...

邊界可以從範圍到範圍內變化......

+0

還要注意,Java內置了排序方法(對於集合或數組) – dbeer