You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

询问用户披萨需求后,如何用if/else嵌套switch实现分支逻辑?

How to Implement Pizza/Cake Order Logic with If/Else + Switch Statements

Got it, let's walk through this clearly—you're exactly right to use if/else to split the logic based on the user's pizza question answer, then pair each branch with a switch for the specific options. Here's a step-by-step breakdown with code examples in two common languages.

Core Logic Breakdown

First, the flow we need:

  1. Ask the user "Do you want pizza?" and capture their response
  2. Use if/else to check if they said "yes" or "no"
  3. For "yes": Prompt for pizza size and use a switch to handle different sizes
  4. For "no": Prompt for cake flavor and use a switch to handle different flavors
  5. Add a fallback for invalid answers (like "maybe" or typos)

Example 1: JavaScript (Browser/Node.js)

This uses prompt for input (in browsers) or you could adapt it for Node.js with readline:

// Get and clean up the user's initial answer
const userChoice = prompt("Do you want pizza?").toLowerCase().trim();

if (userChoice === "yes") {
  // Ask for pizza size and process with switch
  const pizzaSize = prompt("Pick your pizza size: small/medium/large").toLowerCase().trim();
  
  switch(pizzaSize) {
    case "small":
      console.log("Perfect, a small pizza is on its way!");
      break;
    case "medium":
      console.log("Great choice—medium pizza coming right up!");
      break;
    case "large":
      console.log("Whoa, big appetite! Large pizza it is.");
      break;
    default:
      console.log("Sorry, that's not a size we offer.");
  }
} else if (userChoice === "no") {
  // Ask for cake flavor and process with switch
  const cakeFlavor = prompt("What flavor cake would you like? chocolate/vanilla/strawberry").toLowerCase().trim();
  
  switch(cakeFlavor) {
    case "chocolate":
      console.log("Chocolate cake—classic and delicious!");
      break;
    case "vanilla":
      console.log("Smooth vanilla cake, coming up!");
      break;
    case "strawberry":
      console.log("Fresh strawberry cake, yum!");
      break;
    default:
      console.log("Hmm, we don't have that flavor right now.");
  }
} else {
  // Handle invalid initial answers
  console.log("Oops, please answer with 'yes' or 'no'!");
}

Example 2: Java (Console App)

If you're working with Java, here's a version using Scanner for input:

import java.util.Scanner;

public class FoodOrderApp {
    public static void main(String[] args) {
        Scanner inputScanner = new Scanner(System.in);
        
        // Ask initial question and clean input
        System.out.println("Do you want pizza?");
        String userAnswer = inputScanner.nextLine().toLowerCase().trim();
        
        if (userAnswer.equals("yes")) {
            System.out.println("What size pizza do you want? (small/medium/large)");
            String pizzaSize = inputScanner.nextLine().toLowerCase().trim();
            
            switch(pizzaSize) {
                case "small":
                    System.out.println("You've ordered a small pizza!");
                    break;
                case "medium":
                    System.out.println("Medium pizza selected—enjoy!");
                    break;
                case "large":
                    System.out.println("Large pizza coming your way!");
                    break;
                default:
                    System.out.println("That's not a valid pizza size.");
            }
        } else if (userAnswer.equals("no")) {
            System.out.println("What flavor cake would you like? (chocolate/vanilla/strawberry)");
            String cakeFlavor = inputScanner.nextLine().toLowerCase().trim();
            
            switch(cakeFlavor) {
                case "chocolate":
                    System.out.println("Chocolate cake ordered!");
                    break;
                case "vanilla":
                    System.out.println("Vanilla cake is ready for you.");
                    break;
                case "strawberry":
                    System.out.println("Strawberry cake—perfect choice!");
                    break;
                default:
                    System.out.println("We don't carry that cake flavor.");
            }
        } else {
            System.out.println("Please respond with 'yes' or 'no' only.");
        }
        
        inputScanner.close();
    }
}

Key Tips to Remember

  • Clean your input: Using toLowerCase().trim() ensures answers like "YES" or " no " are still recognized—this avoids frustrating users over tiny formatting issues.
  • Don't forget break: In most languages, switch statements will "fall through" to the next case if you don't add break. This is almost never what you want for simple menu choices.
  • Add a default case: Always handle unexpected inputs so your program doesn't just silently fail.

内容的提问来源于stack exchange,提问作者Sunny

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:17:30