初学java,求java简单练习题目。最好是关于if选择结构。如多重if,嵌套if。越多越好。谢谢!
1个回答
展开全部
import java.util.Scanner;
/**
* 由控制台输入三个数,并升序排序
*
* @author young
*
*/
public class Example {
public static void main(String[] args) {
int num1, num2, num3;
Scanner sc = new Scanner(System.in);
System.out.println("输入第一个数:");
num1 = sc.nextInt();
System.out.println("输入第二个数:");
num2 = sc.nextInt();
System.out.println("输入第三个数:");
num3 = sc.nextInt();
/* 进行排序 */
if (num1 >= num2) {
if (num2 >= num3) {
System.out.print(num3 + "<=" + num2 + "<=" + num1);
} else {
if (num1 >= num3) {
System.out.print(num2 + "<=" + num3 + "<=" + num1);
} else {
System.out.print(num2 + "<=" + num1 + "<=" + num3);
}
}
} else {
if (num1 >= num3) {
System.out.print(num3 + "<=" + num1 + "<=" + num2);
} else {
if (num2 >= num3) {
System.out.print(num1 + "<=" + num3 + "<=" + num2);
} else {
System.out.print(num1 + "<=" + num2 + "<=" + num3);
}
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询