how to put a command using only 2 letters the Y and N?
and then prints out a text saying please input y and n only

ex:

``System.out.print("DO YOU WANT TO TRY AGAIN [Y/N]? ");``

Edited by walter307: n/a

4
Contributors
8
Replies
9
Views
8 Years
Discussion Span
Last Post by new_programmer

You want to only accept the inputs "Y" and "N"? One way would be to put the input in a loop, and only break out of the loop when one of those characters is the input. This is a good place to use a switch statement - you'd wrap it in a while loop.

This is my code

``````package amp;

import java.util.Scanner;
import java.util.*;

public class Main {

public static void main(String[] args){
Scanner myScanner = new Scanner(System.in);
double average;
int c1 = 0;
while(c1==0){
System.out.println("What is my grade in Java");
System.out.println();
System.out.println();

System.out.print("JAVA        ");
System.out.print("MATH        ");
System.out.println("___________________");
System.out.print("AVERAGE     ");
System.out.println(average);
System.out.println();

if(average>=95)
System.out.println("YOU ARE GREAT");
else if(average>=90)
else if(average>=85)
else if(average>=80)
else if(average>=75)
System.out.println("YOU PASSED! CONGRATULATIONS");
else if(average>=1)
System.out.println("PERFECT ABSENT");
System.out.print("DO YOU WANT TO TRY AGAIN [Y/N]? ");

// This is where i put my loop on Y and N
c1 = 1;

}
{ c1 = 0;
}
else {
System.out.println();
System.out.println("Please input Y for Yes or N for No");
System.out.print("DO YOU WANT TO TRY AGAIN [Y/N]? ");
System.out.println();

}

}
}
}
}``````

Correct me if im wrong

Yes, that would be a good place to put a loop. I don't see one yet, but if you wrap the stretch from line 50 to line 66 in a while loop, you'll have a good start.

``````while((answer.equalsIgnoreCase("Y") && answer.equalsIgnoreCase("Y")))

c1 = 1;

}
{ c1 = 0;
}
else {
System.out.println();
System.out.println("Please input Y for Yes or N for No");
System.out.println();
System.out.print("DO YOU WANT TO TRY AGAIN [Y/N]? ");
System.out.println();

}``````

but when i press n the it looped me back to the quest but when i press y it doesnt do anything its a never ending input

It's a start. Broken can be fixed.

- You have while (a && a) as your control structure. Is this just to be extra sure that a equals "Y"?

- You have a common and subtle problem in your if/else chain. The else at line 10 of the present code does not do what you think it does. Ignore the indentation (which is deceiving you in this case) and trace the logic. Can you get to line 11 if answer == "Y"?

In line 1 the condiction for while loop answer.equalsIgnoreCase("Y") repeats once.
Why?

hehehe my mistake. . I miss to put another int for the program to run either way c1 or c2

i finish just finish my prog with the help of jon.kiparsky thank you =)

by the way i reconstruct my 2nd while to

``````while (c2 == 0){
}``````

this is my entire code:

``````package amp;

import java.util.Scanner;

public class Main {

public static void main(String[] args){
Scanner myScanner = new Scanner(System.in);
double average;
int c1 = 0, c2;
while(c1==0){
c2 = 0;

System.out.println("What is my grade in Java");
System.out.println();
System.out.println();

System.out.print("JAVA        ");
System.out.print("MATH        ");
System.out.println("___________________");
System.out.print("AVERAGE     ");
System.out.println(average);
System.out.println();

if(average>=95)
System.out.println("YOU ARE GREAT");
else if(average>=90)
else if(average>=85)
else if(average>=80)
else if(average>=75)
System.out.println("YOU PASSED! CONGRATULATIONS");
else if(average>=1)
System.out.println("PERFECT ABSENT");
System.out.print("DO YOU WANT TO TRY AGAIN [Y/N]? ");

while (c2 == 0){

c1 = 1;
c2 = 1;
}
{ c1 = 0;
c2 = 1;
}
else {
System.out.println();
System.out.println("Please input Y for Yes or N for No");
System.out.println();
System.out.print("DO YOU WANT TO TRY AGAIN [Y/N]? ");
System.out.println();
}

}
}
}
}
}``````

thanks again to mr. Tong1 and jon.kiparsky =)