The first piece of code results in only one column and bunch of rows. The second piece of code results in 5x5 board. What is wrong with the first piece of code. It's probably something stupid and simple, but I couldn't find it.
int size = 5; // size of the board
for (int row = 1; row <= size; ++row) {
for (int col = 1; col <= size; ++col) {
System.out.println("# ");
}
System.out.println();
}
int height = 5;
int width = 5;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
System.out.print("# ");
}
System.out.println();
}
int size = 5;
for(int row=1;row<=size;++row)
{
for (int col = 1; col <= size; ++col)
{
System.out.print("# ");
}
System.out.println();
}
int height = 5;
int width = 5;
for(int i=0;i<height;i++)
{
for (int j = 0; j < width; j++)
{
System.out.print("# ");
}
System.out.println();
}
Related
I am doing a Bingo program. I need to save the bingoCard (bidimensional array), because every time I execute a new move the card is different. And it need to be the same.
So I need to know how to save the card. I have a global variable (private static int[][]bingoCard=new int[3][9];) this is the code
public static int[][] bingoCard(){
int numRows = 3;
int numCols = 9;
int randomNumbersPerRow = 5;
int randomNumberBound = 90;
Random random = new Random();
bingoCard = new int[numRows][numCols];
for (int row = 0; row < numRows; row++) {
for (int col = 0; col < numCols; col++) {
bingoCard[row][col] = 0;
}
for (int i = 0; i < randomNumbersPerRow; i++) {
int indexToAssign = random.nextInt(numCols);
while (bingoCard[row][indexToAssign] != 0) {
indexToAssign = random.nextInt(numCols);
}
int numToAssign = random.nextInt(randomNumberBound)+1;
bingoCard[row][indexToAssign] = numToAssign;
}
}
for (int row = 0; row < numRows; row++) {
for (int col = 0; col < numCols; col++) {
System.out.printf("%2d ", bingoCard[row][col]);
}
System.out.println();
}
return bingoCard;
}
I have to create a Java program for Conway's Game Of Life in procedural manner and I'm only one step away from finishing - all I have left to do is figure out how to make the output change itself(something like a GIF). Instead, my program outputs all the results one by one. I'm figuring it should be some kind of a loop, but I'm not sure. Here's the code:
import java.util.Random;
import java.util.Scanner;
class gameOfLife {
public static void main(String[] args) {
//User input
Scanner in = new Scanner(System.in);
System.out.println("How many rows?");
int rows = in.nextInt();
System.out.println("How many columns?");
int cols = in.nextInt();
//Declaring variables and grids
int[][] grid = new int[rows][cols];
int[][] nextGrid = new int[rows][cols];
int[][] temp = new int [rows][cols];
//Initializing first generation
initiateGrid(grid);
printGameBoard(grid);
//Looping through 10 generations
for (int x = 0; x < 20; x++) {
applyTheRules(grid, rows, cols, nextGrid);
temp = nextGrid;
nextGrid = grid;
grid = temp;
printGameBoard(grid);
}
}
//Initiating first generation grid randomly
static void initiateGrid(int[][] grid) {
Random r = new Random();
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
grid[i][j] = r.nextInt(2);
}
}
}
//Printing out the game board
static void printGameBoard(int[][] grid) {
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
if (grid[i][j] == 0)
System.out.print(" . ");
else
System.out.print(" ■ ");
}
System.out.println();
}
System.out.println();
}
//Applying the rules of the game
static void applyTheRules(int [][] grid, int rows, int cols, int [][] nextGrid) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
int count = 0;
if(i-1>=0 && i+1<grid.length && j-1>=0 && j+1<grid[i].length) {
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
count += grid[i + x][j + y];
}
}
count -= grid[i][j];
} else{
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
count += 0;
}
}
}
//Alive cell becomes dead, if there are more than
//3 or less than 2 neighbouring cells
if((grid[i][j]==1)&&(count<2)||(count>3))
nextGrid[i][j]=0;
//Dead cell becomes alive if there are exactly 3 neighbouring cells
else if((grid[i][j]==0)&&(count==3))
nextGrid[i][j]=1;
//State stays the same
else
nextGrid[i][j]=grid[i][j];
}
}
}
}
I am trying to print right aligned triangles using asterisks. So far I have a program that asks a user for a shape and then a size, then prints out a triangle with those dimensions. So far I have a program for left aligned triangles. How do I make it so it's right aligned? Here is my code so far.
if (shape.equals("triangle")) {
System.out.print("size?");
int size = sc.nextInt();
for (int i = 0; i <= size; i = i + 1) {
for (int j = 0; j < i; j = j+ 1) {
System.out.print("*");
}
System.out.println();
}
...
}
Try adding some padding on the left:
System.out.print("size?");
int size = sc.nextInt();
// input 5
for (int i = 0; i <= size; i = i + 1) {
for (int j = 0; j < size-i; ++j) {
System.out.print(" ");
}
for (int j = 0; j < i; ++j) {
System.out.print("*");
}
System.out.println();
}
*
**
***
****
*****
Demo
Try this, this will add left padding and as result, you'll get a right-aligned triangle:
int star = 1, space = size - 1;
while (n > 0) {
for (int i = 0; i < space; i++) {
System.out.print(" ");
}
for (int i = 0; i < star; i++) {
System.out.print("#");
}
star = star + 1;
space = space - 1;
--size;
System.out.println("");
}
public static void main(String[] args) {
int rows, cols, j, i;
Scanner sc = new Scanner(System.in);
System.out.println("enter no of rows and columns respectively");
rows = sc.nextInt();
cols = sc.nextInt();
int[][] matrix = new int[rows+1][cols+1];
System.out.println("enter elements");
for(i = 0; i < rows; i++)
{
for(j = 0; j < cols; j++)
{
matrix[i][j] = sc.nextInt();
}
}
System.out.println("after sum");
int grandtotal = 0;
for(i = 0; i < rows; i++)
{
int rowsum = 0;
int colsum = 0;
for(j = 0; j < cols; j++)
{
rowsum += matrix[i][j];
colsum += matrix[j][i];
}
matrix[i][j] = rowsum;
grandtotal += rowsum;
matrix[j][i] = colsum;
}
j = cols;
matrix[i][j] = grandtotal;
The above code is working fine, but my question is: using this logic, how can I find the grandtotal for a matrix if the number of rows and columns are not the same? And tell me if there are any flaws with my current code.
There is no flaw in your code.Now coming to the point when the number of rows and columns are different.
The statement
matrix[j][i] = colsum;
now would give incorrect result because now number of rows and number of columns are different.
This can be solved very easily by just changing the statement
colsum += matrix[j][i];
to:
matrix[rows][j] += matrix[i][j];
and now you do not need the statement
matrix[j][i] = colsum;
So with these simple modifications, the code for different number of columns and rows would be:
for( i = 0;i < rows;i++)
{
int rowsum=0;
for( j = 0;j < cols;j++)
{
rowsum += matrix[i][j];
matrix[rows][j] += matrix[i][j];
}
matrix[i][j] = rowsum;
grandtotal += rowsum;
}
Keep Rest of the code same.
I am trying to print a shape based on input; the shape is an "x". The inputs must be positive odd ints, and an arbitrary brush character. I have the code completed for the user input, but I need help with the code that actually prints the shape. Here is what I have so far:
public class TestProgram {
public static void main(String[] args) {
int height = 5;//Any positive odd int but 5 does not work correctly. Not sure what is going on.
char brush = '*';
for (int row = 0; row < height/2; row++) {
for (int i = row; i > 0; i--) {
System.out.print(" ");
}
System.out.print(brush);
for (int i = (height/2); i >= 2*row; i--) {
System.out.print(" ");
}
System.out.print(brush);
System.out.print("\n");
}
for (int row = 1; row < (height/2)+1; row++ ) {
System.out.print(" ");
}
System.out.print(brush);
System.out.print("\n");
for (int row = (height/2)-1; row >= 0; row--) {
for (int i = row; i > 0; i--) {
System.out.print(" ");
}
System.out.print(brush);
for (int i = (height/2); i >= 2*row; i--) {
System.out.print(" ");
}
System.out.print(brush);
System.out.print("\n");
}
for (int row = 1; row < (height/2)+1; row++ ) {
System.out.print(" ");
}
}
}
I would start with a routine to repeat your char n times. Something like
private static String repeat(char ch, int count) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < count; i++) {
sb.append(ch);
}
return sb.toString();
}
Then, I would prefer to build the String with a StringBuilder and the repeat routine. Like
int height = 5;
char brush = '*';
char space = ' ';
int half = height / 2;
StringBuilder sb = new StringBuilder();
for (int row = 0; row < half; row++) {
int cols = height - ((row + 1) / 2);
sb.append(repeat(space, row)).append(brush);
sb.append(repeat(space, cols)).append(brush);
sb.append(System.lineSeparator());
}
sb.append(repeat(space, half)).append(brush);
sb.append(System.lineSeparator());
for (int row = half - 1; row >= 0; row--) {
int cols = height - ((row + 1) / 2);
sb.append(repeat(space, row)).append(brush);
sb.append(repeat(space, cols)).append(brush);
sb.append(System.lineSeparator());
}
System.out.println(sb.toString());
Since you haven't learned about buffered io yet, that could also be expressed as
int height = 5;
char brush = '*';
char space = ' ';
int half = height / 2;
for (int row = 0; row < half; row++) {
int cols = height - ((row + 1) * 2);
System.out.print(repeat(space, row));
System.out.print(brush);
System.out.print(repeat(space, cols));
System.out.println(brush);
}
System.out.print(repeat(space, height / 2));
System.out.println(brush);
for (int row = (height / 2) - 1; row >= 0; row--) {
int cols = height - ((row + 1) * 2);
System.out.print(repeat(space, row));
System.out.print(brush);
System.out.print(repeat(space, cols));
System.out.println(brush);
}
And if you really don't know how to create a method,
int height = 5;
char brush = '*';
char space = ' ';
int half = height / 2;
for (int row = 0; row < half; row++) {
int cols = height - ((row + 1) * 2);
for (int t = 0; t < row; t++) {
System.out.print(space);
}
System.out.print(brush);
for (int t = 0; t < cols; t++) {
System.out.print(space);
}
System.out.println(brush);
}
for (int t = 0; t < height / 2; t++) {
System.out.print(space);
}
System.out.println(brush);
for (int row = (height / 2) - 1; row >= 0; row--) {
int cols = height - ((row + 1) * 2);
for (int t = 0; t < row; t++) {
System.out.print(space);
}
System.out.print(brush);
for (int t = 0; t < cols; t++) {
System.out.print(space);
}
System.out.println(brush);
}