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 6 Years Ago by walter307: n/a

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;
		String answer;
		double jgrade, mgrade;
                int c1 = 0;
	while(c1==0){
		System.out.println("What is my grade in Java");
		jgrade = myScanner.nextDouble();
		System.out.println();
		System.out.println("My grade in Math?");
		mgrade = myScanner.nextDouble();
		System.out.println();

		System.out.print("JAVA        ");
		System.out.println(jgrade);
		System.out.print("MATH        ");
		System.out.println(mgrade);
		System.out.println("___________________");
		average = (jgrade + mgrade) / 2;
		System.out.print("AVERAGE     ");
		System.out.println(average);
		System.out.println();


		if(average>=95)
			System.out.println("YOU ARE GREAT");
		else if(average>=90)
			System.out.println("YOU GOT GOOD GRADES");
		else if(average>=85)
			System.out.println("NOT BAD FOR A BEGINNER");
		else if(average>=80)
			System.out.println("YOUR A BEGINNER");
		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]? ");
			answer = myScanner.next();

      // This is where i put my loop on Y and N
                 if(answer.equalsIgnoreCase("N")){
                     c1 = 1;

                 }
                     else {if(answer.equalsIgnoreCase("Y"))
                     { 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]? ");
			answer = myScanner.next();
                        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.

i added

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

             if(answer.equalsIgnoreCase("N")){
                     c1 = 1;
                    
                 }
                     else {if(answer.equalsIgnoreCase("Y"))
                     { 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]? ");
			answer = myScanner.next();
                        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; 
		String answer;
		double jgrade, mgrade;
                int c1 = 0, c2;
	while(c1==0){
            c2 = 0;

		System.out.println("What is my grade in Java");
		jgrade = myScanner.nextDouble();
		System.out.println();
		System.out.println("My grade in Math?");
		mgrade = myScanner.nextDouble();
		System.out.println();

		System.out.print("JAVA        ");
		System.out.println(jgrade);
		System.out.print("MATH        ");
		System.out.println(mgrade);
		System.out.println("___________________");
		average = (jgrade + mgrade) / 2;
		System.out.print("AVERAGE     ");
		System.out.println(average);
		System.out.println();


		if(average>=95)
			System.out.println("YOU ARE GREAT");
		else if(average>=90)
			System.out.println("YOU GOT GOOD GRADES");
		else if(average>=85)
			System.out.println("NOT BAD FOR A BEGINNER");
		else if(average>=80)
			System.out.println("YOUR A BEGINNER");
		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]? ");
			answer = myScanner.next();

         while (c2 == 0){

             if(answer.equalsIgnoreCase("N")){
                     c1 = 1;
                     c2 = 1;
                               }
                  else {if(answer.equalsIgnoreCase("Y"))
                    { 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]? ");
			answer = myScanner.next();
                        System.out.println();        
                }
                     
                     
              }
           }
        }
    }
}

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

This question has already been answered. Start a new discussion instead.