【问题标题】:Trying to prompt for 10 numbers for my array to do a mode尝试提示我的阵列输入 10 个数字以执行模式
【发布时间】:2014-04-17 06:20:03
【问题描述】:

我现在正在尝试的练习将要求我这样做

  • 从用户那里收集 10 个值以存储到数组中
  • 显示10个值的模式
  • (可能使用并行数组来计算值的出现次数)
  • 使用方法将输入的值作为参数,并按顺序返回具有最大值的数组

这是我目前的代码:

问题是我的模式适用于 10 个 DEFINED 值,但是一旦我试图弄乱它并为我自己丢失的值添加扫描仪

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);
}

【问题讨论】:

  • int[] arraytwo = {10}; 你到底想在这里做什么?
  • 我试图确定我最多只能接受 10 个值到我的数组中
  • 那你应该改写int[] arraytwo = new int[10];。你写的意思是你有一个包含一个值的数组,10。

标签: java arrays for-loop logic java.util.scanner


【解决方案1】:

此代码将取十个值并按降序对它们进行排序..希望对您有所帮助..将下面的代码保存在 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]);
        }
    }

}

【讨论】:

    【解决方案2】:

    听起来你的 test3() 方法不是给你带来麻烦的,所以我会谈谈你的 main() 方法。

    您的 main() 中有一些错误,由我插入的 cmets 表示:

    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
    }
    

    对于 main 方法,我建议您执行以下操作:

    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());
    }
    

    【讨论】:

    • 有没有办法使用扫描仪代替?我用扫描仪试了一下,得到了 readLine() 的错误
    • 是 br.next(); ?
    • 成功了。有没有办法以降序显示另一个数组中的值?
    • 我会以某种方式使用冒泡排序吗?
    • 这篇文章对如何对整数数组进行排序有一些很好的想法:stackoverflow.com/questions/9791024/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-27
    • 1970-01-01
    • 2019-07-03
    • 1970-01-01
    • 2014-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多