使用 ||在交换机的情况下? [英] Using || in Cases in a Switch?

查看:30
本文介绍了使用 ||在交换机的情况下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,对于 Java 基础知识的大学实验室,我遇到了麻烦.我必须设置一个开关,然后在那个开关里面装一个箱子.用户输入有3个选项,每个选项都可以用字母回答.问题是这个字母可以是大写或小写,问题是我似乎无法弄清楚如何设置它,所以一个案例将允许其中任何一个.

So for a college lab for Java Fundamentals I'm having trouble. I have to set up a switch and inside that switch a case. There are 3 options for user input, and each option can be answered with letter. The problem is that this letter is allowed to be capital OR lower case, and the problem is I cant seem to figure out how to set it up so a case will allow either of those.

在下面的代码中..crustType 被定义为一个字符.

In the code below..crustType is defined as a char.

请记住,这是 Java 基础知识,我们只是在学习开关,不幸的是,我们的 PPT 没有解释在这种情况下该怎么做.

Keep in mind this is Java Fundamentals and we're just learning about switches, unfortunately our PPT for this doesn't explain what to do in this situation.

switch (crustType)
  {
     case (crustType == 'H' || crustType == 'h'):
        crust = "Hand-tossed";
        System.out.println("You have selected 'Hand-Tossed' crust for your pizza.");
        break;

     case (crustType == 'T' || crustType == 't'):
        crust = "Thin-crust";
        System.out.println("You have selected 'Thin-Crust' crust for your pizza.");
        break;

     case (crustType == 'D' || crustType == 'd'):
        crust = "Deep-dish";
        System.out.println("You have selected 'Deep-Dish' crust for your pizza.");
        break;

     default:
        crust = "Hand-tossed";
        System.out.println("You have not selected a possible choice so a Hand-tossed crust was selected.");
  }

但是我一直收到一个错误 ||...

However I keep getting an error with ||...

97: error: incompatible types
      case (crustType == 'H' || crustType == 'h'):
                             ^   required: char   found:    boolean 
102: error: incompatible types

推荐答案

使用:

case 'H':
case 'h':
    ...
    break;
case 'T':
case 't':
    ...
    break;

相反.由于crustType 的类型是char,那么case 中的内容必须是char 类型.当你把类似的东西

instead. Since the type of crustType is char, then what goes in cases must be of char type. When you put something like

crustType == 'H'

你会得到一个错误,因为该表达式返回一个 boolean.

you will get an error because that expression returns a boolean.

这篇关于使用 ||在交换机的情况下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆