问题描述
有关我的项目,我需要做一个程序,需要10个号码作为输入,并显示这些数字的模式。该计划应使用两个数组,得花数的数组作为参数,并在阵列返回的max值的方法。
基本上,我到目前为止所做的是使用第二阵列跟踪的多少次出现的数字。纵观初始阵列,你会看到,模式为4(号码出现最多)。在第二阵列,索引4将具有2的值,并且因此2将第二阵列中的最大值。我需要在我的第二个数组来定位这个最大值,并打印索引。我的输出应该是'4'。
我的计划是好的,直到我试图产生'4',我已经尝试了一些不同的东西,但似乎无法得到它才能正常工作。
感谢您的时间!
公共类arrayProject {公共静态无效的主要(字串[] args){
INT [] arraytwo = {0,1,2,3,4,4,6,7,8,9};
projecttwo(arraytwo);
}
公共静态无效projecttwo(INT []数组){
/ *程序,它接受10个号码作为输入,并显示这些数字的模式。程序应该使用并行
阵列,得花数的数组作为参数,并在阵列返回的max值的方法* /
INT modetracker [] =新INT [10];
INT最大= 0; INT数= 0;
的for(int i = 0; I< array.length,我++){
modetracker [阵列[我] + = 1; //添加一个modetracker各指标出现的地方数组的元素[I]。
} INT索引= 0;
的for(int i = 1; I< modetracker.length;我++){
INT newnumber = modetracker [I]
如果((newnumber> modetracker [I-1])== TRUE){
指数= I;
}
}的System.out.println(+指数);}
}
您的错误是在比较 IF((newnumber> modetracker [I-1])
你。应检查 newnumber
是更大然后是已经发现的最大也就是说 IF((newnumber方式> modetracker [maxIndex])
您应该改变你的最后行:
INT maxIndex = 0;
的for(int i = 1; I< modetracker.length;我++){
INT newnumber = modetracker [I]
如果((newnumber> modetracker [maxIndex])){
maxIndex = I;
}
}的System.out.println(maxIndex);
For my project, I need to make a program that takes 10 numbers as input and displays the mode of these numbers. The program should use two arrays and a method that takes array of numbers as parameter and returns max value in array.
Basically, what I've done so far is used a second array to keep track of how many times a number appears. Looking at the initial array, you will see that the mode is 4. (Number that appears most). In the second array, the index 4 will have a value of 2, and thus 2 will be the maximum value in the second array. I need to locate this max value in my second array, and print the index. My output should be '4'.
My program is good up until I attempt to produce the '4', and I've tried a few different things but can't seem to get it to work properly.
Thank you for your time!
public class arrayProject {
public static void main(String[] args) {
int[] arraytwo = {0, 1, 2, 3, 4, 4, 6, 7, 8, 9};
projecttwo(arraytwo);
}
public static void projecttwo(int[]array){
/*Program that takes 10 numbers as input and displays the mode of these numbers. Program should use parallel
arrays and a method that takes array of numbers as parameter and returns max value in array*/
int modetracker[] = new int[10];
int max = 0; int number = 0;
for (int i = 0; i < array.length; i++){
modetracker[array[i]] += 1; //Add one to each index of modetracker where the element of array[i] appears.
}
int index = 0;
for (int i = 1; i < modetracker.length; i++){
int newnumber = modetracker[i];
if ((newnumber > modetracker[i-1]) == true){
index = i;
}
} System.out.println(+index);
}
}
Your mistake is in comparing if ((newnumber > modetracker[i-1])
. You should check if the newnumber
is bigger then the already found max. That is if ((newnumber > modetracker[maxIndex])
You should change your last rows to:
int maxIndex = 0;
for (int i = 1; i < modetracker.length; i++){
int newnumber = modetracker[i];
if ((newnumber > modetracker[maxIndex])){
maxIndex = i;
}
} System.out.println(maxIndex);
这篇关于我怎样才能找到并打印最大值的指数在一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!