How to print out the output vertically - java

Basically, the user enters student grades and the program will display and arrange them into sections (0-29 marks, 30-39 marks, 40-69 marks and 70-100 marks). If the user enters anything higher than 100 then the program terminates and outputs the results.
I'm just having trouble printing out 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 prints it out horizontally. E.g: If I typed in the numbers: 23,65,77,87,101 it would display:
0-29 *
30-39
40-69 *
70-100 **
However, I need to find out how to print the output vertically. Basically the stars (asterisks) should be in a vertical line and are below the headlines.

What you need is a single loop that increments all 4 counters, and prints 1-4 asterisks in the same line in 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("");
}
}

if this is the type of output you are looking for:
0-29
*
*
30-39
*
*
...
swap your PRINTING OUT NUMBERS OF STARS code with the following:
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("*");
}

If this the output 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!!

Here is the code. Mind you, there are better ways to handle the whole marks range thing, but I don't know what you are allowed or not allowed to use in your assignment, so I stuck to simple things:
First, you find the maximum number of stars you have. This is in order to know how many rows of stars to print. If you have at most 3 stars, then you'll need to print three lines. If you have 5, then 5, and so on:
// 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;
}
Now you print the header. The trick here is that each heading has to be the same width. Since the widest is 70-100, which is six characters wide, 7 with the space, then we have to space each header right-align in a 7 character field.
System.out.println( " 0-29 30-39 40-69 70-100");
Now we print the columns. For each range, if there is still a star to be printed at this row (that is, if the number of stars is still greater than the row number), we print the star, right aligned in a 7-character field. If there are no more stars in this column, we just print the 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 of the commands are System.out.print() so they don't print end-of-line and just add to the same line. For this reason, when we are finished with a row we need to use one System.out.println() to give the end-of-line for the whole row.

Related

Drawing stars up and down

I want to get this result, where _ space characters :
*___*
_*_*_
__*__
public static void main(String args[]) {
int level = 2; // quantity line
int stars = 5; //quantity drawing stars
for(int i = 1;i <= level ; i++){
for(int j =1 ;j <= i; j++){
System.out.print(" ");
}
System.out.println("*");
}
}
So far, I have drawn,
*__
_*_
__*
And I don't know how to draw up ?
Steps to solve these type of questions:
consider * as 1 and spaces as 0. Now i need this output :
10001
01010
00100
first 1 is appearing according to row no. Row 0 - 1 at Col 0, Row 1 - 1 at Col 1
Second 1 is appearing at (total columns-current Row index-1)
print 1 for above two condition otherwise zero.
int rows=3; // quantity line
int cols=5; //quantity drawing stars
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
int k=cols-i-1;
if(i==j || j==k)
System.out.print("*");
else System.out.print(" ");
}
System.out.println();
}
int size=10; // Only one parameter is required which is quantity drawing stars
int length= size%2==0?size/2:size/2+1; // in case of odd one more line need to be print at last on which one Asteric appears.
for (int i = 0; i < length; i++) {
for (int j = 0; j < size; j++) {
if (i == j || i + j == size - 1) { //condition for diagonals
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
Output :
when size = 10;
* *
* *
* *
* *
**
when size = 11
* *
* *
* *
* *
* *
*
You can try below code and output is what you want..
for(int i=3;i>=1;i--)
{
for(int j=i;j<3;j++)
{
System.out.print(" ");
}
for(int j=1;j<=(2*i-1);j++)
{
if(j==1 || j==(2*i-1))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println("");
}

(Java Number Pyramid) How do I get my numbers to have more spaces between them while lining up accurately when it is an integer is > 9?

I am having difficulty getting the desired output. I know there are problems that are similar to mine that is already posted, but I find it hard to relate my code to their solutions without a massive overhaul.
The solution for my class assignment:
Supposed to continue until every direction of pyramid equals 1
My second method "spaces" is redundant and I am not sure why. Any help would be appreciated.
Blockq
public static void main(String[] args) {
numPar();
spaces();
}
private static void spaces() {
int x = 0;
if(x > 0 && x < 10) {
System.out.print(" ");
} else if (x > 10 && x < 99) {
System.out.print(" ");
} else if (x > 99) {
System.out.print(" ");
}
}
private static void numPar() {
int spaces = 14;
for(int i = 0; i<= 7; i++) {
for(int u = 0; u<spaces; u++) {
System.out.print(" ");
}
spaces--;
spaces--;
for(int j = 0 ; j <i ; j++) {
System.out.print(""+ (int) Math.pow(2,j)+" ");
}
for(int k = i ; k >=0 ; k--) {
System.out.print(""+ (int) Math.pow(2,k)+" ");
}
System.out.println("");
}
}
}
I made every number take 3 places using String.format("%3s", (int) Math.pow(2, j)). You can make it dynamic by replacing the number 3 here with the length of the largest number you'll print. I also changed the number of spaces in your print statements. Here is the full code that prints an evenly spaced pyramid:-
public static void main(String[] args) {
numPar();
spaces();
}
private static void spaces() {
int x = 0;
if (x > 0 && x < 10) {
System.out.print(" ");
} else if (x > 10 && x < 99) {
System.out.print(" ");
} else if (x > 99) {
System.out.print(" ");
}
}
private static void numPar() {
int spaces = 14;
for (int i = 0; i <= 7; i++) {
for (int u = 0; u < spaces; u++) {
System.out.print(" ");
}
spaces--;
spaces--;
for (int j = 0; j < i; j++) {
System.out.print("" + String.format("%3s", (int) Math.pow(2, j)) + " ");
}
for (int k = i; k >= 0; k--) {
System.out.print("" + String.format("%3s", (int) Math.pow(2, k)) + " ");
}
System.out.println("");
}
}
String.format explanation:-
String.format("%3s", str) will print the string str, padding it with spaces, to make the total length 3 if it's less than 3. Note that you can write anything instead of 3 - I used 3 because your biggest number was of length 3.
So "A" will be printed as "_ _ A" (2 spaces), and "Ab" will be printed as "_ Ab" (1 space).
I just replaced str with your Math.pow(2, j).

3 Dimensional Tic Tac Toe prints only one dimension

I'm starting on a program to make a 3D tic tac toe game, and am running into a few problems. The game is supposed to be 4x4x4. I'm not sure why the multiple dimensions are not being printed, and I'm also not sure why my entered value isn't appearing on the one level that shows. Code below. Any help would be awesome. Thanks!
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int draw = 64;
int n = 0;
int board[][][] = new int[4][4][4];
while (n < draw) {
System.out.println("Type your move as one three digit number(lrc)");
int input = scan.nextInt();
int level = input / 100;
int row = input % 100 / 10;
int column = input % 10;
System.out.println(level);
System.out.println(row);
System.out.println(column);
board[level][row][column] = 1;
for (int i = 3; i >= 0; i--) { //level
for (int h = 3; h >= 0; h--) { //row
for (int temp = h; temp >= 0; temp--) {
System.out.print(" ");
}
System.out.print(i + "" + h + " ");
for (int j = 0; j <= 3; j++) { //column
if (board[i][h][j] == 0) {
System.out.print("_ ");
}
if (board[i][h][j] == 1) {
System.out.print("X ");
n++;
}
if (board[i][h][j] == 5) {
System.out.print("O ");
n++;
}
}
System.out.println();
}
System.out.println();
}
System.out.println("\n 0 1 2 3");
}
}
}
In this line
for (int temp = h; h > 0; h--) {
you are decrementing h rather than temp so in the loops below the value of h will always be 1
After this change
change for (int temp = h; temp >= 0; temp--) {
System.out.println(" ");
}
to simply System.out.println(" ");

How can i print my array and my histogram on the same line?

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Histogram
{
public static void main(String[] args) throws FileNotFoundException
{
Row[] numbers = {
new Row("1 - 10"), new Row("11 - 20"), new Row("21 - 30")
new Row("31 - 40"), new Row("41 - 50"), new Row("51 - 60")
new Row("61 - 70"), new Row("71 - 80"), new Row("81 - 90"),
new Row("91 - 100")
};
for(Row number : numbers)
System.out.print(number);
Counter section = new Counter();
section.StarCounter();
}
it prints out something like this:
1 - 10|
11 - 20|
21 - 30|
*****************
*********
***
ect.
I would like it to print out something like:
1 - 10|*****************
11 - 20|*********
21 - 30|***
ect.
thanks in advance for any pointers, I'm probably just overlooking the obvious.
Counter class:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Counter
{
public void StarCounter() throws FileNotFoundException
{
File file = new File("alotofnumbers.txt");
Scanner scan = new Scanner(file);
int[] integers = new int[1000];
int start = 0;
int count1 = 0;
int count2 = 0;
int count3 = 0;
int count4 = 0;
int count5 = 0;
int count6 = 0;
int count7 = 0;
int count8 = 0;
int count9 = 0;
int count10 = 0;
while (scan.hasNextInt())
{
integers[start++] = scan.nextInt();
}
for (int input : integers)
{
if(input <= 10)
{
count1++;
}
else if(input > 10 && input <= 20)
{
count2++;
}
else if(input > 20 && input <= 30)
{
count3++;
}
else if (input > 30 && input <= 40)
{
count4++;
}
else if (input > 40 && input <= 50)
{
count5++;
}
else if (input > 50 && input <= 60)
{
count6++;
}
else if (input > 60 && input <= 70)
{
count7++;
}
else if (input > 70 && input <= 80)
{
count8++;
}
else if (input > 80 && input <= 90)
{
count9++;
}
else if (input > 90 && input <= 100)
{
count10++;
}
}
double counted1 = count1 / 2.7;
double counted2 = count2 / 2.7;
double counted3 = count3 / 2.7;
double counted4 = count4 / 2.7;
double counted5 = count5 / 2.7;
double counted6 = count6 / 2.7;
double counted7 = count7 / 2.7;
double counted8 = count8 / 2.7;
double counted9 = count9 / 2.7;
double counted10 = count10 / 2.7;
for(int star = 0; star <= counted1 ; star++)
{
System.out.print("*");
}
System.out.println();
for(int star = 0; star <= counted2 ; star++)
{
System.out.print("*");
}
System.out.println();
for(int star = 0; star <= counted3 ; star++)
{
System.out.print("*");
}
System.out.println();
for(int star = 0; star <= counted4 ; star++)
{
System.out.print("*");
}
System.out.println();
for(int star = 0; star <= counted5 ; star++)
{
System.out.print("*");
}
System.out.println();
for(int star = 0; star <= counted6 ; star++)
{
System.out.print("*");
}
System.out.println();
for(int star = 0; star <= counted7 ; star++)
{
System.out.print("*");
}
System.out.println();
for(int star = 0; star <= counted8 ; star++)
{
System.out.print("*");
}
System.out.println();
for(int star = 0; star <= counted9 ; star++)
{
System.out.print("*");
}
System.out.println();
for(int star = 0; star <= counted10 ; star++)
{
System.out.print("*");
}
System.out.println();
}
}
Apparently my post has to much code and I don't really have much more info to give so it wants me to type something random up.
public class Row
{
private String rows;
public Row(String colums)
{
rows = colums ;
}
public String toString()
{
Counter section = new Counter();
return rows + "|" + "\n";
}
public void setRow(String colums)
{
rows = colums;
}
public String getRow()
{
return rows;
}
}
Here is a solution to your problem. The problem was that you were printing all of your Row data first and then all of your Integer data. You need to print them both at once. Not one after the other. Also just fyi, there is about a million different ways to make your initial code better. I would try and sit down and think of a better way to do this so you can learn (after you turn in your assignment so you are not rushed, we can all help. This is a good start though.)
First I erased the printing of the Row data, and added a constructor to your Counter class to take in your Row data.
Row[] numbers = {
new Row("1 - 10"), new Row("11 - 20"), new Row("21 - 30"),
new Row("31 - 40"), new Row("41 - 50"), new Row("51 - 60"),
new Row("61 - 70"), new Row("71 - 80"), new Row("81 - 90"),
new Row("91 - 100")
};
Counter section = new Counter(numbers);
section.StarCounter();
New Constructor:
public class Counter
{
Row[] numbers;
public Counter(Row[] numbers) {
this.numbers = numbers;
}
//Rest of your methods
}
Then printed the Row data when you print your Integer data.
System.out.print(numbers[0]); //new line
for(int star = 0; star <= counted1 ; star++)
{
System.out.print("*");
}
System.out.println();
System.out.print(numbers[1]); //new line
for(int star = 0; star <= counted2 ; star++)
{
System.out.print("*");
}
//Do the same for the rest
Hope this helps!
Oh also got rid of the newline in your Row class.
public String toString()
{
return rows + "|";
}

Print a Z shape pyramid using * stars

I am trying to write a program that outputs a Z pattern that is n number of * across the top, bottom, and connecting line using for loops.
Example:
Enter a number: 6
******
*
*
*
*
******
This is my current code, it's producing a half pyramid upside down.
import java.util.*;
public class ZShape {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = input.nextInt();
for (int x = 0; x <= n; x++) {
for (int y = n; y >= 1; y--) {
if (y > x) {
System.out.print("* ");
}
else
System.out.print(" ");
}
System.out.println();
}
}
}
This is the logic in the following code:
Loop over each row of the output (so from 0 to n excluded so that we have n rows)
Loop over each column of the output (so from 0 to n excluded so that we have n columns)
We need to print a * only when it is the first row (x == 0) or the last row (x == n - 1) or the column is in the opposite diagonal (column == n - 1 - row)
Code:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = input.nextInt();
for (int row = 0; row < n; row++) {
for (int column = 0; column < n; column++) {
if (row == 0 || row == n - 1 || column == n - 1 - row) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Sample output for n = 6:
******
*
*
*
*
******
(Note that this output has trailing white-spaces for each row, you did not specify whether they should be included, but it is easy to remove them by adding another check).
How about using three loops instead?
for (int x = 0; x < n; x++) {
System.out.print("*");
}
System.out.println();
for (int x = n-3; x >= 0; x--) {
for (int y = x; y >= 0; y--) {
System.out.print(" ");
}
System.out.println("*");
}
for (int x = 0; x < n; x++) {
System.out.print("*");
}
public class Star {
public static void main(String[] args) {
for (int i = 0; i <=4; i++) {
for (int j = 0; j <=4; j++)
{
if (i==4 || (i+j)==4 || i==0)
{
System.out.print(" * ");
}
else
{
System.out.print(" ");
}
}
System.out.println(" ");
}
}
}
Here is a logic to print Z shape:
when i = 1, then the First line will print only "#"
when i = n, then the Last line will print in same way by "#" only
when i = j, that means it will execute one diagonal line by "#" only
Code:
public static void main(String[] args) {
Scanner scr = new Scanner(System.in);
int n = scr.nextInt();
for (int i = 1; i <= n; i++) {
for(int j = n; j >= 1; j--) {
if (i == 1 || i == n || i == j) {
System.out.print("# ");
}
else {
System.out.print(" ");
}
}
System.out.println();
}
}

Categories

Resources