Here is what I have. I am so lost at this point, I seem to got the code to running, but the numbers won't stack up!! =[
run:
FUN
Enter the integers between 1 and 100: 1 1 2 4 1 5 0
1 occurs 1 time
1 occurs 1 time
2 occurs 1 time
4 occurs 1 time
1 occurs 1 time
5 occurs 1 time
I want it to look like
run:
FUN
Enter the integers between 1 and 100: 1 1 2 4 1 5 0
1 occurs 3 times
2 occurs 1 time
4 occurs 1 time
5 occurs 1 time
package numberse;
import java.util.Scanner;
public class NumberSe {
public static void main(String[] args) {
System.out.println("FUN");
userInput();
}
public static void userInput() {
Scanner input = new Scanner(System.in);
int userIn = 0;
for (userIn = 0; userIn < 100; userIn++) {
}
System.out.print("Enter the integers between 1 and 100: ");
userIn = input.nextInt();
while (userIn != 0) {
printOut(userIn);
userIn = input.nextInt();
}
}
public static void printOut(int score) {
int[] scores = new int[100];
int[] counts = new int[100];
for (int userIn = 0; userIn < 100; userIn++) {
counts[userIn] = 0;
}
int userIn = score;
while (userIn != 0) {
counts[userIn]++;
userIn = 0;
}
for (userIn = 0; userIn < 100; userIn++) {
if (counts[userIn] != 0) {
if (counts[userIn] > 1) {
System.out.println(userIn + " occurs " + counts[userIn] + " times");
} else {
System.out.println(userIn + " occurs " + counts[userIn] + " time");
}
}
}
}
}
I'm guessing my error happens in
while (userIn != 0) {
counts[userIn]++;
userIn = 0;
}
since I set the value to 0 so it forces the program to stop in a way.
My thought process when I tried to do this was:
1. User enters the input other than 0 (inputs stored in one method)
2. The input is then passed onto my printOut method until a 0 is entered by the user
Can someone help me fix my code and show me how to do this properly with at least a main method, an input method and output method? Thanks! I really appreciate it!


Sign In
Create Account

Back to top









