I'm a total begginer in java and I'm having some trouble at understanding how things work... could someone explain to me why the computer understands "i" as horizontal row and "j" as vertical row since both "for" loops are the same, just with different variables?
public class DiagonalStar {
public static void printSquareStar(int number) {
if (number < 5) {
System.out.println("Invalid Value");
} else {
for (int i = 1; i <= number; i++) {
for (int j = 1; j <= number; j++) {
if ((i == 1) || (j == 1) || (i == number) || (j == number) || (i == j) || (j == number - i + 1)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
}
It's actually not a matter of vertical or horizontal, is based on the order the lines of code are executed.
For example:
for(int n=0;n<10;n++)
{
System.out.println(n);
}
Would print
0
1
2
3
4
5
6
7
8
9
But if you put another loop inside it, it will execute it before passing to the next loop of n.
for(int n=0;n<10;n++)
{
for(int m=10;m<15;m++)
{
System.out.println(n + "." + m);
}
}
That would print
0.10
0.11
0.12
0.13
0.14
0.15
All that before getting to 1.10, 1.11, etc...
So when you're printing the "*" you're just looping in that logic, and whenever you complete the inner for you use println (that prints the next line)
I would suggest messing with the variables, see what the program outputs when you switch i with j or when you change the conditions.
Good luck!
You have nested one for loop into another. This means that for each value of i you are going thru all values of j.
After inner loop you have System.out.println() and this moves you to another row.
println() - prints text with a newline
print() - just prints text
System.out.print prints in a row, while System.out.println prints in a column
Hey how would I print a pattern then print the reverse pattern next to that pattern? like this:
1 1 2 3 4 5
1 2 1 2 3 4
1 2 3 1 2 3
1 2 3 4 1 2
1 2 3 4 5 1
I know how to print both the patterns I just can't find out how to print the second pattern next to the first one.
package exc3;
public class Exc3 {
public static void main(String[] args) {
int row = 1;
int i = 0;
for (i=1; i<=row; i++){
System.out.print(i + " ");
if (i == row){
System.out.println();
i = 0;
row++;
}
if (row > 5)
break;
}
}
that's the code I have for making the pattern but I don't think I need help with that just with putting the second pattern next to the first I have no idea how to do that
Here is the code that you want!!
import java.io.*;
public class Exc3 {
public static void main(String[] args) {
int row = 1;
int i = 0;
int j = 0;
int max = 5;
for (i = 1; i <= row; i++) {
System.out.print(i);
System.out.print(" ");
if (i == row) {
for (j = 1; j <= max; j++) {
System.out.print(" ");
}
for (j = 1; j <= max; j++) {
System.out.print(j + " ");
}
System.out.println();
i = 0;
row++;
max--;
}
if (row > 5)
break;
}
}
}
Output:-
1 1 2 3 4 5
1 2 1 2 3 4
1 2 3 1 2 3
1 2 3 4 1 2
1 2 3 4 5 1
You have to consider it as one pattern.
triangle of numbers as well as blank space. And you have to print whole line and then only you can go to next line (System.out.print(i + " ");). If you print 1st triangle, there is no way you can move last triangle from down to up.
I won't give you the code, but the some hints and link to help you learn.
You should first of all know how long the string can be. You can then generate two strings, one for the first part of each line and one for the second.
A this point you can use String.Format() using the right padding format
Okay so my current assignment has me making a program that can mimic percolation, meaning that it has to read an array, where 1 means the space is closed, 0 means the space is open. Next, it must change all the 0's at the top row of the array to 2's representing the liquid being poured in. The liquid can then follow the 0's (representing the flow), changing them all to twos along the way. Liquid can move up, down, left, and right. not diagonal. I have my program almost working, but the for loops don't seem to go through more than the first row of the array.
public class Percolation2 {
public static void main (String[] args) {
int[][] filter1 = {{1,0,1,0,1,1,0},
{1,0,1,1,1,0,0},
{0,0,0,1,0,0,1},
{1,1,0,1,1,1,1},
{1,0,0,1,1,0,1},
{1,0,1,0,1,0,1},
{0,0,0,0,1,1,0},
{1,1,1,0,1,0,1}};
for(int i=0; i<7; i++) {
if(filter1[0][i] ==0) {
filter1[0][i] = 2;
}
}
for ( int i = 0 ; i < filter1.length ; i++ ) {
for ( int j = 0 ; j < filter1[i].length ; j++ ) {
if ( filter1[i][j] == 0 )
if(i-1 > 0 && filter1[i-1][j] == 2)
filter1[i][j] = 2;
if(i+1 < 7 && filter1[i+1][j] ==2)
filter1[i][j] = 2;
if(j-1 > 0 && filter1[i][j-1]==2)
filter1[i][j] = 2;
if(j+1 < 7 && filter1[i][j+1] == 2)
filter1[i][j] = 2;
}
}
for(int i = 0; i < filter1.length; i++) {
for(int j = 0; j < filter1[i].length; j++) {
System.out.print(filter1[i][j]);
if(j < filter1[i].length - 1) System.out.print(" ");
}
System.out.println();
}
}
}
My output is as follows:
2 2 2 2 2 2 2
1 0 1 1 1 0 0
0 0 0 1 0 0 1
1 1 0 1 1 1 1
1 0 0 1 1 0 1
1 0 1 0 1 0 1
0 0 0 0 1 1 0
1 1 1 0 1 0 1
So you can clearly tell that it is not looping properly.
Each turn the liquid can move one level. Your for statements are correct, all elements of the array are visited. You need another loop for the proper amount of turns.
I don't want to give you the answer directly as there is a lesson to be learned here, you need to have something like:
//Init
boolean done = false;
while(!done) {
for (int i = 0; i < filter1.length; i++) {
for(int j = 0; j < filter1[i].length; j++) {
if(filter1[i][j] == 0)
....
}
}
//add the print matrix code here if you want it done after each turn.
done = amIDone();
}
Overall your code is structured pretty poorly. You should make use of constants for the array lengths, and you could also define methods for your two doubly nested for loops. At the bare mininum, I would have a printMatrix() method.
I see a couple problems here:
1) You seemed to have left out curly brackets around the block of code following if(filter1[i][j] == 0) in your double for loop.
2) Below that, your check for out-of-bounds is incorrect if(i-1 > 0 && ... should be if(i-1 >= 0 && .... And, if(j+1 < 7 && ... should be if(j+1 < 8 && ...
3) In your question you say that your output is 2 2 2 2 2 2 2 ... . But your code produces different results, your output should be 1 2 1 2 1 1 2 .... Thats what I get when I run for(int i=0; i<7; i++) { if(filter1[0][i] == 0) { filter1[0][i] = 2; } }
4) Pete hints at the problem. But more specifically, you are doing one pass down the matrix (2D array). There is no ability to go up with the liquid (nor left).
Lets call these items cells. And when a cell gets liquid (a.k.a. it turns to == 2), it should percolate that liquid to the neighboring cells which are empty (a.k.a. set to 0). This sounds like a call for recursion, IMHO!
Once you fix the first three issues. Put the payload of your double for loop into a recursive function such as this here:
public void percolate(int x, int y) {
if ( filter1[x][y] == 0 ) {
if(x-1 >= 0 && filter1[x-1][y] == 2) filter1[x][y] = 2;
if(x+1 < 7 && filter1[x+1][y] == 2) filter1[x][y] = 2;
if(y-1 >= 0 && filter1[x][y-1] == 2) filter1[x][y] = 2;
if(y+1 < 8 && filter1[x][y+1] == 2) filter1[x][y] = 2;
if ( filter1[x][y] == 2 ) {
if ( x-1 >= 0 ) percolate(x-1,y);
if ( x+1 < 7 ) percolate(x+1,y);
if ( y-1 >= 0 ) percolate(x,y-1);
if ( y+1 < 8 ) percolate(x,y+1);
}
}
}
Then just call it for each cell in your double for loop. Actually, since the liquid starts at the top, I guess you could just call this percolate for each cell in the second row, only. It will percolate down and round from there.
As you might can see, this is not the most efficient solution, it double/triple checks for percolation on some cells. But this is the slim, nuts and bolts version.
EDIT: Fixed typeos, got rid of unnecessary boolean.
i am supposed to find two numbers that give 4 when one number is subtracted from another. the numbers can be 1 to 6. it is supposed to print out:
5 1
6 2
1 5
2 6
i have done this but its not showing me the last two combinations. why?
public class number2
{
public static void main(String[] args)
{
for(int i=1; i<=6; i++)
{
for(int j=1; j<=6; j++)
{
if(j-i==4)
{
System.out.println(i+ " " +j);
}
}
}
}
}
Just a slight change:
if(Math.abs(j-i) == 4){
System.out.println(i + " " + j);
}
Result like following 2 items are not printed.
5 1
6 2
It is becuase, in such situations if(j-i==4) is not satisfied, j-i== -4 here instead of j-i==4.
Bearing that in mind, if you would like to print the result as you wanted, the value for j-i could be 4 or -4.
You should include these 2 candidate situations.
Change your code with if-condition
from
if(j-i==4)
To
if (i - j == 4 || j - i == 4)
or a simple way is prefered is to use Math.abs method
if(Math.abs(j-i)==4)
I'm having some problems with my Sudoku Solver that I need to make, what I am supposed to do is to create a program which checks to see if the inputted Sudoku puzzle is correct or not. I have the code which checks to see if each row, column, and "minibox" working. My only problem now is to print out the puzzle. This is what I have so far:
public String printGrid(int size, int[][] puzzle){
double temp = Math.sqrt(size);
for(int row = 0; row < size; row++){
for(int column = 0; column < size; column++){
System.out.print(puzzle[row][column]);
if(column == temp - 1) {
System.out.print("|");
}
if(row == temp - 1){
for(int i = 0; i < size; i++){
System.out.print("-\t");
}
}
if(column == size - 1) {
column = 0;
row++;
}
}
}
return "Correct!";
}
As an example size will be 4 and the inputted sudoku will be:
1 2 3 4
4 3 2 1
3 4 1 2
2 1 4 3
My output is to look like this:
1 2 | 3 4
4 3 | 2 1
----+----
3 4 | 1 2
2 1 | 4 3
My current code however gives an ArrayOutOfBounds error and outputs this:
- 2- - - - 1- - - - 4|121|43
I'm completely lost in how to write this method to output, can anyone shed some light on this for me? (Also ignore the fact that all sudokus return "Correct!" I should be able to figure that out myself.)
if(column == size - 1) {
column = 0;
row++;
}
Using the above if-statement, you are not letting the inner loop to get terminated, because everytime the column value reaches the dead-end, you are resetting it to 0, and hence the terminating condition of inner loop (column < size) will always be true, and also you are increasing the row value continuously, which will gradually result ArrayIndexOutOfBounds.
Just remove that if condition. It is not needed.
You have at least 4 problems that I see right away:
You don't print a newline at the end of your outer (row) loop
The if(row == temp - 1) loop should be in the outer loop only (you want to do it on it's own row, not each time in the column
column == temp-1 etc. will only work for the 2x2 case, it should be column > 0 && column % temp == 0
Don't ever ever modify a for loop variable inside the loop, that will confuse everything and usually cause ArrayIndexOutOfBoundsException, as happens here.