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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

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.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.
