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


相关推荐

  • macbook电脑字体乱码了_mac系统语言改中文

    macbook电脑字体乱码了_mac系统语言改中文按照步骤来亲测可解决①如果没有~/.zshrc这个文件,那么新建一个该文件②新建命令:touch~/.zshrc③打开文件文件编辑vim~/.zshrc④修改内容并保存exportLANG=en_US.UTF-8⑤重启终端即可发现中文可以显示了……

    2025年6月23日
    2
  • Java类加载,getClassLoader()

    Java类加载,getClassLoader()转自【java类加载的深入研究1】loadClass()的研究,IBM深入探讨Java类加载器类加载器基本概念顾名思义,类加载器(classloader)用来加载Java类到Java虚拟机中。一般来说,Java虚拟机使用Java类的方式如下:Java源程序(.java文件)在经过Java编译器编译之后就被转换成Java字节代码(.class文件)。类加载…

    2022年5月27日
    30
  • python读写、创建文件、文件夹等等

    python读写、创建文件、文件夹等等python中对文件、文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块。得到当前工作目录,即当前Python脚本工作的目录路径:os.getcwd()返回指定目录下的所有文件和目录名:os.listdir()函数用来删除一个文件:os.remove()删除多个目录:os.removedirs(r“c:\python”)检验给出的路径是否是一个文件:os.path…

    2022年5月7日
    77
  • 如何成为一名卓越的数据科学家?

    如何成为一名卓越的数据科学家?

    2022年3月4日
    39
  • 百科知识 .e,.ec文件如何打开

    百科知识 .e,.ec文件如何打开1.e是易语言源文件,你可以从以下网址下载e语言编程环境: http://www.xiazaiba.com/html/409.html  2安装之后会自动关联.e文件。  3打开一个e语言文件之后会要求打开相应的易模块文件,既".ec"文件  4下面这个程序点击运行之后打开了一个自定义的透明窗口。  5在纯黑背景下可以看到…

    2022年7月20日
    14
  • NSGA2理解

    NSGA2理解NSGA-II算法学习什么是支配:支配就是统治,在各方面都优于其余个体如个体i支配个体j,就说明个体i在所有目标函数的表现上都不差于个体j,并且至少在一个目标上优于个体j;什么是非支配:非支配就是个体在种群中是最优秀的,它们在该种群中不受其余个体支配。快速非支配排序,将种群中的个体进行分层,最优秀的个体处于第一层,然后除了第一层,再从其余个体中找出非支配个体,以此类推…

    2022年5月19日
    72

发表回复

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

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