java: the basics of ternary operators

Introduction

Ternary operator, also known as conditional operator, is an important part of computer language. It is the only operator with 3 operands.
For the conditional expression x = (a> b)? C: d , first calculate the condition (a>b), and then judge. If the value of (a>b) is true, calculate the value of c and x = the value of c; otherwise, calculate the value of d and x = the value of d. A conditional expression will never evaluate both c and d.

example

The java function returns the maximum value of a, b, and c:
there is no ternary operator:

public static int max(int a, int b, int c) {
    
    
	if(a>=b && a>=c)
		return a;
	else if(b>=a && b>=c)
		return b;
	else
		return c;
}

Use the ternary operator:

public static int max(int a, int b, int c) {
    
    
		return (a>=b) ? ((a>=c) ? a : c) : ((b>=c) ? b : c);
	}

It can be easily seen that the ternary operators are nested in each other, which makes the code a lot more concise, but there may be many students who say that my practical if else can also achieve the function of the ternary operator. This operation Fu will actually be a bit redundant. Don't worry, let's take a look at the next example.

Special Purpose

Look at the following topic: Letter Ring Decryption (Fill in the Blanks) Arrange
26 English letters in a clockwise direction into a ring. The key is an integer.

The known encryption methods are as follows:

If the key is greater than or equal to 0, start from the plaintext letter and move ∣key∣ clockwise to get the ciphertext letter;
if the key is <0, start from the plaintext letter and move ∣key∣ counterclockwise to get the ciphertext letter.
The following program inputs ciphertext and key, and outputs plaintext. Please fill in the appropriate content in the blank to complete the procedure.

#include <stdio.h>
int main()
{
    
    
    char plain, secret;
    int key;
    scanf(" %c %d", &secret, &key);
    ____________________
    printf("%c\n", plain);
    return 0;
}

Input format
cipher text (lowercase letters) key (any integer)

Output format
Plain text (lowercase letters)

Input example 1
c 5
Output example 1
x
Input example 2
y -3
Output example 2
b

We can get from the requirements of the question, the answer should be roughly as follows:

plain=secret-key%26;
if(plain<'a') plain+=26;
if(plain>'z') plain-=26; 

I thought the same way before. The local test also happened to correspond, but after the code was submitted, it was wrong. After checking it, it was found that the title was a code fill-in-the-blank question, and only one line was given, and the code I wrote had three OK, the compiler couldn't pass, I thought about it, and thought of the ternary operator, so I got the following code:

plain = (secret - key % 26 < 'a') ? ((secret - key % 26) + 26) : (((secret - key % 26) > 'z') ? ((secret - key % 26) - 26) : secret - key % 26);

After submission, AC, and this question is also more typical, an application of the ternary operator.
In general: the ternary operator has its special way of using. For some problems, the use of the Miki operator can indeed achieve a multiplier effect with half the effort. I hope that this writing can help the students.

Guess you like

Origin blog.csdn.net/qq_19265749/article/details/102985918