2

I am currently having trouble to end my program with a users input. I currently now can only end when the user has inputted 20 numbers for example. What I want it to do is if a user inputs a number over 100, it should stop the program and display a histogram as well as a number to show the amount of times the user has inputted a number every time. Sorry if I am not making any sense, I will update this post if any further information is needed. Here is my code.

import java.util.Scanner;

public class Marks {
    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        int[] array = new int[23];
        int num = 0;
        int count = 0;
        int total = 0;

        System.out.println ("Enter students marks in the range 0 to 100\n");

        for (count = 0; count <= 20; count++)
        {
            System.out.println ("Enter a number:");
            num = scan.nextInt();
            while (num < 0 || num > 100)
            {
                System.out.println ("Invalid number. Enter a valid number.");
                num = scan.nextInt();
            }
            array[count] = num;
           total=count;
        }
        System.out.println ("How many times a number between 0-100 occur.");

        String[] asterisk = {"0- 29   | ", "30- 39  | ","40- 69  | ", "70- 100 | "}; //4 strings

        for (count = 0; count <= 20; count++)
        {
            num=array[count];
            if (num <=29) asterisk [0] +="*";
            else if (num <=39) asterisk[1] +="*";
            else if (num <=69) asterisk[2] +="*";
            else if (num <=100) asterisk[3] +="*";
        }
        for (count =0;count < 4;count++)
            System.out.println(asterisk[count]);
        System.out.println("The total amount of students is " + total);
    }
}
1
  • see my answer. It should workl. Commented Nov 27, 2013 at 14:12

7 Answers 7

4

When the users interacts, you can write

System.exit(0);
Sign up to request clarification or add additional context in comments.

1 Comment

I tried doing this but it doesn't show the histogram as well as doesn't let me enter more than 1 number
1

You have errors in your code

Here is your updated code

public class Marks {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);

    int[] array = new int[23];
    int num = 0;
    int count = 0;
    int total = 1;

    System.out.println ("Enter students marks in the range 0 to 100\n");

        loop: for (count = 0; count <= 20; count++) {
            System.out.println("Enter a number:");
            num = scan.nextInt();
            if (num < 0 || num > 100) {
                break loop;

            }
            array[count] = num;
            total = count+1;
        }
    System.out.println ("How many times a number between 0-100 occur.");

    String[] asterisk = {"0- 29   | ", "30- 39  | ","40- 69  | ", "70- 100 | "}; //4 strings

    for (count = 1; count <= total; count++)
    {
        num=array[count];
        if (num >=0 && num<=29) asterisk [0] +="*";
        else if (num>29 && num<=39) asterisk[1] +="*";
        else if (num>39 && num <=69) asterisk[2] +="*";
        else if (num >69 && num <=100) asterisk[3] +="*";
    }
    for (count =0;count < 4;count++)
        System.out.println(asterisk[count]);
    System.out.println("The total amount of students is " + total);
}
}

Here is the output

Enter students marks in the range 0 to 100

Enter a number:
1

Enter a number:
10
Enter a number:
50
Enter a number:
111
How many times a number between 0-100 occur.
0- 29   | **
30- 39  | 
40- 69  | *
70- 100 | 
The total amount of students is 3

4 Comments

It worked but on my histogram it shows too many *'s when I only entered three numbers. It should only display three numbers in whichever range
Yes that's the problem it should not be doing that. You entered four numbers. Numbers 44, 44 and 55 should be displayed in range 40-69. The total amount of students should also be saying 3 not 2 since you entered 3 numbers that fall with the mark range. There should not be anything shown in 0-29 since there's no number that fits into that range.
Ah! Thank you it worked. It never came to my mind that I should have changed 20 to total. Didn't know it would work and neither it came to mind.
Not only that you should see hwo I have updated the if conditions. Thumb up and accept as answer now :)
0
else if (num >100) System.exit(0);  

this should do the trick no ?

Comments

0

Have you tried using System.exit(0)?

Comments

0

You should checkout how to use while loops.

Instead of the for loop. and have some statment like

while( userinput < 100)
{
 //insert code here
}

Comments

0

On the matching condition u can use System.exit(0)

if(matchCondition) {
    System.exit(0);
}

Comments

0

This fixes... Quick and dirty:

    for (count = 0; count <= 20; count++)
    {
        System.out.println ("Enter a number:");
        num = scan.nextInt();
        while (num < 0 || num > 100)
        {
            if (num > 100) {
               count = 20;
               break;
            }

            System.out.println ("Invalid number. Enter a valid number.");
            num = scan.nextInt();

        }
        array[count] = num;
       total=count;
    }

But I wouldn't recommend this approach. Consider using while loop instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.