2016-06-28 22 views
-3

基本上它是一個程序,從用戶採取6個輸入數字,將它們存儲在一個數組中,計算它們的模式。如何獲得單模值?

package p18; 
import java.util.Scanner; 
public class P18 { 
public static void main(String[] args) { 
    Scanner S=new Scanner(System.in); 
    int[] arr1=new int [6]; 
    for (int i = 0; i < 6; ++i) { 
    int g = S.nextInt(); 
    arr1[i] = g; 
} 
int input=6; 
for(int i=0;i<input;i++) 
    int count=0; 
for(int j=0;j<input;j++) 
{ 
    int temp=arr1[j];  
    int tempco=0; 
    for(int p=0;p<input;p++) 
    if(arr1[p].equals[temp]) 
    tempco++; 
    if(tempco>count) 
    { 
    int t=temp; 
    count=tempco; 
    }} 
System.out.println("the most frequent number"+t+, +count); 
}} 
+1

首先,正確地設置問題的格式和你的代碼,使用縮進它將在未來幫助你。其次說明你的問題,並儘可能清楚。 –

+0

我的問題是隻計算模式 –

+0

Kruti Patel關於答案是什麼? –

回答

0

我認爲這是最好的辦法。使用一個HashMap,索引數組的元素和每個位置的值,這個數字在數組中的出現次數。

Scanner S=new Scanner(System.in); 
    int[] arr1=new int [6]; 
    for (int i = 0; i < 6; ++i) { 
     int g = S.nextInt(); 
     arr1[i] = g; 
    } 
    S.close(); 
    int t = 0; 
    int count = 0; 
    int input=arr1.length; 

    for(int j=0;j<input;j++){ 
     int temp=arr1[j];  
     int tempco=0; 
     for(int p=0;p<input;p++){ 
      if(arr1[p]==temp) 
       tempco++; 
      if(tempco>count){ 
       t=temp; 
       count=tempco; 
      } 
     } 
    } 
    System.out.println("the most frequent number is "+String.valueOf(t)+"with "+String.valueOf(count)+" occurrences"); 

你走了,解決方案與你的代碼糾正。你的代碼有幾處錯誤,所以請不要習慣於將代碼解決方案作爲例程來使用,這樣你就不會學到任何東西,而這個網絡並不是那樣的,而是要求具體的問題或錯誤,你不知道不要做一個完整的程序

+0

對不起,嘗試自己,當你有一個完整的程序和準時的懷疑或問題嘗試發佈它在stackoverflow,看看有沒有人可以幫助你 –

+0

先生a.ras我試試我自己plz看到我的新代碼MCQS。在這個問題中有一個單獨的問題陣列,問題的關鍵,選項,答案。用戶看到問題及其四個選項,並將輸入答案。答案數組將與關鍵數組進行比較,其中選項是真正的顯示正確答案 –