读取1到100之间的整数的程序,并计算每个的出现次数

时间:2017-03-16 06:28:25

标签: java eclipse

编写一个读取整数的程序 1和100并计算每个的出现次数(您应该将数字存储在数组中)。输出应按升序排列。假设输入在用户输入0时结束。

大家好,我知道这个问题之前已经发布了很多次,但由于我是java的初学者,我不完全理解发布的代码的复杂性。我刚刚开始学习Java课程,如果你能帮助我弄清楚如何让我的程序在最后输出正确的事件,我将不胜感激。我非常接近得到答案,但我无法弄清楚我的生活!提前致谢!

import java.util.Scanner; 
public class Problem1 {

public static void main(String[] args) {

    //declarations 
    int [] myArray = new int [100]; 
    int input = 5; 
    Scanner keyboard = new Scanner(System.in);

    //input and processing 
    System.out.println("Please enter integers between 1 and 100 (enter 0 to stop): ");
    while (input != 0)
    {

        input = keyboard.nextInt();

        for (int i = 0; i < myArray.length; i++)
        {
            if (input == i)
            {
                myArray[i] = input; 
            }
        }

    }

    //output (This is where I need help!!!!) 
    for (int k = 0; k < myArray.length; k++)
    {
        if (myArray[k] != 0)
        {

            System.out.print(k + " occurs " + myArray[k] + " time");
            if (myArray[k] > 1)
            {
                System.out.println("s");
            }
            else 
                System.out.println("");

        }
    }
    keyboard.close(); 
}

}

3 个答案:

答案 0 :(得分:0)

您正在存储用户在数组中输入的数字。相反,您应该在数组的每个位置存储一个计数器以存储相应的整数。当用户输入数字时,您应该增加相应的计数器。

代码的第二部分(输出结果)似乎没问题。这是第一个需要修复的。

答案 1 :(得分:0)

我认为第一个for循环应该是这样的:

for (int i = 0; i < myArray.length; i++)
    {
        if (input == i)
        {
            myArray[i] += 1; 
        }
    }

}

每次出现数字时,都应该将数组加1。

答案 2 :(得分:0)

嘿,这是我自己制定的源代码。

package test2;

import java.util.Arrays;
import java.util.Scanner; 

public class Test2 {

    public static void main(String[] args) {
    java.util.Scanner input = new java.util.Scanner(System.in);

    // ask for user to input numbers
    System.out.println("Enter some integers between 1 and 100 (and 0 when done): ");

    int[] myArray = new int[1000];//create a new array for user inputs

    int number;//variable for user inputs
    int count = 0;

    do
    {
        number = input.nextInt();
        myArray[count] = number;
        count++;
    }

    while (number != 0); 
    int[] mySort = new int [count - 1]; //create a new array with only the numbers 
   for(int i = 0; i< (count-1); i++) { //get the array until 0th number into new

      mySort[i] = myArray[i];
    }

    java.util.Arrays.sort(mySort);// sort the array in ascending order

    int n = 0;

    for(int i = 0; i < mySort.length; i++) {//check if the number have checked before
        int occurance = 0;
        if(n >= mySort[i]) {
            continue;
        }
        else {
        n = mySort[i];//if a new number found do the calculation+ 
        for (int j=0; j<mySort.length; j++) 
            if (n == mySort[j]) 
              occurance++; 
        System.out.print(n +  " occurs " + occurance );
        {
            if (occurance == 1) {
            System.out.println(" time.");
            }
            else {
                System.out.println(" times.");
            }
            }
        }
    }

    }

}