切换后的Java if语句

时间:2018-04-19 19:51:57

标签: java if-statement switch-statement

我刚开始使用Java 3天前,我需要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

我知道没有那么多的Pro程序员使用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!");




        }





    }



}

2 个答案:

答案 0 :(得分:0)

而不是切换后的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!");

答案 1 :(得分:0)

应该可以正常工作:

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;
            }
        }
    }
}