0

I am trying to write a program that repeatedly asks the user to supply scores (out of 10) on a test.It needs to continue until a negative value is supplied. Values higher than 10 should be ignored. I also calculated the average of the inputs. After the scores have been inputted, i need to use a single array to produce a table that automatically fills the test scores and the number of occurrences of the certain test score.

I wanted it to look something like this:

Score | # of Occurrences  

    0   3    
    1   2
    2   4
    3   5 
    4   6

and so on.. P

I am a beginner and this is my first question, so i am sorry if i made a mistake in posting the question or something.

import java.io.*;
import java.util.*;

public class Tester1
{
public static void main()
{   
    Scanner kbReader= new Scanner (System.in);

    int score[] = new int [10];//idk what im doing with these two arrays
    int numofOcc []= new int [10];

    int counter=0;
    int sum=0;

    for (int i=0;i<10;i++)// Instead of i<10... how would i make it so that it continues until a negative value is entered.
    {
        System.out.println("Enter score out of 10");
        int input=kbReader.nextInt();

        if (input>10)
        {
            System.out.println("Score must be out of 10");

        }
        else if (input<0)
        {
            System.out.println("Score must be out of 10");
            break;
        }
        else 
        {
            counter++;
            sum+=input;

        }
    }
    System.out.println("The mean score is " +(sum/counter));

  }
}
jpw
  • 44,361
  • 6
  • 66
  • 86
sm15
  • 85
  • 3
  • 11

3 Answers3

0

I think what you need is a List Array! Create ArrayList from array

Think of it as a dynamic array, you don't need to specify the size of the array and it is expanded/made smaller automatically.

Community
  • 1
  • 1
fiverivers
  • 5
  • 1
  • 6
0

What you're missing is a while loop. Here is a nice way to loop through a Scanner for input. It also catches numbers greater than 10 and provides an error message:

public static void main() {
    Scanner s = new Scanner(System.in);
    ArrayList<Integer> list = new ArrayList<Integer>();
    int response = 0;
    while (response >= 0) {
        System.out.print("Enter score out of 10: ");
        response = s.nextInt();
        if (response > 10) {
            System.out.println("Score must be out of 10.");
        } else if (response >= 0) {
            list.add(response);
        }
    }
    // Do something with list
}
ElliotSchmelliot
  • 7,322
  • 4
  • 41
  • 64
0

You could use a do...while loop like this:

import java.io.*;
import java.util.*;

public class Tester1
{
    public static void main(String args[]) {
        Scanner kbReader= new Scanner (System.in);

        int scores[] = new int [10];

        int counter = 0;
        int sum = 0;    
        int input = 0;

        do {
            System.out.println("Enter score out of 10 or negative to break.");
            input=kbReader.nextInt();

            if (input<0) {
                break;
            } else if (input>10) {
                System.out.println("Score must be out of 10");
            } else {
                scores[input]++;
                counter++;
                sum+=input;
            }
        } while (input>0);

        System.out.println("Score\t# of occur...");
        for(int i =0; i<10; i++) {
            System.out.println(i + "\t" + scores[i]);
        };

        System.out.println("The mean score is " +(sum/counter));
    }
}

The formatting can certainly be done better (without c-style tabs) but I don't remember the syntax at the moment.

jpw
  • 44,361
  • 6
  • 66
  • 86
  • Nicely done! I see how the do while loop is better than the for loop for this question. Can I somehow use a for loop in this situation? – sm15 Feb 14 '14 at 01:08
  • @sm15 you could use a for-loop without specifying parameters for start;end:increment like this: `for(;;)` and that would be the same as doing a `while(true)` loop. In both cases you would have to break out of the loop using a `break;` statement inside the loop. – jpw Feb 14 '14 at 01:10