Java Implementation - Type 3 numbers from the keyboard to judge the size and output the largest number

Code

        Idea: first assume that a number is the largest, and then compare and record the largest value in turn. It can also be implemented using conditional operators.

import java.util.Scanner;
public class homework_3_Maxnumber { //homework_3_Maxnumber 为类名,可修改
    public static void main(String[] args) {
        /*
        程序功能:从键盘上键入3个数,判断大小,并输出最大的数
        涉及到数的比较、数的交换和输出
         */
        float a;//提高精度
        float b;
        float c;
        float max;
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入第一个数:");
        a = scanner.nextFloat();
        System.out.println("请输入第二个数:");
        b = scanner.nextFloat();
        System.out.println("请输入第三个数:");
        c = scanner.nextFloat();
        max = a;
        if (max <=b) {
            max = b;
        } else if (max <=c) {
            max = c;
        }
        System.out.println("三个数中的最大数值为:" + max);
    }
}

operation result

 

Guess you like

Origin blog.csdn.net/m0_54158068/article/details/124393887