Below is my code. Can someone tell me what exactly is the problem in my code? I
ID: 3780130 • Letter: B
Question
Below is my code. Can someone tell me what exactly is the problem in my code? I had tried different ways to change this and that, but it still not working. Thank you so much.
Here is an example of how the chart should look ;
ASCII Chart Using While-Loop:
32= 33=! 34=" 35=# 36=$ 37=% 38=& 39='
40=( 41=) 42=* 43=+ 44=, 45=- 46=. 47=/
48=0 49=1 ...
.
.
.
etc. (to char 127)
**main
public class ASCII
{
public static void main(String[] args)
{
Menus menu= new Menus();
menu.userChoice();
}
}
**Menus
import java.util.Scanner;
public class Menus
{
Loops loop = new Loops();
//This method take the user's input to decide loo[ to print, or to exit the program
public void userChoice()
{
int choice;
Scanner keyboard = new Scanner(System.in);
do
{
System.out.println("Please selectfrom the following new options to see the printable");
System.out.println("Characters of the ASCII chart using various loops, or select 0 to exit");
System.out.println();
System.out.println("1: For Loop");
System.out.println("2: While Loop");
System.out.println("3: Do-While Loop");
System.out.println("0: Exit Program");
choice = keyboard.nextInt();
switch (choice)
{
case 1:
loop.forloop();
break;
case 2:
loop.whileloop();
break;
case 3:
loop.dowhileloop();
break;
case 0:
System.out.println("Existing Program.");
break;
default:
System.out.println("Invalid choice, please try again.");
}
System.out.println();
} while (choice != 0);
keyboard.close();
}
} //end the switch choice
***loops
import java.util.Scanner;
public class Loops
{
//This method print out the ASCII chart by using for loop
public void forloop()
{
int i;
System.out.println("The ASCII chart use a For Loop:");
System.out.println();
for (i=32;i<=127; i++)
{
System.out.printf("%3d = %-2c", i, (char)i);
if (i % 10==0)
System.out.println();
}
System.out.println();
}
//This method print out the ASCII chart using the while loop.
public void whileloop()
{
int i=32;
while (i<27)
{
i++;
System.out.printf("%3d = %-2c", i, (char)i);
if (i % 10==0)
System.out.println();
}
System.out.println();
}
//This method will use do-while loop to print out the appropriate ASCII chart
public void dowhileloop()
{
int i=32;
do {
i++;
System.out.printf("%3d = %-2c", i, (char)i);
}
while (i<127);
System.out.println();
if (i % 7==0);
System.out.println();
}
}
Explanation / Answer
Loops.java
import java.util.Scanner;
public class Loops
{
//This method print out the ASCII chart by using for loop
public void forloop()
{
int i;
System.out.println("The ASCII chart use a For Loop:");
System.out.println();
for (i=32;i<=127; i++)
{
System.out.printf("%3d = %-2c", i, (char)i);
if (i % 10==0)
System.out.println();
}
System.out.println();
}
//This method print out the ASCII chart using the while loop.
public void whileloop()
{
int i=32;
while (i<=127)
{
System.out.printf("%3d = %-2c", i, (char)i);
if (i % 10==0)
System.out.println();
i++;
}
System.out.println();
}
//This method will use do-while loop to print out the appropriate ASCII chart
public void dowhileloop()
{
int i=32;
do {
System.out.printf("%3d = %-2c", i, (char)i);
if (i % 10==0)
System.out.println();
i++;
}
while (i<=127);
System.out.println();
if (i % 7==0);
System.out.println();
}
}
__________________________
Menus.java (Run this program to get the output.As it contains the main() method.)
import java.util.Scanner;
public class Menus {
public static void main(String[] args) {
//Create an object for Loops class Object
Loops loop = new Loops();
//Declaring variable
int choice;
//Scanner class Object is used to read the inputs entered by the user
Scanner keyboard = new Scanner(System.in);
//This loop continues to execute until the user chooses the option 0(Zero)
do
{
//Displaying the menu
System.out.println("Please selectfrom the following new options to see the printable");
System.out.println("Characters of the ASCII chart using various loops, or select 0 to exit");
System.out.println();
System.out.println("1: For Loop");
System.out.println("2: While Loop");
System.out.println("3: Do-While Loop");
System.out.println("0: Exit Program");
//Getting the choice entered by the user
System.out.print("Enter choice :");
choice = keyboard.nextInt();
//Based on the user's choice the corresponding case will get executed
switch (choice)
{
//When the user chooses the choice then case 1 will get executed
case 1:
loop.forloop();
break;
//When the user chooses the choice then case 2 will get executed
case 2:
loop.whileloop();
break;
//When the user chooses the choice then case 3 will get executed
case 3:
loop.dowhileloop();
break;
//When the user chooses the choice then case 0 will get executed
case 0:
System.out.println("Existing Program.");
break;
/* When the user enters a number other than 1 or 2 or 3 or 0
* then default case will get executed
*/
default:
System.out.println("Invalid choice, please try again.");
}
System.out.println();
} while (choice != 0);
keyboard.close();
}
}
______________________________
Output:
Please selectfrom the following new options to see the printable
Characters of the ASCII chart using various loops, or select 0 to exit
1: For Loop
2: While Loop
3: Do-While Loop
0: Exit Program
Enter choice :1
The ASCII chart use a For Loop:
32 = 33 = ! 34 = " 35 = # 36 = $ 37 = % 38 = & 39 = ' 40 = (
41 = ) 42 = * 43 = + 44 = , 45 = - 46 = . 47 = / 48 = 0 49 = 1 50 = 2
51 = 3 52 = 4 53 = 5 54 = 6 55 = 7 56 = 8 57 = 9 58 = : 59 = ; 60 = <
61 = = 62 = > 63 = ? 64 = @ 65 = A 66 = B 67 = C 68 = D 69 = E 70 = F
71 = G 72 = H 73 = I 74 = J 75 = K 76 = L 77 = M 78 = N 79 = O 80 = P
81 = Q 82 = R 83 = S 84 = T 85 = U 86 = V 87 = W 88 = X 89 = Y 90 = Z
91 = [ 92 = 93 = ] 94 = ^ 95 = _ 96 = ` 97 = a 98 = b 99 = c 100 = d
101 = e 102 = f 103 = g 104 = h 105 = i 106 = j 107 = k 108 = l 109 = m 110 = n
111 = o 112 = p 113 = q 114 = r 115 = s 116 = t 117 = u 118 = v 119 = w 120 = x
121 = y 122 = z 123 = { 124 = | 125 = } 126 = ~ 127 =
Please selectfrom the following new options to see the printable
Characters of the ASCII chart using various loops, or select 0 to exit
1: For Loop
2: While Loop
3: Do-While Loop
0: Exit Program
Enter choice :2
32 = 33 = ! 34 = " 35 = # 36 = $ 37 = % 38 = & 39 = ' 40 = (
41 = ) 42 = * 43 = + 44 = , 45 = - 46 = . 47 = / 48 = 0 49 = 1 50 = 2
51 = 3 52 = 4 53 = 5 54 = 6 55 = 7 56 = 8 57 = 9 58 = : 59 = ; 60 = <
61 = = 62 = > 63 = ? 64 = @ 65 = A 66 = B 67 = C 68 = D 69 = E 70 = F
71 = G 72 = H 73 = I 74 = J 75 = K 76 = L 77 = M 78 = N 79 = O 80 = P
81 = Q 82 = R 83 = S 84 = T 85 = U 86 = V 87 = W 88 = X 89 = Y 90 = Z
91 = [ 92 = 93 = ] 94 = ^ 95 = _ 96 = ` 97 = a 98 = b 99 = c 100 = d
101 = e 102 = f 103 = g 104 = h 105 = i 106 = j 107 = k 108 = l 109 = m 110 = n
111 = o 112 = p 113 = q 114 = r 115 = s 116 = t 117 = u 118 = v 119 = w 120 = x
121 = y 122 = z 123 = { 124 = | 125 = } 126 = ~ 127 =
Please selectfrom the following new options to see the printable
Characters of the ASCII chart using various loops, or select 0 to exit
1: For Loop
2: While Loop
3: Do-While Loop
0: Exit Program
Enter choice :3
32 = 33 = ! 34 = " 35 = # 36 = $ 37 = % 38 = & 39 = ' 40 = (
41 = ) 42 = * 43 = + 44 = , 45 = - 46 = . 47 = / 48 = 0 49 = 1 50 = 2
51 = 3 52 = 4 53 = 5 54 = 6 55 = 7 56 = 8 57 = 9 58 = : 59 = ; 60 = <
61 = = 62 = > 63 = ? 64 = @ 65 = A 66 = B 67 = C 68 = D 69 = E 70 = F
71 = G 72 = H 73 = I 74 = J 75 = K 76 = L 77 = M 78 = N 79 = O 80 = P
81 = Q 82 = R 83 = S 84 = T 85 = U 86 = V 87 = W 88 = X 89 = Y 90 = Z
91 = [ 92 = 93 = ] 94 = ^ 95 = _ 96 = ` 97 = a 98 = b 99 = c 100 = d
101 = e 102 = f 103 = g 104 = h 105 = i 106 = j 107 = k 108 = l 109 = m 110 = n
111 = o 112 = p 113 = q 114 = r 115 = s 116 = t 117 = u 118 = v 119 = w 120 = x
121 = y 122 = z 123 = { 124 = | 125 = } 126 = ~ 127 =
Please selectfrom the following new options to see the printable
Characters of the ASCII chart using various loops, or select 0 to exit
1: For Loop
2: While Loop
3: Do-While Loop
0: Exit Program
Enter choice :0
Existing Program.
______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.