Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the example problem of three monks by java

Shulou Source: shulou.com Published: 2022-06-01 02:56:41 09月26日 Update

This article is to share with you about how java solved the example problem of three monks. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Three monks

1. Title:

There are three monks living in a temple. Their height must be measured. Please use the program to obtain the tallest height of the three monks.

two。 Analysis:

The height is unknown, which is realized by keyboard input. (first guide the package, then create the object)

The keyboard enters three heights and assigns values to three variables

The higher height values of the first two monks are obtained by ternary operator and saved with temporary height variable.

The temporary height value obtained by the ternary operator is compared with the height of the third monk and saved with the maximum height variable.

Output the results.

3. Practice:

The code is as follows (example):

Public class Demo2 {public static void main (String [] args) {/ / do not like ginger but not jealous / / height is unknown, it is realized by keyboard input; / / (first import package import java.util.Scanner;, and then create object) Scanner sc=new Scanner (System.in); / / keyboard input three height values are assigned to three variables Int height1=sc.nextInt (); int height2=sc.nextInt (); int height3=sc.nextInt (); / / use the ternary operator to obtain the higher height values of the first two monks and save them with temporary height variables: int tempheight = height1 > height2? Height1:height2; / / use the ternary operator to obtain the temporary height value and compare it with the height of the third monk, and save it with the maximum height variable; int maxheight=tempheight > height3? Tempheight:height3; / / output the result. System.out.print (maxheight);}} Thank you for reading! This is the end of the article on "how to solve the example problems of three monks in java". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Height three monks variables operators keyboard ternary operations examples problems maximum two content objects more articles results output good practical Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Tech Info Xiaomi Redmi MySQL