Star staircase confusion - java

OK I'm embarrassed I have to ask this but I'm stuck so here we go, please nudge me in the right direction.
We need to create this using a nested loop:
*
**
***
****
*****
Here's what I came up with.
int row,col;
for(row = 5; row >= 1; row--)
{
for (col = 0; col < 5 - row; col++)
println("");
for(col = 5; col >= row; col--)
print("*");
}
It ALMOST works but it prints spaces between each row and I cannot for the life of me figure out why.

Why not just one println(""); at the end of the loop instead of looping that statement? You only need the one new line per row of stars.

I think you only want to use one inner loop, to print each row of stars. Then print a newline at the end of each row:
int row, col;
for(row = 1; row <= 5; row++) {
for(col = 0; col < row; col++) {
print("*");
}
println("");
}

Why don't you do this:
for (int row = 0; row < 5; row++) {
for (int col = 0; col <= row; col++) {
System.out.print("*");
}
System.out.print("\n");
}

Why don't you simplify it?
int row, col;
for(row=0; row<5; row++) {
for(col=0;col<=row;col++) {
System.out.print("*");
}
System.out.println();
}
Print one row at a time, and then print the line-end
For the sake of only one I/O operation, a function may be a suitable approach:
public String starStarcase(int rows) {
int row, col;
String str="";
for(row = 0; row < rows; row++) {
for(col = 0; col <= row; col++) {
str += "*";
}
str += "\n";
}
return str;
}
To print the result:
System.out.println(starsStaircase(5));

You can try this, it should work:
for(int j = 0; j < row; j++){
for(int i = 1; i <= row; i++){
System.out.print(i < row-j ? " " : "#");
}
System.out.println("");
}

Related

Same Java code to print triangle using asterisk showing different results

The following codeparts are exactly the same, however, it prints a different result and I am unable to understand why.
//First Part
final int MAX_ROWS = 10;
for (int row = 1; row <= MAX_ROWS; row++)
{
for (int star = 1; star <= row; star++)
System.out.println("*");
System.out.println();
}
System.out.println();
//Second Part
for (int row = 1; row <= MAX_ROWS; row++)
{
for (int star = 1; star <= row; star++)
System.out.print ("*");
System.out.println();
}
Since you are using print() in the second part of your code you should insert \n to wrap text, Because println() will do it by default.
Your code to work should be like :
final int MAX_ROWS = 10;
for (int row = 1; row <= MAX_ROWS; row++){
for (int star = 1; star <= row; star++) {
System.out.println("*");
}
System.out.println();
}
System.out.println();
for (int row = 1; row <= MAX_ROWS; row++){
for (int star = 1; star <= row; star++) {
System.out.print("*\n");
}
System.out.println();
}
Or like this:
final int MAX_ROWS = 10;
for (int row = 1; row <= MAX_ROWS; row++) {
for (int star = 1; star <= row; star++) {
System.out.println("*");
}
System.out.println();
}
System.out.println();
for (int row = 1; row <= MAX_ROWS; row++) {
for (int star = 1; star <= row; star++) {
System.out.println("*");
}
System.out.println();
}

How do I change the numbers on an array of buttons?

My buttons are currently displaying numbers 1-9, but I don't know how to display the numbers 9-1.
I already using different numbers in my for loops, but it still did not work for me.
for (int row=0; row<3; row++) {
for (int col = 1; col<4; col++) {
int pieces = row*3 + col;
String count = Integer.toString(pieces);
Button button = new Button(count);
GridPane.setRowIndex(button, row);
GridPane.setColumnIndex(button, col);
keypad.getChildren().add(button);
button.setMinSize(80, 80);
}
}
Just subtract the calculated number from the maximum number to count backwards:
int rows = 3;
int cols = 3;
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
int pieces = rows * cols - (row * 3 + col);
String count = Integer.toString(pieces);
// ...
}
}
Alternatively you can reverse the both for loops:
for (int row = 2; row >= 0; row--) {
for (int col = 3; col > 0; col--) {
int pieces = row * 3 + col;
String count = Integer.toString(pieces);
// ...
}
}

Get Average number of blocks for game

I'm new to coding in Java. I need to compute the average number of blocks per column on a board. The below is to count the total number of blocks within all columns on a board and then divides by the number of columns.
this is what I have so far but I think it's not right. i feel like I'm missing something.
public int getAverageColumnBlocks(Board board)
{
int avgColumnBlock = 0;
for (int col = 0; col < Board.WIDTH; col++)
{
for (int row = 0; row < Board.HEIGHT; row++)
{
if(board.isBlockAt(col, row))
{
avgColumnBlock++;
}
}
}
return avgColumnBlock;
}
public int getAverageColumnBlocks(Board board)
{
int total = 0;
for (int col = 0; col < Board.WIDTH; col++)
{
for (int row = 0; row < Board.HEIGHT; row++)
{
if(board.isBlockAt(col, row))
{
total++;
}
}
}
return total/Board.WIDTH;
}

Multiplication table with 2d array

I need to make a multiplication table that shows 1 * 1 up to 12 * 12. I have this working but it needs to be in 13 columns in a format that looks like the diagram below, really appreciate any help.
1 2 3 4 5 6 7 8 9 10 11 12
1 1 2 3 4 5 ...
2 2 4 6 8 10 ....
3
4
5
6
...
Code so far:
public class timetable {
public static void main(String[] args) {
int[][] table = new int[12][12];
for (int row=0; row<12; row++){
for (int col=0; col<12; col++){
table[row][col] = (row+1) * (col+1);
}
}
for (int row = 0; row < table.length; row++) {
for (int col = 0; col < table[row].length; col++) {
System.out.printf("%6d", table[row][col]);
}
System.out.println();
}
}
}
Print column headings before printing the table, and print row headings at the start of each row. You can use the code below.
int[][] table = new int[12][12];
for (int row=0; row<12; row++){
for (int col=0; col<12; col++){
table[row][col] = (row+1) * (col+1);
}
}
// Print column headings
System.out.printf("%6s", "");
for (int col = 0; col < table[0].length; col++) {
System.out.printf("%6d", col+1);
}
System.out.println();
for (int row = 0; row < table.length; row++) {
// Print row headings
System.out.printf("%6d", row+1);
for (int col = 0; col < table[row].length; col++) {
System.out.printf("%6d", table[row][col]);
}
System.out.println();
}
This only prints 9x9 timetable, if you need to change it 12x12, then just change the numbers in the code from "9" to "12", and add more "----" lines in the system output to match it
This includes " * |" and "----" ...
Thought this might be helpful for anyone else
Output:
9x9 Timetable
public class timetable2DArray
{
public static void main(String[] args)
{
int[][] table = new int[9][9];
for (int row=0; row<9; row++)
{
for (int col=0; col<9; col++)
{
table[row][col] = (row+1) * (col+1);
}
}
// Print column headings
System.out.print(" * |");
for (int col = 0; col < table[0].length; col++)
{
System.out.printf("%4d", col+1);
}
System.out.println("");
System.out.println("------------------------------------------");
for (int row = 0; row < table.length; row++)
{
// Print row headings
System.out.printf("%4d |", row+1);
for (int col = 0; col < table[row].length; col++)
{
System.out.printf("%4d", table[row][col]);
}
System.out.println();
}
}
}
int [][] A = new int[5][5];
int [][] B = new int[5][5];
for (int row = 0; row < A.length; row++) {
System.out.println();
for (int col = 0; col < A.length; col++) {
B[row][col] = (row+1)*(col+1);
System.out.print("\t");
System.out.printf("%2d", B[row][col]);
}
}
You could implement a timesTable() method. Here's my code, modify it anyway you would like.
//main driver
public static void main(String[] args) {
int[][] data; //declaration
data = timesTable(4,6); //initialisation
for (int row = 0; row < data.length ; row++)
{
for (int column = 0; column < data[row].length; column++)
{
System.out.printf("%2d ",data[row][column]);
}
System.out.println();
}
}
//method
public static int[][] timesTable(int r, int c)
{
int [][] arr = new int[r][c];
for (int row = 0; row < arr.length ; row++)
{
for (int column = 0; column < arr[row].length; column++)
{
arr[row][column] = (row+1)*(column+1);
}
}
return arr;
}

Printing integers with for loops

I'm having a strange issue trying to print a 9x9 grid of integers. When I try this code in the main method:
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 0; col++) {
System.out.format( "%d ", entries[row][col] );
}
System.out.print("\n");
}
The output is just a bunch of spaces and newlines, without the actual integers. I've tested 'entries' to make sure it actually contains the correct values (and it does). The weird thing is, when I try the following code also in the main method:
System.out.format("%d ", entries[0][0]);
It works. For some reason the for loop is messing up the output. Any ideas?
You did a mistake in the inner for loop:
for (int col = 0; col < 0; col++)
This wont do any iteration because zero is equals zero.
I think this is what you want:
for (int col = 0; col < 9; col++)
Problem is with the condition in second for loop
for (int col = 0; col < 0; col++) {
It is always false and hence never gets executed.
You should instead use:
for (int col = 0; col < 9; col++) {
This condition is never met:
for (int col = 0; col < 0; col++) {
so you can just simplify it by doing:
for (int row = 0; row < 9; row++) {
System.out.format( "%d ", entries[row][0] );
System.out.print("\n");
}
To print a two-dimensional array , you have to print each element in the array using a loop like the following:
int[][] matrix = new int[9][9];//9*9 grid container
for(int row=0 ; row < matrix.length ;row++){
for(int column= 0 ; column < matrix[row].length;column++){
System.out.print(matrix[row][column] + "");
}
System.out.println();
}

Categories