编写Java程序_输入三个整数x,y,z,请把这三个数由小到大输出,请写出实现代码。(3种方法)
2024-10-19 11:57:42
要求说明:
输入三个整数x,y,z,请把这三个数由小到大输出。
实现代码:
第1种方法:
import java.util.Scanner;
public class xyzMaxMin{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入3个整数:");
int x = sc.nextInt();
int y = sc.nextInt();
int z = sc.nextInt();
System.out.println("从小到大排序后的结果:");
if (x < y && x < z) {
if (y < z) {
System.out.println(x + "<" + y + "<" + z);
} else {
System.out.println(x + "<" + z + "<" + y);
}
} else if (y < x && y < z) {
if (x < z) {
System.out.println(y + "<" + x + "<" + z);
} else {
System.out.println(y + "<" + z + "<" + x);
}
} else {
if (x < y) {
System.out.println(z + "<" + x + "<" + y);
} else {
System.out.println(z + "<" + y + "<" + x);
}
}
}
}
第2种方法:
import java.util.Scanner;
public class xyzMaxMin{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入3个整数:");
int x = sc.nextInt();
int y = sc.nextInt();
int z = sc.nextInt();
int i = 0;
if(x>y){
i=y;
y=x;
x=i;
}
if(x>z){
i=z;
z=x;
x=i;
}
if(y>z){
i=z;
z=y;
y=i;
}
System.out.println("从小到大排序后的结果:");
System.out.println(x+"<"+y+"<"+z);
}
}
第3种方法:
import java.util.Arrays;
import java.util.Scanner;
public class xyzMaxMin{
public static void main(String[] args) {
int[] num = new int[3];
Scanner sc = new Scanner(System.in);
System.out.println("请输入3个整数:");
num[0] = sc.nextInt();
num[1] = sc.nextInt();
num[2] = sc.nextInt();
Arrays.sort(num);
System.out.println("从小到大排序后的结果:");
for (int i = 0; i < num.length; i++) {
System.out.print(num[i]+"\t");
}
}
}
最新文章
- codeforces 451D Count Good Substrings
- 使用sessionStorage得一个坑
- NSDate的处理:前一天、后一天等关于时区偏移的处理以及在数据库中的使用
- SQL入门学习2-聚合与排序
- 将SQL获取的信息传递到Email中
- 【模板】AC自动机(加强版)
- [笔试题目]使用Stringbuffer无 参的构造函数创建 一个对象时,默认的初始容量是多少? 如果长度不够使用了,自动增长多少倍?
- 新概念英语(1-37)Making a bookcase
- SQLAlchemy+Flask-RESTful使用(一)
- C语言实现邻接矩阵创建无向图&;图的深度优先遍历
- Error fetching command &#39;collectstatic&#39;: You&#39;re using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path. Command &#39;collectstatic&#39; skipped
- iOS - 富文本直接设置文字的字体大小和颜色
- NET设计模式 第二部分 行为型模式(18):观察者模式(Observer Pattern)
- java截取电脑全屏
- Java学习之——Java Serializable
- LogHelp 日记分天记录,只记30天日记
- Excel Metadata
- 如何使用canvas进行2d绘图
- 期中HTML代码及技术博客
- 一篇博文将JavaScript尽收眼底