简体   繁体   中英

"How can I request an if-else input after a switch statement?"

After a switch statement I would like to request a 'Y' or 'N' statement and print out a statement for the respective response. How can I declare the input char, then provide a scanner input for that value?

I've tried using input as a char and an integer. I've also tried using the boolean method as well.

import java.util.*;
public class Dowhile {


  public static void main(String[] args) { 
    Scanner in = new Scanner(System.in);
    int x;
    System.out.println("0,1,-1: ");
    x = in.nextInt();

    switch(x)
    {
      case 1:
        System.out.println("Positive");
        break;
      case -1:
        System.out.println("Negative");
        break;
      case 0:
        System.out.println("Zero");
        break;
      default:
        System.out.println("You're a bad person!");
        break;
    }   

    char input = (('Y'||'N'));
    System.out.println("Enter 'Y' or 'N'");
    in.nextInt();                    
    if(input = 'Y')
      System.out.println("OK");
    else
      System.out.println("wow");



  }}

I expect the output to be the println response for the respective input.

I would try something like this:

    System.out.println("Enter 'Y' or 'N'");
    char input = in.next("Y|N").charAt(0);
    if('Y' == input)
      System.out.println("OK");
    else
      System.out.println("wow");

The in.next("Y|N") part requests either a 'Y' or a 'N' (the String "Y|N" is interpreted as a regular expression) and returns the result as a String. The charAt(0) function returns the first (and only) character from this String.

Note that this approach throws an exception if you enter neither 'Y' nor 'N'.

If you want to avoid the exception you can use the following code snippet:

    System.out.println("Enter 'Y' or 'N'");
    char input = in.next(".").charAt(0);
    if('Y' == input)
      System.out.println("OK");
    else if ('N' == input)
      System.out.println("wow");
    else
      System.out.println("You haven't entered a valid character");

But beware, because your first call to in.nextInt() will still fail if someone enters something that isn't an integer.

After a switch statement I would like to request a 'Y' or 'N' statement and print out a statement for the respective response. How can I declare the input char, then provide a scanner input for that value?

I've tried using input as a char and an integer. I've also tried using the boolean method as well.

import java.util.*;
public class Dowhile {


  public static void main(String[] args) { 
    Scanner in = new Scanner(System.in);
    int x;
    System.out.println("0,1,-1: ");
    x = in.nextInt();

    switch(x)
    {
      case 1:
        System.out.println("Positive");
        break;
      case -1:
        System.out.println("Negative");
        break;
      case 0:
        System.out.println("Zero");
        break;
      default:
        System.out.println("You're a bad person!");
        break;
    }   

    char input = (('Y'||'N'));
    System.out.println("Enter 'Y' or 'N'");
    in.nextInt();                    
    if(input = 'Y')
      System.out.println("OK");
    else
      System.out.println("wow");



  }}

I expect the output to be the println response for the respective input.

Assuming you don't need to expand your program to do anything other than print to the console, the following would be the approach I'd take:

public static void main(String[] args) { 
    Scanner in = new Scanner(System.in);

    System.out.println("0,1,-1: ");
    int x = in.nextInt();
    System.out.println(
            x == 1 ? "Positive" :
            x == -1 ? "Negative" :
            x == 0 ? "Zero" :
            "You're a bad person!"
    );

    System.out.println("Enter 'Y' or 'N'");
    System.out.println(in.next().equalsIgnoreCase("Y") ? "OK" : "wow");
    in.close();
}

Ternary operators are used for checking the conditions and printing to the console without switch or if statements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM