【问题标题】:Java if statement after switch切换后的Java if语句
【发布时间】:2018-04-19 19:51:56
【问题描述】:

我需要关于if 声明的帮助。

我要做的是在默认之后,放一个if 声明,基本上就是这样

if name equals Mike or lady 
    then print out "Type a number between 1-3 to see your prize". 

如果你输入例如 1,它应该打印出you won a Bicycle

我知道没有多少专业程序员使用switch,但我现在只知道这些:)

import java.util.Scanner;
public class Ifwasif {

public static void main (String [] args) {


    System.out.println("Welcome to our Store!");
    System.out.println("we hope you will find what you're looking for");

    Scanner input = new Scanner (System.in);

    System.out.print("To check out, please type your name: ");
    String name = input.nextLine();

    System.out.print("You need to confirm your age, please type your age: ");
    int age = input.nextInt();

    Scanner input1 = new Scanner (System.in);

    System.out.print("You have an award to collect! To collect it type your name: ");
    String namee = input1.nextLine();

    switch (namee) {

    case ("Mike"):
        System.out.println("Congrats, you are the Winner!");
        break;

    case ("Don"):
        System.out.println("Sorry you are not the winner!Better luck next time");
        break;

    case ("lady"):
        System.out.println("Congrats, you are the Winner!");
        break;

    default:
        System.out.println("Your name is not in the list!");




        }





    }



}

【问题讨论】:

  • 然后在默认后面加上if。开关与你是否可以这样做没有任何关系。
  • 顺便说一句:为什么要创建第二个扫描仪?只需使用input.nextLine() 阅读namee。 (如果你这样做,你可能会发现你得到一个空的namee。调用input.nextLine(); 来消耗包含 int 的行的其余部分)。

标签: java if-statement switch-statement


【解决方案1】:

而不是切换后的 if 语句,将您的 2 个“赢家”案例合并为一个案例:

switch (namee) {
case ("Mike"):
case ("lady"):
    System.out.println("Congrats, you are the Winner!");
    // insert code here to prompt for input, read result, compare, and award
    // or put that code into a new method
    break;
case ("Don"):
    System.out.println("Sorry you are not the winner!Better luck next time");
    break;
default:
    System.out.println("Your name is not in the list!");

【讨论】:

    【解决方案2】:

    应该可以正常工作:

    import java.util.Scanner;
    
    class Main {
        public static void main(String[] args) {
            System.out.println("Welcome to our Store!");
            System.out.println("we hope you will find what you're looking for");
            Scanner input = new Scanner(System.in);
            System.out.print("To check out, please type your name: ");
            String name = input.nextLine();
            System.out.print("You need to confirm your age, please type your age: ");
            int age = input.nextInt();// variable never used
            input.nextLine();
            System.out.print("You have an award to collect! To collect it type your name: ");
            String namee = input.nextLine();
            switch (namee) {
                case ("Mike"):
                case ("lady"):
                    System.out.println("Congrats, you are the Winner!");
                    break;
                case ("Don"):
                    System.out.println("Sorry you are not the winner!Better luck next time");
                    break;
                default:
                    System.out.println("Your name is not in the list!");
                    break;
            }
            if("Mike".equals(name) || "lady".equals(name)){
                System.out.println("Type a number between 1-3 to see your prize'");
                int number = input.nextInt();
                switch (number) {
                    case 1:
                        System.out.println("You won a Bicycle");
                        break;
                    default:
                        break;
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-20
      • 1970-01-01
      • 2010-09-11
      • 1970-01-01
      相关资源
      最近更新 更多