提问人:Tshepo 提问时间:10/17/2023 最后编辑:ChaosfireTshepo 更新时间:10/17/2023 访问量:58
是否可以在 Java 中的 1 个 src 代码上有一个三元运算符、if、if/else 语句和 switch 语句?
Is it possible to have a ternary operator, if,if/else statement and switch statement on 1 src code in Java?
问:
我尝试了所有 4 对 1 src 代码,我的程序中途停止。
这是代码,我做错了什么?
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package com.operators;
import java.util.Scanner;
/**
*
* @author Casner
*/
public class Operators {
public static void main(String[] args) {
Scanner type = new Scanner(System.in);
System.out.print("Welcome to Chip and Burger Joint, would you like to order? please type Yes or No: ");
String st = type.nextLine();
String b = "You are now leaving Chip and Burger Joint";
String c = "Start your order: " ;
String ord = "Yes".equals(st)? c:b;
System.out.println(ord);
if(ord.equals(c)){
System.out.print("You have 3 options: 1. Chips only, 2. Burger only, 3. Meal, Please Choose: ");
int ord1 = type.nextInt();
if(ord1 == 1){
System.out.print("Please enter MM for mini, S for small, M for medium and L for large: ");
String chips = type.nextLine();
switch(chips){
case "MM": System.out.println("You picked mini chips");
break;
case "S": System.out.println("You picked small chips");
break;
case "M": System.out.println("You picked medium chips");
break;
case "L": System.out.println("You picked large chips");
break;
default: System.out.println("Thank you!");
}
}
else if(ord1 == 2){
System.out.print("Please enter C for Chicken burger or B for Beef burger: ");
String burger = type.nextLine();
switch(burger){
case "C": System.out.println("You picked Chicken burger");
break;
case "B": System.out.println("You picked Beef burger");
default: System.out.println("Thank you!");
}
}
else if(ord1 == 3){
System.out.println("Please enter CM for chicken burger meal or BM for beef burger meal");
String bm = type.nextLine();
switch(bm){
case "CM": System.out.println("You picked Chicken burger meal");
break;
case "BM": System.out.println("You picked Beef burger meal");
break;
default: System.out.println("Thank you!");
}
}
else System.out.println("Thank You!");
}
//System.out.println("Thank you!");
}
}
答: 暂无答案
评论
nextInt()
nextLine()