java 常量表达式,需要常量表达式? (Java switch语句)[通俗易懂]

java 常量表达式,需要常量表达式? (Java switch语句)[通俗易懂]Ihavenoideawhatiscausingthiserror.MyteacherandIwentoveritandcouldn’tfindwhatwaswrong.importjava.util.Scanner;publicclassMailAssignment{publicstaticvoidmain(String[]args){S…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

java 常量表达式,需要常量表达式? (Java switch语句)[通俗易懂]

I have no idea what is causing this error. My teacher and I went over it and couldn’t find what was wrong.

import java.util.Scanner;

public class MailAssignment

{

public static void main(String [] args){

Scanner userinput = new Scanner(System.in);

char p;

char f;

double price = 0;

System.out.println(“First class or priority?”);

char type = userinput.next().charAt(0);

System.out.println(“How much does the package weigh? (in ounces)”);

double weight = userinput.nextDouble();

switch (type){

case p:

if (weight > 16)

price = weight * 3.95;

else if (weight > 32)

price = (1.20 * (weight / 16));

else

price = 3.50 * weight;

break;

case f:

if (weight < 1 )

price = 0.34;

else if ( weight > 1)

price = 0.34 + (weight * 21);

else if (weight > 13)

price = weight * 3.95;

else if (weight > 32)

price = 1.20 * (weight / 16);

else

price = 3.50 * weight;

break;

}

System.out.println(“Your price is: ” +price);

}

}

It throws a “Constant expression required” error when it’s compiled and it points to the case p: line, however, it also throws it for f: if I switch them so I must be doing something completely off.

解决方案

Yes, a case expression has to be a constant (or an enum constant name) – you can’t use a variable. See the Java Language Specification section 14.11 (switch statements) for more details. (You hadn’t even initialized the variables, so it’s not clear what you expected to happen, to be honest.)

Did you mean:

case ‘p’:

case ‘f’:

? This will match your input (type) against the character literals ‘p’ and ‘f’.

(As an aside, if this stumped your teacher, I have concerns about how suitable they are to be teaching Java. This is reasonably basic stuff.)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/187886.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • mysql查询去除重复记录[通俗易懂]

    mysql查询去除重复记录[通俗易懂]mysql查询去除重复记录1:使用distinctselectdistinctnamefromawherestatesign=0查询表a中的name字段,去除重复记录2:使用groupbyselectnamefromawherestatesign=0groupbyname查询表a中的name字段,分组,去除重复记录…

    2025年10月1日
    5
  • keil MDK5搭建STM32开发环境

    keil MDK5搭建STM32开发环境1.安装keil到keil的官方网站http://www.keil.com/download/product/下载MDK-ARM并安装,注意可以更改安装路径,但是不能安在需要管理员权限的文件夹,例如不能在ProgramFiles,否则会出现一些问题。2.注册未注测有代码容量限制,需要破解。网上找KEIL_Lic。3.安装器件包打开PackInstaller左边选择STMicro

    2022年5月10日
    80
  • 读取文档数据的各列的每行中

    读取文档数据的各列的每行中

    2022年1月7日
    38
  • Apache POI使用详解[通俗易懂]

    Apache POI使用详解[通俗易懂]一:简介开发中经常会设计到excel的处理,如导出Excel,导入Excel到数据库中,操作Excel目前有两个框架,一个是apache的poi,另一个是JavaExcelApachePOI简介是用Java编写的免费开源的跨平台的JavaAPI,ApachePOI提供API给Java程式对MicrosoftOffice(Excel、WORD、PowerPoint、Visio等

    2022年6月28日
    37
  • [渝粤教育] 西安工业大学 汉语文字学 参考 资料[通俗易懂]

    教育-汉语文字学-章节资料考试资料-西安工业大学【】请简要说明黄帝时期与半坡遗址之间的关系。第一章汉字的起源章测试题1、【多选题】请选从下列选项中找出“八卦”的用途()A、卜筮B、族徽C、计数D、装饰参考资料【】2、【多选题】请找出《周礼》中有关史官系统的名称()A、大史B、小史C、内史D、外史参考资料【】3、【多选题】请从下面选项中选出黄帝时期的特点。()A、战争B、纺织C、宫室D、穴居参考资料【】4、【判断题】文

    2022年4月7日
    116
  • Bootstrap 流式布局

    Bootstrap 流式布局流式布局同理,将Bootstrap的流式栅格放到class="container-fluid"的流式容器中,即可创建流式布局。流式布局将填满整个视口宽度。如:&lt;divclass="container-fluid"&gt; &lt;divclass="row-fluid"&gt;   &lt;divclass="span2"&gt;     &lt;!–

    2025年8月4日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号