是否可以在 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?

提问人:Tshepo 提问时间:10/17/2023 最后编辑:ChaosfireTshepo 更新时间:10/17/2023 访问量:58

问:

我尝试了所有 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!");
    }
}
java if-statement switch-statement 条件运算符

评论

0赞 10/17/2023
是的,当然有可能。查看您的代码,您似乎正在调用后跟 .因此,您遇到的问题可能是 Scanner 在使用 next() 或 nextFoo() 后跳过了 nextLine()?nextInt()nextLine()
0赞 Jorn 10/17/2023
是的,这是可能的。如果您想知道您的代码在运行时的实际情况,您应该使用 IDE 中包含的调试器。
3赞 aled 10/17/2023
请提供一个最小的可重复示例。显然,使用 Scanner 的代码不应该是关于 if 和 switch 的最小示例的一部分。如果它是问题的一部分,那么你问错了问题。
0赞 Sören 10/18/2023
这回答了你的问题吗?扫描仪在使用 next() 或 nextFoo() 后跳过了 nextLine()?

答: 暂无答案