if-else练习
练习1
import java.util.Scanner; public class V{ public static void main(String[] args){ Scanner s=new Scanner(System.in); System.out.println("请输入第一个数"); int num1=s.nextInt(); System.out.println("请输入第二个数"); int num2=s.nextInt(); System.out.println("请输入第三个数"); int num3=s.nextInt(); if (num1>num2) { if (num3 < num2) System.out.println(num3 + "," + num2 + "," + num1); else if (num3 > num1) System.out.println(num2 + "," + num1 + "," + num3); else System.out.println(num2 + "," + num3 + "," + num1); }else{ if (num3<num1) System.out.println(num3+","+num1+","+num2); else if (num3>num2) System.out.println(num1+","+num2+","+num3); else System.out.println(num1+","+num3+","+num2); } } }
注意:如果if-else if-else条件的执行语句块{}只有一条语句的话,那么这一对{}可以省略
练习2
import java.util.Scanner; public class V{ public static void main(String[] args){ Scanner s=new Scanner(System.in); System.out.println("身高:(cm)"); int cm=s.nextInt(); System.out.println("财富:(千万)"); double money=s.nextDouble(); System.out.println("帅否:(true/false)"); Boolean face=s.nextBoolean(); if (cm>180 && money>1 && face==true){ System.out.println("我一定要嫁给他!!!"); }else if (cm>180 || money>1 || face==true) { System.out.println("嫁吧,比上不足,比下有余"); }else{ System.out.println("不嫁"); } } }