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

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


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


相关推荐

  • System.setProperty用法

    System.setProperty用法setProperty(Stringprop,Stringvalue)—–设定健值对到系统内存中(操作系统变量的方法)。首先我们来了解一下参数:prop-系统属性的名称。value-系统属性的值正确的返回值情况:系统没有查到健值,返回null,查到的话返回value值。抛错情况:SecurityException-如果安全管理器存在并且其checkPermission方法不允许设置指定属性。NullPointerException-如果key或v.

    2022年7月12日
  • SnackBar_冲洗器使用方法图解

    SnackBar_冲洗器使用方法图解我们在googlekeep中删除记事块儿时,下面会弹出一个小条儿,问你是否撤消,一段时间后自动隐去,同时右划也可以使它隐去。最初我以为这个小条儿是做的一个自定义控件,后来无意中发现不用这么麻烦。Go

    2022年8月4日
  • 操作系统的概念 (OS学习笔记)

    操作系统的概念 (OS学习笔记)操作系统的概念定义计算机系统的层次结构操作系统(OperatingSystem,os)是指控制和管理整个计算机系统的硬件和软件资源,并合理地组织调度计算机的工作和资源的分配,以提供给用户和其他软件方便的接口和环境,它是计算机系统中最基本的系统软件。操作系统的概念和目标从以下三大方面进行阐述:…

    2022年7月14日
  • STM32的NVIC和中断的总结「建议收藏」

    STM32的NVIC和中断的总结「建议收藏」前言:1.要想学习STM32中断,要先掌握STM32对优先级的定义;2.有51单片机开发经验会比较容易理解中断优先级;3.本篇博文基于STM32F103ZET6芯片和3.5.0标准库编写;4.本篇博文从寄存器入手,最终实现编程的步骤;如有不足之处,还请前辈多多指教;一基础知识1.cortex-m3支持256个中断,其中包含了16个内核中断,240个外部中断。(本博文只介绍

    2022年5月28日
  • php表单提交并发送邮件给某个邮箱(示例源码)

    php表单提交并发送邮件给某个邮箱(示例源码)

    2021年9月21日
  • zodmod论坛_我的世界作弊mod

    zodmod论坛_我的世界作弊modmodCount:记录当前集合被修改的次数在所有的集合实现类中(Collection与Map中),都会有一个 modCount 的变量出现,它的作用就是记录当前集合被修改的次数。下面以ArrayList 类进行说明:添加方法:删除方法:可以看出,两个操作都会影响元素的个数。 当我们使用迭代器或 foreach 遍历时,如果你在 foreach 遍历时,自动调用迭代器的迭代方法,此时在遍历过程中调用了集合的add,remove方法时,modCount就会改变,而迭代器记录的modCount

    2022年8月9日

发表回复

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

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