Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Code Analysis of Java process Control example

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of Java process control example code analysis, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this Java process control example code analysis article. Let's take a look at it.

Java process control user interaction Scanner

Java.util.Scanner is a new feature of Java5. Users' input can be obtained through the Scanner class.

Basic grammar

Scanner sc = new Scanner (System.in)

To get the input string through the next () and nextLine () methods of the Scanner class, we usually need to use hasNext () and hasNextLine () to determine whether there is any input data before reading it.

Next ()

1. Be sure to read valid characters before you can end the input.

2. The next () method automatically removes the white space encountered before entering valid characters.

3. Use the white space entered after a valid character as a delimiter or Terminator only after you enter a valid character.

4. Next () cannot get a string with spaces.

Public static void main (String [] args) {/ / create a scanner object to receive keyboard data Scanner scanner = new Scanner (System.in); System.out.println ("receive with next:") / / determine whether the user has input string if (scanner.hasNext ()) {/ / receive String str = scanner.next () using next; / / if hello world is entered, the output is only hello System.out.println ("input:" + str) } / / classes belonging to IO stream (input / output stream) need to be closed. If not closed, resources will be consumed by scanner.close ();} nextLine ()

1. Take Enter as the Terminator, that is, the nextLine () method returns all the characters before entering enter.

2. You can get the characters after the white space.

Public static void main (String [] args) {Scanner scanner = new Scanner (System.in); System.out.println ("receive with nextLine"); if (scanner.hasNextLine ()) {String str = scanner.nextLine (); / / input hello world can output System.out.println in the console ("input:" + str);} scanner.close () }

Integer types and floating point types also have corresponding uses

Public static void main (String [] args) {Scanner scanner = new Scanner (System.in); System.out.println ("Please enter an integer"); if (scanner.hasNextInt ()) {int a = scanner.nextInt (); System.out.println ("Integer you enter:" + a);} else {System.out.println ("you entered not an integer") } System.out.println ("Please enter a decimal"); if (scanner.hasNextFloat ()) {float b = scanner.nextFloat (); System.out.println ("decimal you entered:" + b);} else {System.out.println ("you entered not a decimal");} scanner.close () } Select structure if single selection structure

Most of the time, we need to judge whether something is feasible or not, and then we execute it. Such a process is represented by if statements in the program.

Grammar

If (Boolean expression) {/ / statement to be executed if the Boolean expression is true

Example:

Public static void main (String [] args) {Scanner scanner = new Scanner (System.in); System.out.println ("Please enter a content"); String str = scanner.nextLine (); / / if what you type is Hello, if (str.equals ("Hello")) {System.out.println (str);} scanner.close () } if double selection structure

Suppose there is a demand now, the company wants to buy a software, succeed, pay 1 million yuan to someone, fail, find someone to develop it. Such a requirement can not be handled with an if, we need to have two judgments, we need a double choice structure, so we have the if-else structure.

Grammar

If (Boolean expression) {/ / statement to be executed if Boolean expression is true} else {/ / statement to be executed if Boolean expression is false

Example:

Public static void main (String [] args) {/ / pass if the input score is greater than or equal to 60, otherwise fail Scanner scanner = new Scanner (System.in); System.out.println ("Please enter score"); int score = scanner.nextInt (); if (score > = 60) {System.out.println ("pass") } else {System.out.println ("fail");} scanner.close ();} if multiple selection structure

We found that the code just now does not accord with the actual situation, the real situation may also exist ABCD, there are interval multi-level judgments. For example, 90-100 is ABE 80-90 is B.. Wait, we often have more than two choices in life, so we need a multi-choice structure to deal with this kind of problem!

Grammar

If (Boolean expression 1) {/ / if Boolean expression 1 is the statement to be executed by true} else if (Boolean expression 2) {/ / if Boolean expression 2 is the statement to be executed by true} else if (Boolean expression 3) {/ / if Boolean expression 3 is the statement to be executed by true} else {/ / if none of the above Boolean expressions are true

Example:

Public static void main (String [] args) {Scanner scanner = new Scanner (System.in); System.out.println ("Please enter grades"); int score= scanner.nextInt (); if (score==100) {System.out.println ("full marks!!") ;} else if (score=90) {System.out.println ("A");} else if (score=70) {System.out.println ("B");} else if (score=60) {System.out.println ("C");} else if (score=0) {System.out.println ("fail, keep up") } else {System.out.println ("grade error");} scanner.close ();} nested if structure

It is legal to use nested if...else statements. This means that you can use if or else if statements in another if or else if statement. You can nest else if...else like if statements.

Syntax:

If (Boolean expression 1) {/ / if Boolean expression 1 is the code to be executed by true (Boolean expression 2) {/ / if Boolean expression 2 is the code to be executed by true}}

Example:

Public static void main (String [] args) {Scanner scanner = new Scanner (System.in); System.out.println ("Please enter an integer between 1 and 100"); int I = scanner.nextInt (); if (iTun1) {System.out.println ("legal number:" + I);} else {System.out.println ("illegal") }} else {System.out.println ("illegal");} scanner.close ();} Switch multiple selection structure

Another way to implement the multi-choice structure is the switch case statement.

The switch case statement determines whether a variable is equal to a value in a series of values, each of which is called a branch.

Syntax:

Switch (expression) {case value: / / execution statement break;// optional case value: / / execution statement break;// optional / / any number of case statements default:// optional / / statement}

The variable types in the switch statement can be:

Byte, short, int, or char.

Since Java SE7, switch supports string String types.

At the same time, the case tag must be a string constant or literal.

Public static void main (String [] args) {Scanner scanner = new Scanner (System.in); System.out.println ("Please enter your name"); String name = scanner.nextLine (); switch (name) {case "Li Si": System.out.println (name); break Case "Zhang San": System.out.println (name); break; default: System.out.println ("your name:" + name);}} while loop

While is the most basic loop, and as long as the Boolean expression is true, the loop will continue to execute. Its structure is as follows:

While (Boolean expression) {/ / Loop content}

Example: use the while loop to calculate the sum of 1 / 2 / 3 / 100.

Public static void main (String [] args) {/ / calculates the sum of int I = 0; int sum = 0 / to receive the sum of while (I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report