[英]program that reads integers between 1 and 100 and counts the occurrence of each
Write a program that reads integers between 1 and 100 and counts the occurrence of each (you should store the numbers in an array).编写一个程序,读取 1 到 100 之间的整数并计算每个整数的出现次数(您应该将这些数字存储在一个数组中)。 Output should be in ascending order.
输出应按升序排列。 Assume the input ends when the user enters a 0.
假设当用户输入 0 时输入结束。
Hi guys, I know that this question has been posted before, perhaps a lot of times, but as I am a complete beginner at java, I don't completely understand the complexity of the codes that are posted.大家好,我知道这个问题之前已经发布过,可能已经发布了很多次,但是由于我是 Java 的一个完整的初学者,我并不完全理解发布的代码的复杂性。 I just started taking Java classes, and would appreciate if you could help me figure out how to get my program to output the correct occurrences at the end.
我刚开始学习 Java 课程,如果您能帮我弄清楚如何让我的程序在最后输出正确的出现,我将不胜感激。 I'm so close to getting the answer but I can't figure it out for the life of me!!
我很接近得到答案,但我一生都无法弄清楚!! Thanks in advance!
提前致谢!
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();
}
} }
You are storing the number entered by the user in the array.您将用户输入的数字存储在数组中。 Instead, you should store a counter in each position of the array for the corresponding integer.
相反,您应该在数组的每个位置为相应的整数存储一个计数器。 When the user inputs a number, you should increase the corresponding counter.
当用户输入一个数字时,应该增加相应的计数器。
The second part of your code (output results) seems ok.代码的第二部分(输出结果)似乎没问题。 It is the first one that needs fixing.
这是第一个需要修复的。
I think the first for loop should be something like this:我认为第一个 for 循环应该是这样的:
for (int i = 0; i < myArray.length; i++)
{
if (input == i)
{
myArray[i] += 1;
}
}
}
This should store add 1 to the array everytime the numbers occurs.这应该在每次出现数字时将加 1 存储到数组中。
hey this my source code that worked out or me.嘿这是我的源代码,或者我。
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.");
}
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.