Home> Java> javaTutorial> body text

How to use the Switch conditional statement in Java

WBOY
Release: 2023-04-18 11:37:03
forward
3001 people have browsed it

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!