How to print output vertically
Basically the user enters the students' grades and the program will display and place them in sections (0-29 marks, 30-39 marks, 40-69 marks and 70-100 marks). If the user enters anything over 100, then the program exits and displays the results.
I'm just having problems printing the output vertically.
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Type in the students marks. Type above 100 to show output.");
int marks;
int starsfrom0to29 = 0;
int starsfrom30to39 = 0;
int starsfrom40to69 = 0;
int starsfrom70to100 = 0;
int counter = 0;
marks = input.nextInt();
while (marks < 100) {
//PUTTING MARKS INTO CATEGOERY
if ((marks >= 0) && (marks <= 29)) {
starsfrom0to29++;
}
if ((marks >= 30) && (marks <= 39)) {
starsfrom30to39++;
}
if ((marks >= 40) && (marks <= 69)) {
starsfrom40to69++;
}
if ((marks >= 70) && (marks <= 100)) {
starsfrom70to100++;
}
if (marks < 100) {
counter++;
}
marks = input.nextInt();
}
//PRINTING OUT NUMBER OF STARS
System.out.println();
System.out.print("0-29 ");
for (int x = 0; x < starsfrom0to29; x++) {
System.out.print("*");
}
System.out.println();
System.out.print("30-39 ");
for (int x = 0; x < starsfrom30to39; x++) {
System.out.print("*");
}
System.out.println();
System.out.print("40-69 ");
for (int x = 0; x < starsfrom40to69; x++) {
System.out.print("*");
}
System.out.println();
System.out.print("70-100 ");
for (int x = 0; x < starsfrom70to100; x++) {
System.out.print("*");
}
System.out.println();
System.out.println();
}
This brings it out horizontally. For example: if I typed in numbers: 23,65,77,87,101 it will display:
0-29 *
30-39
40-69 *
70-100 **
However, I need to figure out how to print the output vertically. Basically, the stars (asterisks) should be in a vertical line and below the headings.
source to share
What you need is one loop that increments all 4 counters and prints 1-4 asterisks on one line at each iteration.
int i = 0;
int j = 0;
int k = 0;
int l = 0;
System.out.println("0-29 30-39 40-69 70-100");
while (i<starsfrom0to29 || j < starsfrom30to39 || k < starsfrom40to69 || l < starsfrom70to100) {
if (i<starsfrom0to29) {
System.out.print(" * ");
i++;
} else {
System.out.print(" ");
}
if (j<starsfrom30to39) {
System.out.print(" * ");
j++;
} else {
System.out.print(" ");
}
if (k<starsfrom40to69) {
System.out.print(" * ");
k++;
} else {
System.out.print(" ");
}
if (l<starsfrom70to100) {
System.out.println(" * ");
l++;
} else {
System.out.println("");
}
}
source to share
if this is the type of output you are looking for:
0-29
*
*
30-39
*
*
...
replace the code PRINTING OUT NUMBERS OF STARS
like this:
System.out.println();
System.out.println("0-29 ");
for (int x = 0; x < starsfrom0to29; x++) {
System.out.println("*");
}
System.out.println("30-39 ");
for (int x = 0; x < starsfrom30to39; x++) {
System.out.println("*");
}
System.out.println("40-69 ");
for (int x = 0; x < starsfrom40to69; x++) {
System.out.println("*");
}
System.out.println("70-100 ");
for (int x = 0; x < starsfrom70to100; x++) {
System.out.println("*");
}
source to share
If this is the result you are looking for: -
Type in the students marks. Type above 100 to show output.
12
30
31
17
60
43
89
97
100
0-29 30-39 40-69 70-100
* * * *
* * * *
Then here is the Java code: -
import java.util.*;
public class {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Type in the students marks. Type above 100 to show output.");
int marks;
int starsfrom0to29 = 0;
int starsfrom30to39 = 0;
int starsfrom40to69 = 0;
int starsfrom70to100 = 0;
int counter = 0;
int array[]={0,0,0,0};
int i=0;
marks = input.nextInt();
while (marks < 100) {
//PUTTING MARKS INTO CATEGOERY
if ((marks >= 0) && (marks <= 29)) {
starsfrom0to29++;
}
if ((marks >= 30) && (marks <= 39)) {
starsfrom30to39++;
}
if ((marks >= 40) && (marks <= 69)) {
starsfrom40to69++;
}
if ((marks >= 70) && (marks <= 100)) {
starsfrom70to100++;
}
if (marks < 100) {
counter++;
}
marks = input.nextInt();
}
//PRINTING OUT NUMBER OF STARS
System.out.println();
System.out.print("0-29\t30-39\t40-69\t70-100");
for (int x = 0; x < starsfrom0to29; x++) {
array[0]=array[0]+1;
}
System.out.println();
for (int x = 0; x < starsfrom30to39; x++) {
array[1]=array[1]+1;
}
System.out.println();
for (int x = 0; x < starsfrom40to69; x++) {
array[2]=array[2]+1;
}
System.out.println();
for (int x = 0; x < starsfrom70to100; x++) {
array[3]=array[3]+1;
}
for(i=0;i<4;i++)
{
if(array[0]>0)
{
System.out.print("*\t");
array[0]=array[0]-1;
}
else System.out.print("\t");
if(array[1]>0)
{
System.out.print("*\t");
array[1]=array[1]-1;
}
else System.out.print("\t");
if(array[2]>0)
{
System.out.print("*\t");
array[2]=array[2]-1;
}
else System.out.print("\t");
if(array[3]>0)
{
System.out.print("*\t");
array[3]=array[3]-1;
}
else System.out.print("\t");
System.out.print("\n");
}
}
}
Hope this helps!
source to share
Here is the code. Keep in mind that there are better ways to deal with the whole range of labels, but I don't know what you are allowed or not allowed to use in your assignment, so I stuck with simple things:
First, you will find the maximum number of stars you have. This is in order to know how many lines of stars to print. If you have no more than 3 stars, you need to print three lines. If you have 5, then 5, etc .:
// Find max
int max = 0;
if ( starsfrom0to29 > max ) {
max = starsfrom0to29;
}
if ( starsfrom30to39 > max ) {
max = starsfrom30to39;
}
if ( starsfrom40to69 > max ) {
max = starsfrom40to69;
}
if ( starsfrom70to100 > max ) {
max = starsfrom70to100;
}
You are now printing the title. The trick here is that each header must have the same width. Since the widest 70-100
, which is six characters wide, is 7 with space, then we have to skip every right aligner of the header in the 7 character box.
System.out.println( " 0-29 30-39 40-69 70-100");
We are now printing the columns. For each range, if there is still a star on that line (that is, if the number of stars is even greater than the line number), we print the star right-aligned in a 7-character box. If there are more stars in this column, we just print spaces.
for ( int i = 1; i <= max; i++ ) {
if ( starsfrom0to29 >= i ) {
System.out.print( " *");
} else {
System.out.print( " ");
}
if ( starsfrom30to39 >= i ) {
System.out.print( " *");
} else {
System.out.print( " ");
}
if ( starsfrom40to69 >= i ) {
System.out.print( " *");
} else {
System.out.print( " ");
}
if ( starsfrom70to100 >= i ) {
System.out.print( " *");
} else {
System.out.print( " ");
}
System.out.println();
}
Note that all commands System.out.print()
, so they don't print the end of the line and just add to the same line. For this reason, when we are done with a line, we need to use one System.out.println()
to give the end of the line for the entire line.
source to share