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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • JS后退一页, JS返回上一页, JS返回下一页代码[通俗易懂]

    JS后退一页, JS返回上一页, JS返回下一页代码[通俗易懂]Javascript返回上一页:1.history.go(-1),返回两个页面:history.go(-2);2.history.back().3.window.history.forward()返回下一页4.window.history.go(返回第几页,也可以使用访问过的URL)例:&lt;ahref="javascript:history.go(-1);"…

    2022年7月25日
    5
  • 367. 学校网络(Tarjan强连通分量)[通俗易懂]

    367. 学校网络(Tarjan强连通分量)[通俗易懂]一些学校连接在一个计算机网络上,学校之间存在软件支援协议,每个学校都有它应支援的学校名单(学校 A 支援学校 B,并不表示学校 B 一定要支援学校 A)。当某校获得一个新软件时,无论是直接获得还是通过网络获得,该校都应立即将这个软件通过网络传送给它应支援的学校。因此,一个新软件若想让所有学校都能使用,只需将其提供给一些学校即可。现在请问最少需要将一个新软件直接提供给多少个学校,才能使软件能够通过网络被传送到所有学校?最少需要添加几条新的支援关系,使得将一个新软件提供给任何一个学校,其他所有学校就都可

    2022年8月9日
    7
  • iocomp入门教程(绘制Plot)[通俗易懂]

    iocomp入门教程(绘制Plot)[通俗易懂]最近要做一个项目需要绘制曲线,为了节省时间,就选用了iocomp控件,可网上相关的教程极少,官方给的文档还是比较详尽,但缺少具体的前期准备步骤,在初次接触这个控件很容易蒙,所以我写下这篇,给入门者以便利。用到的材料:iocomp激活成功教程版(目前常见的为V3和V4,两个版本按喜好选择吧~…

    2022年7月25日
    21
  • angular7 父组件向子组件传值

    angular7 父组件向子组件传值1.新建子组件app-child2.在父组件中引用子组件<app-child[value1]=”fatherValue”></app-child>2.在子组件中使用@Input接受父组件传的值@Input()value1:string;value1就是父组件传到子组件的值了,可以在子组件中去使用…

    2022年6月5日
    25
  • mysql datetime格式化日期(日期格式化)

    Mysql日期格式化查询问题写sql语句时发现怎么都查不出来数据,后来发现数据格式化后和前台传入的数据格式不一样。前台传入数据格式‘2018-11-5’原先sql查询语句(mybatis中)SELECTcount(*)count,a.store_idstoreid,DATE_FORMAT(a.timeone,’%Y-%m-%d’)mytime,s.abbrev…

    2022年4月13日
    116

发表回复

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

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