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

Start from 0 to learn the basics of big data-Java-ternary operator / keyboard entry (4)

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

We learn big data technology from scratch, from the foundation of java, to Linux technology, then to the Hadoop, Spark, Storm technology of big data technology, and finally to the construction of big data enterprise platform, step by step, from point to face! I hope Technology Bull can come to guide the study.

In the previous section, we learned about the Java operator, in which the ternary operator was not explained. In this section, we begin to learn the basics of Java-ternary operator / keyboard input, which will focus on the following knowledge points:

Ternary operator

Keyboard input data

I. operator

1. Ternary operator

Following the topic operators in the previous section, this section talks about ternary operators. Before talking about ternary operators, many friends may ask, is there a unary operator and a binary operator?

The answer is yes, what we have learned before:

Bit operators are unary operators (unit operators), such as: ~ 3

Arithmetic operators are binary operators (binary operators), such as: 3 + 4

So what does the ternary operator look like? let's move on:

Format of the ternary operator:

Compare expressions? Expression 1: expression 2

That is: (?)

Note: the result of the comparative expression is a boolean type

Perform the process:

First calculate the value of the comparative expression to see whether it is true or false

If it is true, expression 1 is the result

If it is false, expression 2 is the result

Take a look at the following example to understand the ternary operator in Java:

Program execution result:

Int z = (x > y)? X: y); statement execution result

Int z = (x < y)? X: y); statement execution result

Int z = (x = = y)? X: y); statement execution result

Int z = (x = y)? X: y); statement execution result

It can be seen that the above statements are based on the return value (true or false) of our first expression to determine the value of the expression to be output later.

Finally, x = y, this statement is an assignment statement that is not of type boolean, so it will report an error, which needs to be noted in the ternary operator of this expression.

II. Keyboard input

1. Keyboard input characteristics:

Purpose: in order to make the data of the program more in line with the data developed, we have added keyboard input to make the program more flexible.

So, how do we achieve keyboard data entry? Mainly through the following three steps:

A, guide package

Format: import java.util.Scanner

Location: enter above class in the Java program code.

B. Create keyboard entry object

Format: Scanner sc = new Scanner (System.in)

C. Get data through objects

Format: int x = sc.nextInt ()

Case study:

Program execution result:

It can be seen that we can now enter data flexibly through the keyboard.

Next, let's do two exercises to see what else our code capabilities can do.

Exercise:

(1) the keyboard inputs two pieces of data, sums the two data, and outputs the results. The program code is as follows:

Program execution result:

(2) enter two pieces of data on the keyboard to get the maximum of the two data. The program code is as follows:

The execution result of the program is as follows:

It can be seen that in the process of compiling the program, we can nest many knowledge points we have learned before and use them together, which helps us to develop our global thinking in the process of learning.

Here I leave two questions for you to complete independently. After you have finished, you can also send me your ideas and final verification results. You are also welcome to communicate with me.

You can send me an email address (cciehelp@qq.com)

Extended exercises (completed by yourself):

(1) enter three data on the keyboard to obtain the maximum value of the three data.

(2) enter two data on the keyboard and compare whether the two data are equal.

We will conclude this section, and in the next section we will learn about process control statements.

Previous articles:

Start from 0 to learn the basics of big data-Java (1)

Learn big data-Java basic Grammar from 0 (2)

Learn the big data-Java operator from 0 (3)

My ability is limited, if there is any deficiency, I hope to correct it.

Thank you for your continued support.

I hope to share it with more people.

Let's learn big data's skills together.

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