标题: 对于if语句的用法 [打印本页] 作者: Jason_bao 时间: 2016-7-24 12:24 标题: 对于if语句的用法 如何在只用if语句实现从键盘录入三个整数,并按从小到大的顺序输出?作者: 无聊此刻 时间: 2016-7-24 12:54
import java.util.Scanner;
class Demo1
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("输入三个数,请输入第一个数");
int a =sc.nextInt();
System.out.println("请输入第二个数");
int b =sc.nextInt();
System.out.println("请输入第三个数");
int c =sc.nextInt();
if (a > b && b > c) {
System.out.println("从大到小依次为:"+ c +","+ b +","+ a);
}else if (a > c && c > b ) {
System.out.println("从大到小依次为:"+ b +","+ c +","+ a);
}else if (b > c && c > a) {
System.out.println("从大到小依次为:"+ a +","+ c +","+ b);
}else if (b > a && a > c) {
System.out.println("从大到小依次为:"+ c +","+ a +","+ b);
}else if (c > a && a > b) {
System.out.println("从大到小依次为:"+ b +","+ a +","+ c);
}else if (c > b && b > a) {
System.out.println("从大到小依次为:"+ a +","+ b +","+ c);
}
}
}
//...if的感觉有点麻烦了...不过用起来还是不错的作者: Frank_Ms1ZR 时间: 2016-7-24 13:51
import java.util.Scanner;
class Test12 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个整数");
int x = sc.nextInt();
System.out.println("请输入第二个整数");
int y = sc.nextInt();
System.out.println("请输入第三个整数");
int z = sc.nextInt();
int max = (((x > y) ? x : y) > z) ? ((x > y) ? x : y) : z;
int min = (((x < y) ? x : y) < z) ? ((x < y) ? x : y) : z;
int mid = x + y + z - max - min;
System.out.println("按照从小到大排序后的顺序为:" + min + " " + mid + " " + max);作者: miqideliwu 时间: 2016-7-24 14:16
/**
只用if语句实现从键盘录入三个整数,并按从小到大的顺序输出
*/
import java.util.Scanner; //导包
class HomeWork7 {
public static void main(String[] args) {
//System.out.println("Hello World!");
Scanner sc = new Scanner(System.in); //创建Scanner类的对象
System.out.println("请输入第一个整数:");
int x = sc.nextInt(); //键盘录入的第一个数存入x中
System.out.println("请输入第二个整数:");
int y = sc.nextInt(); //键盘录入的第一个数存入y中
System.out.println("请输入第三个整数:");
int z = sc.nextInt(); //键盘录入的第一个数存入z中
int temp = 0;
int temp1 = 0;
int temp2 = 0;
if (x < y){
if (x < z){
temp = x;
if (y < z){
temp1 = y;
temp2 = z;
}else {
temp1 = z;
temp2 = y;
}
}else {
temp = z;
temp1 = x;
temp2 = y;
}
}else if (y < z){
temp = y;
if (x < z){
temp1 = x;
temp2 = y;
}else {
temp1 = y;
temp2 = x;
}
int max=(num1>num2?num1:num2)>num3?(num1>num2?num1:num2):num3;
int min=(num1<num2?num1:num2)<num3?(num1<num2?num1:num2):num3;
int mid=((num1>num2?num1:num2)>num3?num3:(num1>num2?num1:num2))>(num1>num2?num2:num1)?((num1>num2?num1:num2)>num3?num3:(num1>num2?num1:num2)):(num1>num2?num2:num1);