【问题标题】:How to make a switch that skips switch?如何制作跳过开关的开关?
【发布时间】:2015-09-15 17:19:25
【问题描述】:

注意:它只是一个基本的 java 程序,没有 jframe 等。

import java.io.*;
public class Case1 {
public static void main(String[] Ropher) throws IOException{
    BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
    int t; int y;

    System.out.print("Welcome To M.Y Hotel!");

    System.out.println("\nPlease Select For Information");

    System.out.println("1 --- Bed Menu");
    System.out.println("2 --- Food Menu");
    System.out.println("3 --- Exit");

    System.out.println("");
    t = Integer.parseInt(br.readLine());

    switch (t){

    case 1 :
        System.out.println("1 --- Single Bed");
        System.out.println("2 --- Double Bed");
        System.out.println("3 --- Family Bed");
        System.out.println("4 --- Social Bed");
    break;

    case 2:
        System.out.println("1 --- Appetizer");
        System.out.println("2 --- Soup");
        System.out.println("3 --- Breakfast");
        System.out.println("4 --- Lunch");
        System.out.println("5 --- Dinner");
    break;

    }

    System.out.println("Please Select");
    y = Integer.parseInt(br.readLine());

    switch (y){

    case 1:
        System.out.println("Single Beds: ");
        System.out.println("Standard --- P750");
        System.out.println("King Bed --- P1000");
        System.out.println("Crystal Bed --- P1750");
        System.out.println("Water Bed --- P2000");

    case 2:
        System.out.println("Double Beds: ");
        System.out.println("Deluxe A --- P1000");
        System.out.println("Deluxe B --- P1500");
        System.out.println("Water Double Bed --- P3000");




    }
}

}

我想制作一个程序,如果我在 switch (t) 中选择 case 2,将跳过一个 case,某些内容会弹出(例如 Appetizer)该选择的信息并跳过 switch (y) 中的 case .

【问题讨论】:

  • 为什么不能把switch(y) 放在case 1 语句中。或者您可以在某处添加return。这里有很多解决方案
  • if (t != 2) switch (y)
  • 你应该真正使用设计模式。
  • 我不知道如何输入'return'。你能给我举一些例子吗? @SylvainBiehler
  • @GeoddieChristopherOrtiz 您可以根据 Kevin 的建议查看状态机设计模式。

标签: java switch-statement case


【解决方案1】:
public static void main(String[] args) throws Exception {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int t = 0;
    int y;
    welcome(t, br);
    System.out.println("Please Select");
    y = Integer.parseInt(br.readLine());
    switch (y) {
    case 1:
        System.out.println("Single Beds: ");
        System.out.println("Standard --- P750");
        System.out.println("King Bed --- P1000");
        System.out.println("Crystal Bed --- P1750");
        System.out.println("Water Bed --- P2000");
        break;
    case 2:
        System.out.println("Double Beds: ");
        System.out.println("Deluxe A --- P1000");
        System.out.println("Deluxe B --- P1500");
        System.out.println("Water Double Bed --- P3000");
        break;
    }
}

private static void welcome(int t, BufferedReader br) throws Exception {
    System.out.print("Welcome To M.Y Hotel!");
    System.out.println("\nPlease Select For Information");
    System.out.println("1 --- Bed Menu");
    System.out.println("2 --- Food Menu");
    System.out.println("3 --- Exit");
    System.out.println("");
    t = Integer.parseInt(br.readLine());
    switch (t) {
    case 1:
        System.out.println("1 --- Single Bed");
        System.out.println("2 --- Double Bed");
        System.out.println("3 --- Family Bed");
        System.out.println("4 --- Social Bed");
        break;
    case 2:
        welcome(t, br);
        break;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-22
    • 2020-10-18
    • 2014-02-13
    • 2010-11-28
    • 1970-01-01
    • 2015-08-08
    相关资源
    最近更新 更多