I am a beginner who needs help with one part of my coursework
I basically have to create a histogram that displays marks in this form
0 - 29: ****
30 - 49: *****
50 - 69: **
70 - 100: *****
I have nearly done everything except don't know how to actually display the "asteriks" (*)
I know for a fact that I have to use for or while loops but don't know how to
I am using Java Net Beans and my questions are
1) What code would I need to display display the asteriks?
2) Where would I put it?
P.S I could have just used strings at the beginning but for this piece of assignment I am not allowed to...
Here is my code;
package javaapplication3;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int inputnumber = 0;
int cat1 = 0;
int cat2 = 0;
int cat3 = 0;
int cat4 = 0;
int counter = 0;
while (inputnumber <= 100) {
System.out.println("Please Enter Mark:");
inputnumber = input.nextInt();
if (inputnumber < 29) {
cat1 = cat1 + 1;
counter = counter + 1;
}
if ((inputnumber > 29) && (inputnumber <= 39)) {
cat2 = cat2 + 1;
counter = counter + 1;
}
if ((inputnumber > 39) && (inputnumber <= 69)) {
cat3 = cat3 + 1;
counter = counter + 1;
}
if ((inputnumber > 69) && (inputnumber <= 100)) {
cat4 = cat4 + 1;
counter = counter + 1;
}
}
System.out.println("\n0-29: " + cat1 + "\n\n30-39: " + cat2 + "\n\n40-69: " + cat3 + "\n\n70-100: " + cat4);
System.out.println("\n" + counter + " " + "Students in total");
}
}
------------------------
Please help me ):
Kind Regards
16 replies to this topic
#1
Posted 05 December 2011 - 09:32 AM
|
|
|
#2
Posted 05 December 2011 - 11:23 PM
Just loop from 0 to cat1 / cat2 / cat3 / cat4, like you looped from 0 to 100 already.
With just 1 thing in the loop: System.out.print("*"); -- NOT println!
By the way, you're missing '=' at the first if-statement. number 29 matches not a single if.
With just 1 thing in the loop: System.out.print("*"); -- NOT println!
By the way, you're missing '=' at the first if-statement. number 29 matches not a single if.
#3
Posted 06 December 2011 - 05:49 AM
Thanks for the reply and correction!!
I've done what you told em to do but it's still not printing?
Is this the right code to use
while (cat1 > 0){
cat1 ++;
System.out.print("*");
}
or
while (cat1 > 0){
System.out.print("*");
}
or do I need o use a for loop, or putting in the wrong values?
Do I repeat this for every cat? and at the bottom have one line for all of them?
Do I put this code within the while loop I already have or do I have place is right at the bottom after the 100 loop???
---------- Post added at 05:49 AM ---------- Previous post was at 05:46 AM ----------
sorry if its from o to cat is it something like this?
int br = 0;
while (br => cat1){
cat1 ++;
System.out.print("*");
}
I've done what you told em to do but it's still not printing?
Is this the right code to use
while (cat1 > 0){
cat1 ++;
System.out.print("*");
}
or
while (cat1 > 0){
System.out.print("*");
}
or do I need o use a for loop, or putting in the wrong values?
Do I repeat this for every cat? and at the bottom have one line for all of them?
Do I put this code within the while loop I already have or do I have place is right at the bottom after the 100 loop???
---------- Post added at 05:49 AM ---------- Previous post was at 05:46 AM ----------
sorry if its from o to cat is it something like this?
int br = 0;
while (br => cat1){
cat1 ++;
System.out.print("*");
}
#4
Posted 06 December 2011 - 05:51 AM
It should be after this loop:
So I assume you placed it wrong (luckily^^)
You can choose between
---------- Post added at 03:51 PM ---------- Previous post was at 03:50 PM ----------
This would work as well:
while (inputnumber <= 100) {
...
}
//here
If your code would've ran, I expected it would run indefinitely :PSo I assume you placed it wrong (luckily^^)
You can choose between
while (cat1 > 0){
cat1--;
System.out.print("*");
}
Or
for(int i=0 ; i<cat1 ; i++){
System.out.print("*");
}
---------- Post added at 03:51 PM ---------- Previous post was at 03:50 PM ----------
This would work as well:
int br = 0;
while (br < cat1){
br ++;
System.out.print("*");
}
#5
Posted 06 December 2011 - 06:01 AM
I placed it like this as you asked but it still doesn't work ):
while (inputnumber <= 100) {
System.out.println("Please Enter Mark:");
inputnumber = input.nextInt();
}
for(int i=0 ; i<cat1 ; i++){
System.out.print("*");
}
thanks so much for helping me
while (inputnumber <= 100) {
System.out.println("Please Enter Mark:");
inputnumber = input.nextInt();
}
for(int i=0 ; i<cat1 ; i++){
System.out.print("*");
}
thanks so much for helping me
#6
Posted 06 December 2011 - 06:06 AM
Works fine for me.
You still need those if-statements in your while loop.
This works:
You still need those if-statements in your while loop.
if (inputnumber < 29) {
cat1 = cat1 + 1;
counter = counter + 1;
}
if ((inputnumber > 29) && (inputnumber <= 39)) {
cat2 = cat2 + 1;
counter = counter + 1;
}
if ((inputnumber > 39) && (inputnumber <= 69)) {
cat3 = cat3 + 1;
counter = counter + 1;
}
if ((inputnumber > 69) && (inputnumber <= 100)) {
cat4 = cat4 + 1;
counter = counter + 1;
}
This works:
Scanner input = new Scanner(System.in);
int inputnumber = 0, cat1 = 0, cat2 = 0, cat3 = 0, cat4 = 0;
while (inputnumber <= 100)
{
System.out.println("Please Enter Mark:");
inputnumber = input.nextInt();
if (inputnumber < 29)
{
cat1 = cat1 + 1;
// counter = counter + 1;
}
if ((inputnumber > 29) && (inputnumber <= 39))
{
cat2 = cat2 + 1;
// counter = counter + 1;
}
if ((inputnumber > 39) && (inputnumber <= 69))
{
cat3 = cat3 + 1;
// counter = counter + 1;
}
if ((inputnumber > 69) && (inputnumber <= 100))
{
cat4 = cat4 + 1;
// counter = counter + 1;
}
}
for (int i = 0; i < cat1; i++)
{
System.out.print("*");
}
}
#7
Posted 06 December 2011 - 06:17 AM
Ok thanks so much the concept does work but I have another problem, how do I actually print the asterisks * next to the range ;)
for example, the program should end like this
0 - 29 *****
30 - 59 ****
60 - 79 ***
80 - 100 ***
---------- Post added at 06:17 AM ---------- Previous post was at 06:14 AM ----------
so basically the numbers in cat1 would be replaced with the asteriks so if i get 4 numbers in the range between 0 - 29
it should be displayed as
0 - 29 ****
thanks ever so much
for example, the program should end like this
0 - 29 *****
30 - 59 ****
60 - 79 ***
80 - 100 ***
---------- Post added at 06:17 AM ---------- Previous post was at 06:14 AM ----------
so basically the numbers in cat1 would be replaced with the asteriks so if i get 4 numbers in the range between 0 - 29
it should be displayed as
0 - 29 ****
thanks ever so much
#8
Posted 06 December 2011 - 06:19 AM
Just print it before you do the asterisks :P
System.out.print("0-29");
for (int i = 0; i < cat1; i++) {
System.out.print("*");
}
#9
Posted 06 December 2011 - 06:39 AM
Hey I tried it, the concept works again thank you but it's literally printing it off like this
0-29*30-49*40-69*70-100
0-29: 1
30-39: 1
40-69: 1
70-100: 0
What am I doing wrong lol, here is my code:
if ((inputnumber > 69) && (inputnumber <= 100)) {
cat4 = cat4 + 1;
counter = counter + 1;
}
}
System.out.print("0-29");
for (int i = 0; i < cat1; i++)
{
System.out.print("*");
}
System.out.print("30-49");
for (int i = 0; i < cat2; i++)
{
System.out.print("*");
}
System.out.print("40-69");
for (int i = 0; i < cat3; i++)
{
System.out.print("*");
}
System.out.print("70-100");
for (int i = 0; i < cat4; i++)
{
System.out.print("*");
}
System.out.println("\n0-29: " + cat1 + "\n\n30-39: " + cat2 + "\n\n40-69: " + cat3 + "\n\n70-100: " + cat4);
System.out.println("\n" + counter + " " + "Student(s) in total");
}
}
0-29*30-49*40-69*70-100
0-29: 1
30-39: 1
40-69: 1
70-100: 0
What am I doing wrong lol, here is my code:
if ((inputnumber > 69) && (inputnumber <= 100)) {
cat4 = cat4 + 1;
counter = counter + 1;
}
}
System.out.print("0-29");
for (int i = 0; i < cat1; i++)
{
System.out.print("*");
}
System.out.print("30-49");
for (int i = 0; i < cat2; i++)
{
System.out.print("*");
}
System.out.print("40-69");
for (int i = 0; i < cat3; i++)
{
System.out.print("*");
}
System.out.print("70-100");
for (int i = 0; i < cat4; i++)
{
System.out.print("*");
}
System.out.println("\n0-29: " + cat1 + "\n\n30-39: " + cat2 + "\n\n40-69: " + cat3 + "\n\n70-100: " + cat4);
System.out.println("\n" + counter + " " + "Student(s) in total");
}
}
#10
Posted 06 December 2011 - 06:41 AM
That's correct output apart from needing some enters, right?
for (int i = 0; i < cat2; i++)
{
System.out.print("*");
}
[B]System.out.println();[/B]
#11
Posted 06 December 2011 - 06:50 AM
It's still printing it out like this;
0-29*30-49**40-6970-100*
When I basically need the ranges to be layed out vertically ;)
0 - 29 *
30 - 49 **
40 - 69
70 - 100*
Is it somethign to do with print and println, shall; i put println next to the ranges on the top, here is my current code wih the problem:
System.out.print("0-29");
for (int i = 0; i < cat1; i++)
{
System.out.print("*");
}
System.out.print("30-49");
for (int i = 0; i < cat2; i++)
{
System.out.print("*");
}
System.out.println();
System.out.print("40-69");
for (int i = 0; i < cat3; i++)
{
System.out.print("*");
}
System.out.println( );
System.out.print("70-100");
for (int i = 0; i < cat4; i++)
{
System.out.print("*");
}
System.out.println( );
System.out.println("\n0-29: " + cat1 + "\n\n30-39: " + cat2 + "\n\n40-69: " + cat3 + "\n\n70-100: " + cat4);
System.out.println("\n" + counter + " " + "Student(s) in total");
Thanks
0-29*30-49**40-6970-100*
When I basically need the ranges to be layed out vertically ;)
0 - 29 *
30 - 49 **
40 - 69
70 - 100*
Is it somethign to do with print and println, shall; i put println next to the ranges on the top, here is my current code wih the problem:
System.out.print("0-29");
for (int i = 0; i < cat1; i++)
{
System.out.print("*");
}
System.out.print("30-49");
for (int i = 0; i < cat2; i++)
{
System.out.print("*");
}
System.out.println();
System.out.print("40-69");
for (int i = 0; i < cat3; i++)
{
System.out.print("*");
}
System.out.println( );
System.out.print("70-100");
for (int i = 0; i < cat4; i++)
{
System.out.print("*");
}
System.out.println( );
System.out.println("\n0-29: " + cat1 + "\n\n30-39: " + cat2 + "\n\n40-69: " + cat3 + "\n\n70-100: " + cat4);
System.out.println("\n" + counter + " " + "Student(s) in total");
Thanks
#12
Posted 06 December 2011 - 06:53 AM
Apart from missing a println after the first loop that seems fine...
Maybe you didn't compile / save the new source properly? Because you should have 3 [Enter]s even with the missing one.
run this:
Maybe you didn't compile / save the new source properly? Because you should have 3 [Enter]s even with the missing one.
run this:
public static void main(String[] args)
{
System.out.print("0-29");
for (int i = 0; i < 5; i++)
{
System.out.print("*");
}
System.out.println();
System.out.print("30-49");
for (int i = 0; i < 4; i++)
{
System.out.print("*");
}
System.out.println();
System.out.print("40-69");
for (int i = 0; i < 6; i++)
{
System.out.print("*");
}
System.out.println();
System.out.print("70-100");
for (int i = 0; i < 2; i++)
{
System.out.print("*");
}
System.out.println();
}
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









