I try do disply a 2 D table but for some reason I can't, I don't see what i wrote wrong.
This is my code (I am working in vim):
int [][] tab = new int [5][5];
for (int i= 0; i<tab.length ; i++){
for (int j =0; j<tab[i].length; j++){
tab[j][i]=i;
System.out.println("" + tab[j][i]);
}
System.out.println("");
}
}
The result of this code is:
0
0
0
0
1
1
1
1
2
2
2
2
3
3
3
3
4
4
4
4
And i want:
0
0
0
0
1
1
1
1
2
2
2
2
3
3
3
3
4
4
4
4
Can someone help me with this?
Thank you
In the inner loop, replace: -
System.out.println("" + tab[j][i]);
with: -
System.out.print(" " + tab[j][i]);
since you want to continue printing in the same row.
The problem is that System.out.println("" + tab[j][i]);
print a whole line in your output, if you change the
System.out.println("" + tab[j][i]);
with
System.out.print(tab[j][i] + " ");
adding a blank space, you will print the String in the line but it wont break and
start in a new line. Try what #Rohit Jain post, this is a correct solution...
You need to change the code
int [][] tab = new int [5][5];
for (int i= 0; i<tab.length ; i++){
for (int j =0; j<tab[i].length; j++){
tab[i][j]=i;
System.out.print( tab[i][j]+" ");
}
System.out.println("");
}
Cause your outer loop run for every row and the inner for every column. So you must use tab[i][j] (the convention is tab[row][col]) to make the code error free. And here in your case you have equal rows & columns. But problem may arise when they are different.
Related
I desire a code output that looks like this:
6 5 4 3 2 1
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
Keep in mind that my code takes in the size of the pyramid through input before
My code now looks like:
for(int numRows=sizePyr;numRows>=1;numRows--){
for(int i=sizePyr;i>=numRows;i--){
System.out.print(i + " ");
}
System.out.println();
}
Changing the nested for loop to for (int i = numRows; i >= 1; i--) fixed the issue
You want to start printing i with the current numRows value, then work the way down to 1.
Your current code start printing i with sizePyr (which is a constant 6 throughout the function), then work the way down to numRows.
for(int numRows=sizePyr;numRows>=1;numRows--){
for(int i=numRows; i >= 1; i--){
System.out.print(i + " ");
}
System.out.println();
}
For the first line, you want to start with sizePyr (as your inner loop does), but want to end with 1 (which your loop decidedly does not). In fact, every line should end with 1. Change your loop to reflect this.
I am supposed to print the following output by using loops:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
7 6 5 4 3 2 1
The highest number in this pattern (in this example, 7) is determined by user input. Here is the applicable code for the pattern:
index=patternLength+1; n=1; //These values are all previously intitialized
while (index!=1) {
index--;
printSpaces((index*2)-2); //A static method that prints a certain number of spaces
while(n!=1) {
n--;
System.out.print(n + " ");
}
System.out.print("\n");
n=patternLength+1-index;
}
And here is the incorrect output for the user input "7":
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
There are two blank lines preceding the incorrect output; these lines have the correct number of spaces necessary for the complete/correct pattern, but for some reason, the actual numbers start printing too "late" in the loop. In other words, the spaces that appear before the "1, 2 1" in the correct example are in the incorrect output. It's some of the numbers that are missing and make the incorrect example incorrect.
OK, I got it.
index=patternLength+1; n=1;int nSetter=1;
//Loop C
System.out.println("Pattern C:");
while (index!=1) {
index--;
printSpaces((index*2)-2);
while(n!=0) {
System.out.print(n + " ");
n--;
}
System.out.print("\n");
nSetter++;
n = nSetter;
}
My problem was that my "n" needed to go both up and down, so the extra variable "nSetter" seems to have solved that, although this may be a round-about solution. Whatever. Thanks to #Andreas for pointing me in the correct direction and #JohnKugelman for the helpful edit.
Please try this code your second while loop is not correct.
int index = patternLength + 1;
int n = 2; //These values are all previously intitialized
int i = 1;
while (index != 1) {
index--;
printSpaces((index * 2) - 2); //A static method that prints a certain number of spaces
while (n != 1) {
n--;
System.out.print(n + " ");
}
System.out.print("\n");
i++;
n = i+1;
}
So I'm kind of new to Java and decided to create a sliding number puzzle of some sort. Here's what I have :
int[] puz = {1,2,3,
4,5,6,
7,8,9}
for(int i=0; i<puz.length; i++){
System.out.println(puz[i]);
}
The 1 is supposed to be the blank spot but I'll figure that out later. My problem is that the code prints:
1
2
3
4
5
6
7
8
9
when I want it to print:
1 2 3
4 5 6
7 8 9
I've also tried doing a nested loop that I'm too embarrassed to show on here due to how hideous it was.
Would I try using a 2d array instead?
I guess you could try...
int puz = {1,2,3,4,5,6,7,8,9};
int n = Math.ceil(Math.sqrt(puz.length));
for (int i = 0; i < puz.length; i++) {
System.out.print(puz[i] + ((i + 1) % n == 0 ? "\r\n" : " ");
}
Try creating a variable counter and increment it every time you iterate through the loop. Using a modulus operator, divide it by 3 and when remainder is 0, create a new line.
int puz = {1,2,3,4,5,6,7,8,9};
int counter = 1;
for(int i=0; i<puz.length; i++){
System.out.print(puz[i]);
if (counter % 3 == 0){
System.out.println("");
}
counter++;
}
The trick here is to use the modulus operator. This operator divides one number by another, and returns the remainder. In java (and everywhere else as far as I know), % is the modulus operator. If you want every third number to have a line break after it, simply divide by three using modulus division, like so:
int[] puz = {1,2,3,4,5,6,7,8,9};
//For what it's worth, you don't have this semicolon in your question, so I added it in.
for(int i=0; i<puz.length; i++){
System.out.print(puz[i] + " ");
if(i % 3 == 2){//It's equal to 2 because you start at 0 and not 1.
System.out.println("");
}
}
This code, when executed, prints the following, which is what you wanted:
1 2 3
4 5 6
7 8 9
I have the solution to the code, but I don't understand how it works. Can somebody explain?
for (int i = 1; i <= 3; i++)
for (int j = i; j <= 4; j++)
System.out.print(j + " ");
The output for the code is 1 2 3 4 2 3 4 3 4
j starts at i=1 and goes to 4.
i increments.
j starts at i=2 and goes to 4.
Rinse, repeat...
Maybe this visual helps delineate the loops
1 2 3 4 | 2 3 4 | 3 4
At the beginning I is at 1 so j goes to 1 to 4 right
Than I pass to 2 so j goes to 2 to 4 cuz I=j remember
Than I pass to 3 so j goes to 3 to 4 and voila
You get: 1234 234 34...
how do I make a loop that checks 1, 4 times and then moves to 2, and checks it 4 times, and moves to 3, and checks it 4 times.
For example:
//Btw, isNextFree is a boolean that returns true or false if the next line is free.
while(linenumber.isNextFree()){
int linenumber=1;
username = line(linenumber,usernamefile);
linenumber+=1;
}
So, what that loop does is it checks linenumber of usernamefile.txt and stores that value in a hashmap, I need to check use that string value in line one which is stored in that hashmap to make a 4 strings on what is on linenumber 1 on username file concatenate with 1 same passwordstring on passwordfile.txt.
By the way, I'm using scanner, so the line and linenumber.isNextFree doesn't exist, it is like scanner's .isNext basically.
I don't understand the second part of the question, for the first:
for(int i=1; i<4; i++)
{
for(int j=1; j<5; j++)
{
//Do somethings for check
//Example
System.out.println("It's "+ i + " = " j ");
}
System.out.println(""); //only a new line like \n
}
With this code, it do somethings with 1, 4 times
after do something with 2, 4 times
and after do somethings with 3, 4 times
the output is:
It's 1 = 1
It's 1 = 2
It's 1 = 3
It's 1 = 4
It's 2 = 1
It's 2 = 2
It's 2 = 3
It's 2 = 4
It's 3 = 1
It's 3 = 2
It's 3 = 3
It's 3 = 4
I hope this helps.