2013-03-14 49 views
0

所以我有一個方法,我打電話給我的主要10個數字的數組,並創建一個嵌套for循環直方圖輸出。我無法弄清楚如何在行標題旁邊找到正確數量的星號。該數組從前一個方法傳遞給該方法。謝謝!!如何從Java中的數組生成直方圖輸出?

public static void outputHistogram(int [] list) 
{ 

    int k =0; 
    for(int i=0;i<=9;++i) 
    { 
     System.out.print((i*10+1) +"-"+(i*10+10)+":"+"\t"); 

     for(int j=1; j<=i;++j) 
      System.out.print("*"); 


     System.out.println(); 
    } 
} 
+0

你爲什麼不使用列表輸入?你怎麼想輸出看起來像? – Spoike 2013-03-14 08:11:48

+0

int k = 0;什麼都不做......你甚至需要它嗎? – Lyrion 2013-03-14 08:18:05

+0

不,我不需要它。刪除!謝謝! – MaryK 2013-03-14 08:22:26

回答

1

您甚至不會在outputHistorgram方法的任何地方使用您的列表。我不知道究竟是本應產生,但也許做for循環像這可能幫助:

for(int i : list) 

如果這是不正確的,然後給例如輸入和輸出。

+0

好吧,所以輸入是一個從前面的方法十個整數的數組,例如{ 0,3,2,4,5,1,0,2,1,0},輸出應該如下所示:1-10:11-20:*** 21-30:**等。 – MaryK 2013-03-14 08:11:47

2

如果你打算使用list,那麼你可能有一個for循環看起來像這樣: -

for(int i=0;i<list.length;i++)

和第二for循環應該是這樣的: -

for(int j=0; j<list[i];j++)

+0

哦,我愛你。這工作完美!我覺得花了這麼多小時試圖弄清楚這一點真是愚蠢!謝謝你,R.J! – MaryK 2013-03-14 08:22:04

+0

樂意幫忙! :) – SudoRahul 2013-03-14 08:26:43

+0

不要忘記接受爲您服務的答案。當你的問題解決後,請始終接受答案。:) – SudoRahul 2013-03-14 11:46:57

1

您在第二個循環中的測試需要爲j < list[i]

+0

謝謝克里斯!這就是它!花了我足夠長的時間! eesh! – MaryK 2013-03-14 08:23:38

+0

@MaryK新用戶建議:不要忘記投票答案,幫助並接受最符合您需求的答案。 :) – 2013-03-14 10:40:48

0

我想你想這樣做。

public static void outputHistogram(int [] list) 

{

int k =0; 
for(int i=0;i<=9;++i) 
{ 
    System.out.print((i*10+1) +"-"+(i*10+10)+":"+"\t"); 

    for(int j=1; j<=(i*10+10);++j) 
     System.out.print("*"); 


    System.out.println(); 
} 

}

0
import java.util.Scanner; 
class Histogram 
{ 
    private int count[]=new int[10]; // count array will keep elements of element 
            // in particular range; 
    public void showHistogram(int elements[])  // for example 27 15 34 22 11 11 19 
    {         // in above input there is count[0]=0; 
    for(int i=0;i<elements.length;i++) // count[1]=4 and count[2]=2 and count[3]=1; 
    { 
    if(elements[i]>=0 && elements[i]<50) 
    { 
     if(elements[i]<10) 
     { 
      count[0]++; 
     } 
     else if(elements[i]>=10 && elements[i]<20) 
     { 
      count[1]++; 
     } 
     else if(elements[i]>=20 && elements[i]<30) 
     { 
      count[2]++; 
     } 
     else if(elements[i]>=30 && elements[i]<40) 
     { 
      count[3]++; 
     } 
     else 
     { 
      count[4]++; 
     } 
    } 
    else if(elements[i]>=50 &&elements[i]<=100) 
    { 
     if(elements[i]<60) 
     { 
      count[5]++; 
     } 
     else if(elements[i]>=60 && elements[i]<70) 
     { 
      count[6]++; 
     } 
     else if(elements[i]>=70 && elements[i]<80) 
     { 
      count[7]++; 
     } 
     else if(elements[i]>=80 && elements[i]<90) 
     { 
      count[8]++; 
     } 
     else 
     { 
      count[9]++; 
     } 
    } 
    } 
    showHistogram1(); 
} 
    private void showHistogram1() 
    { 
    System.out.println("Histogram of the elements:"); 
    for(int i=0;i<count.length;i++)  // this loop will print line 
    { 
     for(int j=0;j<count[i];j++)  // this will print elements element(*) 
     {        // at each line. 
      System.out.print("* "); 
     } 
     if(count[i]!=0)     // if line does'nt contain zero 
     System.out.println("");   // then if will change the row; 
    } 
    } 
} 
/* 
    in above code if count[i]=zero means if there is elements 
    element in particular range say [0-9] then it will 
    elementst jump on next line; 
*/ 
class HistogramGenerator //Question3 
{ 
public static void main(String args[]) 
{ 
    Histogram hg=new Histogram(); 
    System.out.println("Enter the elements of Elements want in a Histogram:"); 
    Scanner sc=new Scanner(System.in); 

    int noOfElements=sc.nextInt(); 
    int histogramElements[]=new int[noOfElements]; 

    System.out.println("Enter the Elements for Histogram:"); 
    for(int i=0;i<noOfElements;i++) 
    { 
     histogramElements[i]=sc.nextInt(); 
    } 

    hg.showHistogram(histogramElements); 
} 
}