Reputation: 55
I am trying to come up with the following output:
Row 1 Col A
Row 1 Col B
Row 2 Col A
Row 2 Col B
I am having trouble figuring out the logic to complete this task. So far I came up with the code provided below:
int iRowNum;
int iColLetter;
iRowNum = 1;
iColLetter = 65;
while (iColLetter < 67)
{
System.out.println("Row " + iRowNum + " Col " + (char)iColLetter);
iColLetter++;
while (iRowNum < 3)
{
iRowNum += 1;
}
}
Unfortunately, I receive the output of:
Row 1 Col A
Row 3 Col B
That begin said, I have a feeling I am getting close to where I need to be, but I spent a good chunk of time trying to figure out the logic behind my desired output.
Final Question How do I display the rows and columns as shown in the first block of this post?
Upvotes: 1
Views: 1766
Reputation: 106
Your output has only two rows that mean the program is not getting into the inner while loop to print. The reason is iColLetter = 65 and it increased to 57 and stop going inside while loop. So you have to reset iColLetter = 65 in the outer loop. Look at the example
public class NestWhileLoop {
public static void main(String [] args) {
int iRowNum = 1;
int iColLetter = 65;
while (iRowNum < 3) {
while (iColLetter < 67){
System.out.println("Row " + iRowNum + " Col " + (char)iColLetter);
iColLetter++;
}
iColLetter = 65;
iRowNum ++;
}
}
}
Upvotes: 0
Reputation: 79580
Do it as follows:
public class Main {
public static void main(String[] args) {
int iRowNum = 1;
int iColLetter = 65;
while (iRowNum < 3) {
iColLetter = 65;
while (iColLetter < 67) {
System.out.println("Row " + iRowNum + " Col " + (char) iColLetter);
iColLetter++;
}
iRowNum += 1;
}
}
}
Output:
Row 1 Col A
Row 1 Col B
Row 2 Col A
Row 2 Col B
Explanation:
iRowNum
to go up to 2
, you start with while (iRowNum < 3)
or while (iRowNum <= 2)
.iRowNum
, the inner loop has to run for two times, starting with 65
i.e. you need to reset iColLetter
to 65
before the start of the inner loop. Upvotes: 1
Reputation: 3986
I have pasted an answer as close as possible to your code. I had to make several minor changes - you should analyze each one to understand the subtle differences each line of code on how loops work, and how in some cases you need to reinitialize variables/ restructure for loops.
int iRowNum = 1;
int iColLetter = 65;
while (iRowNum < 3)
{
while (iColLetter < 67)
{
System.out.println("Row " + iRowNum + " Col " + (char)iColLetter);//print alphabet for the given row
iColLetter++;//increment the alphabet
}
iRowNum += 1;//row 1 printed, point to row 2
iColLetter=65;//row 1 printed, reinitialize to start printing at A
}
}
Upvotes: 0
Reputation: 496
iRowNum = 1;
iColLetter = 65;
while (iRowNum < 3) {
iColLetter = 65;
while (iColLetter < 67) {
System.out.println("Row " + iRowNum + " Col " + (char) iColLetter);
iColLetter++;
}
iRowNum += 1;
}
Output
Row 1 Col A
Row 1 Col B
Row 2 Col A
Row 2 Col B
Upvotes: 0
Reputation: 10184
You are looping through the rows first and then columns. That's why you are not getting the desired output. See the following correction in your code:
iRowNum = 1;
while (iRowNum < 3)
{
iColLetter = 65;
while (iColLetter < 67)
{
System.out.println("Row " + iRowNum + " Col " + (char)iColLetter);
iColLetter++;
}
iRowNum += 1;
}
Upvotes: 0
Reputation: 26076
You have mixed up the ordering of incrementing. This should do the job:
iRowNum = 1;
iColLetter = 65;
while (iRowNum < 3) {
for(int i = 0; i + iColLetter < 67; i++){
System.out.println("Row " + iRowNum + " Col " + (char)(iColLetter + i));
}
iRowNum += 1;
}
Upvotes: 1