Hi,
I'm having trouble grasping 'for' & 'while' statements. While I understand the general concept, practical use in combining the two eludes me.
I have some code that I've written for 3 end of the chapter problems. While they work to some extent, they don't work as I expect them to. I would appreciate any help as to what I'm doing wrong and why it's wrong.
Problem1
CODE
/*
Write a Java program to create the following grid of numbers, using for statements:
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
*/
import java.util.Scanner;
public class Chap8_Prob1 {
public static void main (String []args){
Scanner input = new Scanner(System.in);
int howMany;
char y;
System.out.println("Drawing program 1");
System.out.print("Do you want to start? (Y/N) ");
y = input.next().charAt(0);
while (y =='Y' || y == 'y'){
System.out.print("How many rows/columns? (5-21) ");
howMany = input.nextInt();
while(howMany < 5 || howMany > 21) {
System.out.print("ERROR! OUT OF RANGE (5-21). Reenter: ");
howMany = input.nextInt();
}
for (int row = 1; row <= howMany; row++) {
for (int column = 1; column <= howMany; column++) {
if(column == row)
System.out.print(+row);
else
System.out.print(+column);
}
System.out.println();}
}
}
}
/*My results:
12345
12345
12345
12345
12345
How do I subtract 1 from the beginning while adding 1 to the end? Additionally, the loop does not come back to the question, "Do you wish to continue (Y/N)?"
*/
Problem 2
CODE
/*
Use nested for loops statements to generate the following output.
Sample output:
Do you want to start(Y/N): y
Enter an integer (1 - 9) 12
ERROR! Should be positive. REENTER: 4
1
22
333
4444
Do you want to continue(Y/N): y
Enter an integer (1 - 9) 6
1
22
333
4444
55555
666666
*/
import java.util.Scanner;
public class Chap8_Prob2 {
public static void main (String []args){
Scanner input = new Scanner(System.in);
int howMany;
char y;
System.out.print("Drawing Program 2");
System.out.print("Do you want to start? (Y/N) ");
y = input.next().charAt(0);
while (y =='Y' || y == 'y'){
System.out.print("Enter an integer (1-9) ");
howMany = input.nextInt();
while(howMany < 1 || howMany > 9) {
System.out.print("ERROR! OUT OF RANGE (5-21). Reenter: ");
howMany = input.nextInt();
}
for (int row = 1; row <= 9; row++) {
for (int col = 1; col <= row; col++) {
System.out.print(row);
}
System.out.println();
}
}
}
}
/*
My results:
1
22
333
4444
55555
666666
7777777
88888888
999999999
As you can see, my numbers are backwards...grr! Additionally, I'm having the same issue in Problem1. The loop does not come back to the question, "Do you wish to continue (Y/N)?"
*/
Problem 3
CODE
/*Write a Java program that prompts for integers and dispays them in binary.
Sample output:
Do you want to start(Y/N): y
Enter an integer and I will convert it to binary code: 16
You entered 16.
16 in binary is 10000.
Do you want to continue(Y/N): y
Enter an integer and I will convert it to binary code: 123
You entered 123.
123 in binary is 1111011. */
import java.util.Scanner;
public class Chap8_Prob3 {
public static void main (String []args){
Scanner input = new Scanner(System.in);
int num;
char y;
System.out.print("Do you want to start? (Y/N) ");
y = input.next().charAt(0);
while (y =='Y' || y == 'y'){
System.out.print("Enter an integer and I will convert it to binary code: ");
num = input.nextInt();
//Honestly, I don't have a clue about how to convert an integer to binary code. HELP, please!
}
}
}
As you can see, I can do a little in each problem, but I can't seem to finish the job. Any type of help is appreciated. I must say I'm struggling to one degree or another because I'm very much a visual type of learner. I do much better when I see practical examples of code rather than theoretical pieces of code.
Thanks,
Heinz