【发布时间】: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