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

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);
// ...
}
}

Related

Save bidimensional Array

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;
}

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;
}

How do I generate 2d array table with non-repeating numbers?

How should I generate a 4X4 2D array table with every element different?
Here's my code:
public class Game {
public static void main(String[] args) {
int gameboard[][] = new int[4][4];
for (int row=0; row < gameboard.length; row++) {
for (int col=0; col < gameboard[row].length; col++) {
gameboard[row][col] = ((int)(1+Math.random() * 16));
System.out.printf("%-4d",gameboard[row][col]);
}
System.out.println();
}
}
}
Solution 1 :
public static void main(String[] s1) throws Exception {
int gameboard[][] = new int[4][4];
Set<Integer> mySet = new HashSet<>();
for (int row = 0; row < gameboard.length; row++) {
for (int col = 0; col < gameboard[row].length; col++) {
int randNum = (int) (1 + Math.random() * 16);
while (mySet.contains(randNum)) {
randNum = (int) (1 + Math.random() * 16);
}
mySet.add(randNum);
gameboard[row][col] = randNum;
System.out.printf("%-4d", gameboard[row][col]);
}
System.out.println();
}
}
Here in each iteration we check if the random number generated is present in the set. If it is present then we loop until we get a different random number that is not present in the set.
Solution 2 :
List<Integer> myList = IntStream.range(1, 17).boxed().collect(Collectors.toList());
Collections.shuffle(myList);
for (int row = 0; row < gameboard.length; row++) {
for (int col = 0; col < gameboard[row].length; col++) {
gameboard[row][col] = myList.get(row * gameboard.length + col);
System.out.printf("%-4d", gameboard[row][col]);
}
System.out.println();
}
Here we generate a list of numbers and then shuffle it using Collections.shuffle(). Now we iterate over the multi-dimensional array and assign the values of the list to the array.
You just need to put a control after like:
int a == gameboard[row][col];
for (int row=0; row < gameboard.length; row++) {
for (int col=0; col < gameboard[row].length; col++) {
gameboard[row][col] = ((int)(1+Math.random() * 16));
if(gameboard[row][col] == a){
col = col - 1;
}
}
}
You could use a Set first to get no duplicates, and then a List to access them easily
Set<Integer> set = new LinkedHashSet<>();
int gameboard[][] = new int[4][4];
while(set.size() != 4*4){
set.add((int)(1+Math.random() * 16));
}
List<Integer> list = new ArrayList<>(set);
for (int row=0; row < gameboard.length; row++) {
for (int col=0; col < gameboard[row].length; col++) {
gameboard[row][col] = list.get(row*gameboard.length + col);
System.out.printf("%-4d",gameboard[row][col]);
}
System.out.println();
}
}

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;
}

Star staircase confusion

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("");
}

Categories