How do I print this specific pyramid in Java? - java

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.

Related

"while" loop not iterating correctly

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

Creating a sliding number puzzle board using arrays in Java

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

Java add asterisks with counter

I have a counter variable and for every 3 input in the range from 1-5 I need to add an Asterisk to show the total. So if 7 answers in the range 1-5 occurred, it would only print out 2 Asterisks. I need to somehow take the counter total, put it in a loop I'm assuming and for every 3, add the asterisks to the total below.
if(value >= 1 && value <= 5){
counter++;
}
The print out would look something like this
Answers from 1 - 5: ** // from 7 in this range
Answers from 1 - 5: *** // from 9 in this range
This should give the correct result.
int value = counter / 3;
System.out.print("Answers from 1 - 5: ");
for (int i = 0; i < value; i++)
System.out.print("*");
System.out.println();
end should give you the correct result.
Given that the number you want is an integer it will store it as a whole number (e.g. 7/3 = 2). You can use System.out.print() to start the line, print on the same line as many asterisks you need and then end the line.
Hope this helps.

Return to the top of a for-loop midway through execution?

I am trying to go back to the top a for-loop if a certain condition in an inner if-statement is fulfilled. Is there any code that can do this?
continue;
skips the rest of the loop and returns to the top.
Do you mean you want to use continue?
String myTraingle = "";
for (int i=1;i<=9;i++) {
myTraingle = "";
for (int j=1;j<=9;j++) {
myTraingle = myTraingle + " " + j;
if (i==j) {
System.out.println(myTraingle);
continue;
}
}
}
Output
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
Demo
You can use
continue;
And if you've got some nested loops use
continue <label>;
Examples are here: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
you can use continue statement. In every programming language including java, one can find certain condition in which loop has to iterate next iteration by discontinuing present iteration. continue is used only for that purpose. Just write continue; in inner if statement.
continue;
Skips the current iteration of a for, while, or do-while loop.
break;
Terminates the innermost switch, for, while, or do-while statement.
Example:
for(int i=0; i<100; i++) {
if(i==0) continue; // skip
if(i==10) break; // terminate
System.out.println(i);
}
// prints: 1 2 3 4 5 6 7 8 9 (one per line)
Reference: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
When the if-statement is fulfilled, before using 'continue', overwrite count to '0'.
for(int i=0; i<10; i++)
{
if(/* Some Condition */)
{
i=0; // to go to top again.
continue;
}
}

How to display a 2D table in java

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.

Categories

Resources