1_Java_judgment

比較


すべての関係演算子は、算術演算よりも優先順位が低くなりますが、代入演算よりも優先順位が高くなります
。equality==および!=は他の演算子よりも優先順位が低く、連続する関係演算は左から右に実行されます。

package hello;

public class Main {
    
    

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		double a = 1.0;
		double b = 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1;
		System.out.println(a);
		System.out.println(b);
		System.out.println(a == b);
		System.out.println(Math.abs(a - b) < 1e6);
		//浮点数的运算有误差 所以用两个数的绝对值小于某一个精度
	}

}

if-else

package hello;

import java.util.Scanner;

public class Main {
    
    

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        System.out.print("请输入你的年龄:");
        int a = in.nextInt();
        System.out.println("你的年龄:" + a);
        if(a < 20) 
        {
    
    
        	System.out.println("你的年龄是美好的!");
        }
        else
        {
    
    
        	System.out.println("该好好努力了!");
        }
        System.out.println("好好珍惜吧!");
	}

}

ブランチ

package hello;

import java.util.Scanner;

public class Main {
    
    

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
        double f;
        if(a > 0)
        {
    
    
        	f = 0.1;
        }else if(a < 0)
        {
    
    
        	f = -0.1;
        }else
        {
    
    
        	f = 0;
        }
        System.out.println(f);
	}

}

スイッチケース

package hello;

import java.util.Scanner;

public class Main {
    
    

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        int type = in.nextInt();
        double f;
        switch(type) {
    
    
        case 1:
        	f = 0.1;
        	break;
        case 2:
        	f = 0.2;
        	break;
        default:
        	f = 0;
        	break;
        }
        System.out.println(f);
	}

}

おすすめ

転載: blog.csdn.net/qq_45459526/article/details/122492434