我现在所做的练习将要求我
这是我到目前为止掌握的代码:
问题是,我的模式适用于10个定义的值,但一旦我试图处理它,并为我自己丢失的值添加扫描器。
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int[] arraytwo = {10};
test3(arraytwo);
number = scan.nextInt;
number = arraytwo[i];
}
public static void test3(int[] array)
{
int modeTrack[] = new int[10];
int max =0; int number =0;
for (int i = 0; i < array.length; i++)
{
modeTrack[array[i]] += 1;
}
int maxIndex = 0;
for (int i = 1; i < modeTrack.length; i++)
{
int newNum = modeTrack[i];
if (newNum > modeTrack[maxIndex])
{
maxIndex = i;
}
}System.out.println(maxIndex);
}发布于 2014-04-17 06:30:01
听起来您的test3()方法不是给您带来麻烦的方法,所以我将谈谈您的main()方法。
您在main()中有一些错误,由我插入的注释表示:
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int[] arraytwo = {10};
test3(arraytwo);
number = scan.nextInt; //number does not exist in this scope, also nextInt method should use () to call it.
number = arraytwo[i]; //i does not exist in this scope
}对于主要方法,我建议您执行如下操作:
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
Integer[] arrTwo = new Integer[10];
for (int i = 0; i < 10; i++)
{
arrTwo[i] = scan.nextInt();
}
sort(arrTwo);
//System.out.println(Arrays.toString(arrTwo)); //this line will print your sorted array for you..
}对于排序方法,可以执行如下操作:
public static void sort(Integer[] arr){
Arrays.sort(arr, Collections.reverseOrder());
}发布于 2014-04-17 06:44:11
这段代码将取10个值,并在降序order..Hope中对它们进行排序,它将帮助you..save在Test.java中的代码下面运行
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class Test {
public static void main(String[] args)
{
Integer[] array = new Integer[10];
Scanner scan = new Scanner(System.in);
for (int i = 0; i <10; i++) {
array[i] = scan.nextInt();
}
doSort(array);
}
static void doSort(Integer[] array)
{
Arrays.sort(array,Collections.reverseOrder());
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
}https://stackoverflow.com/questions/23125945
复制相似问题