Java流程控制Part3:深入理解Switch语句

需积分: 0 0 下载量 100 浏览量 更新于2024-08-26 收藏 4KB MD 举报
"Java流程控制Part3的笔记,主要探讨了Java中的Switch语句及其用法,包括如何根据条件执行不同的代码块以及与if-else语句的对比。" 在Java编程中,流程控制是程序设计的基础部分,它决定了代码执行的顺序。本篇笔记主要聚焦于`switch`语句,这是一种条件控制结构,用于在多个可能的选项中选择执行特定的代码段。 `switch`语句的基本语法结构如下: ```java switch(expression) { case value1: // statements break; case value2: // statements break; ... default: // default statements break; } ``` 这里的`expression`是一个可以返回整数、字符、枚举类型或`String`(自Java 7开始支持)的表达式,`value1`、`value2`等是与`expression`匹配的值。当`expression`的计算结果与某个`case`后的值相匹配时,相应的代码块将被执行。每个`case`后通常会有一个`break`语句,用于跳出`switch`结构,防止执行后续的`case`。如果没有`break`,程序会继续执行下一个`case`,这种现象称为"case穿透"。 例如,以下代码展示了`switch`语句的基本用法: ```java public class SwitchExample { public static void main(String[] args) { int choice = 1; switch (choice) { case 1: System.out.println("You chose 1"); break; case 2: System.out.println("You chose 2"); break; case 3: System.out.println("You chose 3"); break; default: System.out.println("You chose default"); break; } } } ``` 当`choice`的值为1时,输出"你选择了1";若没有`break`,则会继续执行`case 2`和`case 3`的代码,直至遇到`break`退出。如果`choice`的值没有匹配的`case`,则会执行`default`分支的代码。 相比`if-else if-else`结构,`switch`语句在处理多个等值判断时,可读性和效率通常更高。例如,当有多个相同的代码块时,可以使用`case`穿透优化代码: ```java int choice = 2; switch (choice) { case 1: System.out.println("You chose 1"); break; case 2: case 3: System.out.println("You chose 2 or 3"); break; default: System.out.println("You chose default"); break; } ``` 在这个例子中,`choice`为2或3时,都将输出"你选择了2或3",减少了重复的代码。 总结来说,`switch`语句是Java中一种强大的条件控制结构,尤其适合处理多个相似的条件分支。通过合理利用`case`穿透和`default`分支,我们可以编写出简洁且易于维护的代码。在实际编程中,应根据具体场景灵活选择`if-else`或`switch`,以实现最优的代码组织。

这是什么问题FATAL EXCEPTION: main Process: com.example.lightcontrol_app2, PID: 4533 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lightcontrol_app2/com.example.lightcontrol_app2.ui.control.activity.EditingSingleLampActivity}: java.lang.RuntimeException: setOnItemClickListener cannot be used with a spinner. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2668) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2729) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1480) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6176) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783) Caused by: java.lang.RuntimeException: setOnItemClickListener cannot be used with a spinner. at android.widget.Spinner.setOnItemClickListener(Spinner.java:571) at com.example.lightcontrol_app2.ui.control.activity.EditingSingleLampActivity.init(EditingSingleLampActivity.java:111) at com.example.lightcontrol_app2.ui.control.activity.EditingSingleLampActivity.onCreate(EditingSingleLampActivity.java:65) at android.app.Activity.performCreate(Activity.java:6692) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2621) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2729) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1480) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6176) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783)

2023-06-11 上传

以下问题如何修改jakarta.servlet.ServletException: 类text.HandlePassword不是Servlet org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:356) org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399) org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:867) org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1716) org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) java.lang.Thread.run(Thread.java:748) 根本原因。 java.lang.ClassCastException: text.HandlePassword cannot be cast to jakarta.servlet.Servlet org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:356) org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399) org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:867) org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1716) org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) java.lang.Thread.run(Thread.java:748)

2023-06-08 上传
2023-03-02 上传