Home  >  Article  >  Java  >  Java ternary operator puzzle

Java ternary operator puzzle

王林
王林forward
2023-09-23 08:17:021278browse

Java ternary operator puzzle

What are operators in Java environment?

The

operator is some special characters or symbols or data indicators in the Java environment that can perform certain specific operations. There are multiple operands (variables) that can participate in the operation here. After successful compilation, we get the desired output. There are many operators in the Java language that are mainly used to manipulate the actual values ​​of key variables present in the Java build code.

For ternary operations, there is a condition, followed by a question mark (?), then an expression that executes the method if the condition is true, followed by a colon (:), and finally an expression that executes if the condition is true. of. Ternary statements should only be used when the resulting statement is too short to perform the procedure. Otherwise, you should write ordinary statements as "if statements".

The purpose of the ternary operator is for practical purposes, making code more concise and readable in real-world environments. Moving complex if statements to the ternary operator defeats that goal. The ternary operator in Java programming is a conditional expression that can be used to replace the "if...else" condition to perform certain operations. In this article, we will learn how to implement Java ternary operator puzzle using some logic and algorithms for some specific situations.

What is the ternary operator?

  • The binary operator is a Java environment operator that takes three integer data types to perform specific operations.

  • In Java programming, methods of bypassing "if....else" conditions to reduce code complexity are widely used.

  • As another way of expressing the "if...else" method, it follows the same algorithm but takes up less memory space.

  • The ternary operator consists of three parts; as contains a Boolean expression, a True part, and a false part. Based on these inputs, it is decided whether a given condition is TRUE or FALSE.

  • The ternary operator works based on the result of a given condition (condition, expression 1, expression 2).

  • One operand is of type T (byte), one is a short of char value, and the other is a constant; it only represents the type T of the conditional expression.

Ternary Operator Algorithm

  • Step 1 - Start the program.

  • Step 2 - Declare variables.

  • Step 3 - Enter the value of int

  • Step 4 - Check the condition via the ternary (conditional) operator.

  • Step 5 - Show Answer.

  • Step 6 - Kill the process.

grammar

condition to be checked? value_find_if_true : value_find_if_false
class Solutionternaryop {
   public static String getNearestNumber(String number1, int dist1, String number2, int dist2) {
      return "?";
   }
}
Or;
Condition check ? expression given no.1 : expression given no.2;

This statement is evaluated to determine whether the condition is true or false. Below is an example that distinguishes the difference between an "if….else" condition and a "ternary operator".

"if…..else" condition:

int x = 10, y = 20, ans;
if (x == 10) {
   if (y == 20) {
      ans = 30;
   } else {
      ans = 50;
   }
} else {
   ans = 0;
}
printf ("%d\n", ans);

"Ternary operator":

int x = 10, y = 20, ans;
ans = (x == 10 ? (y == 2 ? 30 : 50) : 0);
printf ("%d\n", ans);

Different methods to follow:

  • Method 1 - Find the largest of two numbers

  • Method 2 - Java Ternary Operator Puzzle

Method 1: Find the largest number among two numbers

Here, we have implemented various logic using the ternary operator to get the maximum number. For this process here, the time and auxiliary space complexity are both O(1).

Example 1

import java.io.*;
public class Ternaryextupoint {
	public static void main(String[] args){	
		int a1 = 50, a2 = 100, max;
		System.out.println("First num coming by as: " + a1);
		System.out.println("Second num coming by as: " + a2);	
		max = (a1 > a2) ? a1 : a2;	
		System.out.println("The Maximum Is Here = " + max);
   }
}

Output

First num coming by as: 50
Second num coming by as: 100
The Maximum Is Here = 100

Example 2

import java.io.*;
public class Ternarypotupoint {
	public static void main(String[] args){	
		int s1 = 500, s2 = 100, res;
      System.out.println("First num mentioned in the data: " + s1);
		System.out.println("Second num mentioned in the data: " + s2);
		res = (s1 > s2) ? (s1 + s2) : (s1 - s2);
		System.out.println("Result: Here the largest number is - = " + res);
	}
}

Output

First num mentioned in the data: 500
Second num mentioned in the data: 100
Result: Here the largest number is - = 600

Example 3

public class Ternarybubooleantupoint {
	public static void main(String[] args){
		boolean condition = true;
		String result = (condition) ? "True It Is" : "False It Is";
		System.out.println(result);
	}
}

Output

True It Is

Ternary operator puzzle using Java environment

By using the ternary operator puzzle, we can find whether the statement value is true or false.

Example 1

public class ternaryoppuzzle {
	public static void main(String[] args) {
      char y = 'Y';
      int k = 0;
      System.out.print(true ? y : 0);
      System.out.print(false ? k : y);
	}
}

Output

Y89

After executing the program, we can see that the output here is Y89. If we decode it, X is the first statement and 89 is the second statement.

in conclusion

Through this article we learned about the ternary operator method in the Java language. Here we experience the many advantages of the ternary operator by using it to build a puzzle. The readability, performance, and ability to bypass if-else statements using nested compact functions is what makes this feature so unique in Java.

The above is the detailed content of Java ternary operator puzzle. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete