How to use the Switch conditional statement in Java
1. Switch conditional statement
1. Switch is a very commonly used selection statement. It is different from the if statement. It judges the value of a certain expression and then decides which section of the program to execute. code. For example: a student's English score is divided into grades. A score of 90~100 indicates a grade of A, a score of 80-89 indicates a grade of B, a score of 70~79 indicates a grade of C, a score of 60~69 indicates a grade of D, and 0~ A score of 60 indicates a grade of E.
2.switch syntax statement:
switch(expression){
case Condition 1:
c1: Single or multiple statements
break;
case Condition 2:
c2: single or multiple statements
break;
case Condition 3:
c3: Single or multiple statements
break;
……
case condition n:
cn: Single or multiple statements
break;
default:
c(n 1): Single or multiple statements
}
Expression of switch statement The value of the formula matches the conditions in each case. If a matching value is found, the statement after the corresponding case will be executed. If no matching value is found, the statement after default will be executed. The function of the break of the switch statement is Jump out of the switch statement.
3. Use switch to write a student’s English score and divide it into grades. A score of 90~100 indicates a grade of A, a score of 80-89 indicates a grade of B, a score of 70~79 indicates a grade of C, and a score of 60 A score of ~69 indicates a grade of D, and a score of 0-60 indicates a grade of E.
int score = 88;//Student score
int quotient=score/10;//Used to determine
char level;//Define a char variable type level Display the grade of the grade
switch(quotient){
case 10:
case 9:
level='A';
break;
case 8:
level='B';
break;
case 7:
level=' C';
break;
case 6:
level='D';
break;
default:
level='E';
}
System.out.print("The grade level is divided into "level");
In this example, use The quotient after division is obtained. If it is greater than 90, the quotient divided by 10 must be 9 or 10 (the score is 100 points). In the case, it is equal to 10. There is no description and no break, so it will continue. It is executed until break leaves the switch, so the student's score is 100 points and the grade level will also be displayed as A; if the comparison condition is not a value from 10 to 6, the default statement will be executed, which means that the quotient is less than 6. The student's grade is shown as E.
4. The expression in the switch statement can only be values of byte, short, char, and int types. If other values are passed in, the program will report an error. The enum enumeration referenced from JDK5.0 can also be used as the value of the switch statement expression, and the String type is referenced in JDK7.0.
2. Use of Scanner class
1.java5 adds the java.util.Scanner class. Its main function is to simplify text scanning and obtain console input. We can obtain user input through the Scanner class.
The following is the basic syntax for creating a Scanner object:
Scanner scanner = new Scanner(System.in);
Create a Scanner through new Scanner(System.in), The console will wait for user input until the Enter key is pressed, and then pass all input content to the Scanner as the scanning object. If you want to get the contents of the console input, just call the Scanner's nextLine() or next() method.
Scanner allows multi-line input;
next() takes the data before the separator each time. For example: The value of input The man should be The, because there is a space after The;
nextLine() takes the data before the newline character each time. For example: enter The man and press Enter, the value is The man;
nextInt() takes next() to parse the string into an int number.
hasNextInt() is used to determine whether an int string can be obtained by calling next() next time. If the end of the input has been reached or the return value of next() cannot be parsed into a number and does not conform to the format of a number, a false is returned.
2. To use the Scanner class, you must use the import java.util.Scanner; statement to import the package.
3. For example: Enter your name case
String name;
Scanner scan = new Scanner(System.in);
System.out .println("Please enter your name:");
name = scan.nextLine();
System.out.println("Your name is:" name);
The result of the operation is:
Please enter your name:
张三
Your name is: Zhang San
三, Data output
Data output: System.out standard output. Two ways:
System.out.println();----Newline output
System.out.print();---No newline output
For example:
System.out.print("Student ID:");
System.out.println("01");
System.out.print( "Name:");
System.out.println("Zhang San");
System.out.print("Class:");
System. out.println("Class 1");
The output result is as follows:
Student number: 01
Name: Zhang San
Class : Class 1
4. Use of continue statement
The continue statement can only appear in the loop body of a loop statement (while, do-while and for loop), and its function is to skip the current loop. The remaining statements after the continue statement are directly executed in the next loop.
For example:
int i=0;
while(i<10){
i ;
if(i==5){
continue;
}
System.out.print(i);
}
The output result is: 1234678910
The above is the detailed content of How to use the Switch conditional statement in Java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

Java Made Simple: A Beginner's Guide to Programming Power Introduction Java is a powerful programming language used in everything from mobile applications to enterprise-level systems. For beginners, Java's syntax is simple and easy to understand, making it an ideal choice for learning programming. Basic Syntax Java uses a class-based object-oriented programming paradigm. Classes are templates that organize related data and behavior together. Here is a simple Java class example: publicclassPerson{privateStringname;privateintage;

Spring Boot simplifies the creation of robust, scalable, and production-ready Java applications, revolutionizing Java development. Its "convention over configuration" approach, inherent to the Spring ecosystem, minimizes manual setup, allo

The best practices of default in C language: place it at the end of the switch statement as the default processing for unmatched values; it is used to handle unknown or invalid values to improve program robustness; avoid duplication with case branches to maintain conciseness; comment clearly on the purpose of the default branch to improve readability; avoid using multiple defaults in one case to maintain clarity; keep the default branch concise and avoid complex operations; consider using enumeration values as case conditions to improve maintainability; in large switch statements, use multiple default branches to handle different situations.

A stack is a data structure that follows the LIFO (Last In, First Out) principle. In other words, The last element we add to a stack is the first one to be removed. When we add (or push) elements to a stack, they are placed on top; i.e. above all the
