Home > Java > javaTutorial > body text

Ternary operator, array method and integer comparison code in Java

不言
Release: 2018-09-11 14:02:05
Original
3419 people have browsed it

The content of this article is about the ternary operator and array method in Java and the code for integer comparison. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. .

Code block

The code is as follows, for example:

//class前,导入的java包import java.util.Scanner;

//我只把主方法列出来了
public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 接收用户输入        
        System.out.println("清输入第1个数:");        
        int a = scanner.nextInt();        
        System.out.println("清输入第2个数:");        
        int b = scanner.nextInt();        
        System.out.println("清输入第3个数:");        
        int c = scanner.nextInt();        
        System.out.println("清输入第4个数:");        
        int d = scanner.nextInt();

        // 三目运算解决        
        int m = a > b ? a : b;
        int n = c > d ? c : d;
        int x = m > n ? m : n;
        System.out.println("max=" + x);

        // 分支方法解决
        int t;        
        if (a > b) {
            t = a;  a = b;  b = t;  
            }        
        if (a > c) {
            t = a;  
            a = c;  
            c = t;  
            }  
         if (a > d) {
             t = a;  
             a = d;  
             d = t;  
             }        
         if (b > c) {
             t = b;  
             b = c;  
             c = t;  
             }        
         if (b > d) {
             t = b;  
             b = d;  
             d = t;  
             }        
         if (c > d) {
             t = c;  
             c = d;  
             d = t;  
             }
        System.out.println("max=" + d);

        // 数组方法解决
        int p[] = new int[4];        
        for (int i = 0; i < 4; i++) {
            System.out.println("请输入第" + (i + 1) + "个数字:");
            p[i] = scanner.nextInt();
        }        
        for (int i = 0; i < 3; i++) {            
        if (p[i] > p[i + 1]) {
                // 这里也可以设置一个中间变量t来进行交换
                p[i] = p[i] + p[i + 1];
                p[i + 1] = p[i] - p[i + 1];
                p[i] = p[i] - p[i - 1];
            }
        }
        System.out.println("max=" + p[3]);// 最大的就放在数组最后了
    }
Copy after login

Related recommendations:

Judge array equality in php Introduction to methods and array operators, array operators

The above is the detailed content of Ternary operator, array method and integer comparison code in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!