2016-06-28 51 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。在这个问题中有一个单独的问题阵列,问题的关键,选项,答案。用户看到问题及其四个选项,并将输入答案。答案数组将与关键数组进行比较,其中选项是真正的显示正确答案 –