I know this is extremely simple, but I need help with printing a code that displays
1
2 3
4 5 6
7 8 9 10
For some reason, my brain can't figure out how to do it, even though I am familiar with for loops, this is what I have so far.
for(int i = 1; i <= 4; ++i)
{
for(int j = 1; j <= i; ++j)
{
System.out.print(j);
}
System.out.println(" ");
}
I don't know what to do from here, it's been bugging me all day. Thank you!
int counter = 1;
for (int i = 1; i <= 4; ++i) {
for (int j = 1; j <= i; ++j) {
System.out.print(counter++);
System.out.print(" ");
}
System.out.println();
}
Use a counter variable to reach 10. Also move the space in the inner loop and add a line break in the outer loop.
Output:
1
2 3
4 5 6
7 8 9 10
Try this:
int i=0,j=0,n=4,k=1;
for(i=1; i<n+1; i++)
{
for(j=0; j<i; j++)
System.out.print(" "+k++);
System.out.println(" ");
}
Related
I wanna ask you something about my code. I want to display output as below:
2 3 4 1
6 7 8 5
10 11 12 9
14 15 16 13
But the output what is shown:
2 3 4 1
6 3 0 -3
2 -1 -4 -7
-2 -5 -8-11
Here is my current code:
for(int i=0; i<4; i++){
for(int j=0; j<4; j++){
System.out.printf("%3d", number);
if(j==2){
plus=-3;
}
number+=plus;
}
number+=8;
System.out.println("");
}
Can you tell me what's wrong with it? Thank you
You need to reset plus to 1 at the end of each iteration of the outer loop. See the below code in action here.
int number = 2;
int plus = 1;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.printf("%3d", number);
if (j == 2) {
plus = -3;
}
number += plus;
}
number += 8;
plus = 1;
System.out.println("");
}
This question already has answers here:
for loop without braces in java
(6 answers)
Closed 10 months ago.
For the code:
// Demonstrate a two-dimensional array.
class TwoDArray {
public static void main(String args[]) {
int twoD[][] = new int[4][5];
int i, j, k = 0;
for (i = 0; i < 4; i++)
for (j = 0; j < 5; j++) {
twoD[i][j] = k;
k++;
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}
The output gives me:
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
The question is, why isn't a new line given to every number? I mean in the for loop, if the first System.out outputted 20 times, why isn't the next System.out.println(); outputting the same amount?
If you used proper indentation, it would have been clearer :
for (i=0; i<4; i++) {
for (j=0; j<5; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
System.out.println(); belongs to the outer loop, so it executes once for every iteration of the outer loop, after the inner loop ends.
You can also wrap the inner loop in curly braces to make it clearer :
for (i=0; i<4; i++) {
for (j=0; j<5; j++) {
System.out.print(twoD[i][j] + " ");
}
System.out.println();
}
Without braces, a for loop body is one statement. If we add explicit braces, then your code looks like
for(i=0; i<4; i++) {
for(j=0; j<5; j++) {
System.out.print(twoD[i][j] + " ");
}
System.out.println();
}
which is why the println only executes after the inner for loop.
I'm trying to print user input numbers and indent them by that number of spaces. I can't seem to get the numbers to indent, however, I am able to print them all vertically. Any help? Here is my code.
for (i = 0; i <= userNum; i++) {
for (j = 0; j < i; j++) {
System.out.println(i);
break;
If a user entered the number 3, my output would currently look like this:
1
2
3
When it should look like this:
1
2
3
This should do it
for (int i = 0; i <= userNum; i++) {
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
System.out.println(i);
}
You can try to add another for loop within that for loop. The print statement would be after this nested for loop. The inner loop would start from zero to i+1. In this for loop, you can print the spaces or tabs. Then after the for loop you can print the number. Make sure you do not include a new line inside the print statement in the inner for loop.
You haven't added the code yet to add the indenting. Try this:
public static void printNum(int userNum) {
for (int i = 0; i < userNum; i++) {
System.out.print(" ");
}
System.out.print(userNum+ "\n");
}
Calling it with:
printNum(10);
printNum(1);
printNum(2);
printNum(3);
Gives the following:
run:
10
1
2
3
BUILD SUCCESSFUL (total time: 0 seconds)
I hope this solves your problem :
for (i = 1; i <= userNum; i++)
System.out.format("%+(i-1)+s]%n", i);
public class Program {
public static void main(String[] args) {
int i = 3; // or your userNum
// this loop will iterates through 1 to i (or 1 to userNum for you)
for (int j = 1; j <= i; j++) {
// this loop iterates until j2 equals j (e.g. if j = 5, this loop will iterates 4 times)
for (int j2 = 1; j2 < j; j2++) {
// prints the space(s)
System.out.print(" ");
}
// prints the current number (in the first loop) and line break
System.out.println(j);
}
}
}
Alright so, I'm new to nested for loops adn I'm having a bit of an issue on understanding them. I've read many guides, but I still don't fully understand.
Alright the prompt:
Write nested for loops that produce the following output:
000111222333444555666777888999
000111222333444555666777888999
000111222333444555666777888999
What I have so far
for(int num2 = 0; num2 <= 9; num2++) {
for(int num1 = 0; num1 <= 2; num1++) {
System.out.println(num2 + " " + num2 + " " + num2);
}
}
And the output is
0 0 0
0 0 0
0 0 0
1 1 1
1 1 1
1 1 1
2 2 2
2 2 2
2 2 2
3 3 3
3 3 3
3 3 3
4 4 4
4 4 4
4 4 4
5 5 5
5 5 5
5 5 5
6 6 6
6 6 6
6 6 6
7 7 7
7 7 7
7 7 7
8 8 8
8 8 8
8 8 8
9 9 9
9 9 9
9 9 9
What am I doing wrong?
You got 3 copies of each number.
the outer loop:
for (int i = 0; i < 10; i++) {
chooses which number you want to print so that is fine.
The inner loop however is comparing j against the chosen number. You want 3 copies, not a variable number of copies. This change will make 3 copies:
for (int j = 0; j < 3; j++) {
You also don't need this:
System.out.println(i);
EDIT: I just noticed you need 3 of these outputs.
add an outer loop:
for (int x = 0; x < 3; x++) {
and a blank space
System.out.println(" ");
So the final result should be:
for (int x = 0; x < 3; j++) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(i);
}
}
System.out.println(" ");
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 10; j++) {
System.out.print(j + "" + j + "" + j);
}
System.out.println();
}
In the program provided by you the following events take place:-
In first loop variable i is initiated, condition for loop is checked and then it moves to the second loop if condition is true.
Now second loop iterates over the value of j until the condition is false and then control returns to the first loop.
Try to follow the working of loop and you can see yourself where were you wrong.
Try this:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 10; j++) {
for (int k = 0; k < 3; k++) {
System.out.print(j);
}
}
System.out.println("");
}
loop 1: You want the sequence 3 times, each occurrence on its own line.
loop 2: You want the sequence to have digits 0 through 9 ascendingly.
loop 3: You want the sequence to have each digit in succession 3 times.
for (int k = 0; k<3, k++){
for (int i = 0; i< 10; i++) {
for (int j = 0; j < 3;j++) {
System.out.println(i);
}
}
System.out.println("")};
}
}
for(int k=0;k<3;k++) {
for (int i = 0; i< 10; i++) {
for (int j = 0; j < 3;j++) {
System.out.println(i);
}
}
}
Although I am by far most inexperienced guy here, I think this should give exact output you're looking for.
I wanted to write a function which should fill a two dimensional array like this:
1 -- 3 -- 5 -- 7 -- 9
3 -- 5 -- 7 -- 9 -- 11
5 -- 7 -- 9 -- 11 -- 13
7 -- 9 -- 11 -- 13 -- 15
9 -- 11 -- 13 -- 15 -- 17
Here's it what I could come up with ... by all my logic it should work, but obviously I made some mistake:
public class Array {
public static void TwoDimFill(int[] [] array, int start, int inc){
array[0] [0] = start;
for (int i = 0; i < array.length; i++){
for (int j = 0; j < array[i].length; j++){enter code here
array[i][j+1] = array[i][j] + inc;
}
array [i+1][0] = array[0][i+1];
}
}
public static void main(String[] args){
int[] [] b = new int[5] [5];
TwoDimFill(b, 1, 2);
for (int x = 0; x < b.length; x++){
for (int y = 0; y<b[x].length; y++){
TextIO.put(b[x][y]+"\t");
}
TextIO.putln();
}
}
By the way: This TextIO.class is something we use for printing, compareable to system.out ...
Edit: Answered! Thank you a lot guys, you're great!
i+1 and j+1 will be out of bounds because you loop until < length. Change it to < length -1.
for (int i = 0; i < array.length; i++){
for (int j = 0; j < array[i].length -1; j++){
array[i][j+1] = array[i][j] + inc;
}
if (i < array.length -1)
array [i+1][0] = array[0][i+1];
}
EDIT: As pointed out on comments, the last output line was incorrect. Variable i can be looped until < length, but it must be verified for the last line that the index won't get out of bounds. Corrected the code above.
There is an excess number of operations you are doing. There is a much cleaner, simpler and faster solution. Sometimes a different approach can solve the problem better.
for (int i = 0; i < array.length; i++)
for (int j = 0; j < array[i].length; j++)
array[i][j]=((i+j)<<1)+1;
You need to check if the index is out of bounds before executing the last line in your loop, as well as only making your inner loop iterate up to length-1 (since indexes for arrays in Java start at 0 and end at 1 less than their length):
for(int i = 0; i < array.length; i++) {
for(int j = 0; j < array[i].length - 1; j++) {
array[i][j + 1] = array[i][j] + inc;
}
if(i + 1 < array.length) {
array[i + 1][0] = array[0][i + 1];
}
}
This produces:
1 3 5 7 9
3 5 7 9 11
5 7 9 11 13
7 9 11 13 15
9 11 13 15 17
Note that simply clipping the outer loop to array.length-1 doesn't work (since in this case you still want the first part of the loop to execute), you'd end up with the following:
1 3 5 7 9
3 5 7 9 11
5 7 9 11 13
7 9 11 13 15
9 0 0 0 0
Bolding is mine, note the last line is obviously incorrect.
This is wrong because of array[i][j+1]. If j is the last position j+1 doesn't exist. You can try this:
public static void TwoDimFill(int[] [] array, int start, int inc){
int aux = 0;
for(int i = 0; i < array.length; i++) {
aux = start;
for(int j = 0; j < array[i].length; j++) {
array[i][j] = aux;
aux += inc;
}
start += inc;
}
}
You can do this really simply, use the following
public class TestClass {
public static void main(String args[]) {
int[][] temp = new int[5][5];
int[][] output = fill2dim(temp,1,2);
for (int i = 0; i < output.length; i++) {
System.out.println(java.util.Arrays.toString(output[i]));
}
}
public static int[][] fill2dim(int[][] arr, int start, int inc) {
int[][] output = new int[arr.length][arr[0].length];
for (int i = 0;i<arr.length;i++) {
for (int j = i;j<arr[0].length;j++) {
output[i][j] = (i+j)*inc+start;
output[j][i] = (i+j)*inc+start;
}
}
return output;
}
}
You can remove some loops by just considering the upper triangular matrix, since the lower triangular matrix will be a reflection of the upper triangle about the diagonal