输入两个整数a和b,把这两个数由小到大输出。java编程

2024-11-01 18:28:51
推荐回答(2个)
回答1:

给你一个模板,你自己看看就会了。
import java.util.Scanner;

public class $ {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

int size = 3;
int[] arr = new int[size];

for (int i = 0; i < size; i++) {
System.out.print("输入第" + (i + 1) + "个数:");
arr[i] = in.nextInt();
}

回答2:

import java.util.Scanner;
public class Test_1 {

public static void main(String[] args){
Scanner reader = new Scanner(System.in);
int a = reader.nextInt();
int b = reader.nextInt();
if(a>b)
{
System.out.println(a);
System.out.println(b);
}
else
{
System.out.println(b);
System.out.println(a);
}

}
}

或者

import java.util.Scanner;
public class Main {

public static void main(String[] args){
Scanner reader = new Scanner(System.in);
int a = reader.nextInt();
int b = reader.nextInt();
System.out.println(a>b?a:b);
System.out.println(a>b?b:a);
}
}