黑马程序员技术交流社区
标题:
键盘录入3个数,倒序输出
[打印本页]
作者:
shenzhinishimen
时间:
2016-6-7 22:38
标题:
键盘录入3个数,倒序输出
分析以下需求,并用代码实现:
(1)键盘录入三个整数,按照从小到大的顺序输出
(2)如果用户输入的是3 2 1,程序运行后打印格式"按照从小到大排序后的顺序为:1 2 3"
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
//1.创建键盘录入对象
Scanner sc = new Scanner(System.in);
//2.通过键盘录入输入三个整数
System.out.println("请输入第一个整数:");
int x = sc.nextInt();
System.out.println("请输入第二个整数:");
int y = sc.nextInt();
System.out.println("请输入第三个整数:");
int z = sc.nextInt();
int temp;
//3.将x,y,z中的最小数存入到x中
if(x>y) {
temp = x;
x = y;
y = temp;
}
if(x>z) {
temp = x;
x = z;
z = temp;
}
//4.将y,z中的最小数存入到y中
if(y>z) {
temp = y;
y = z;
z = temp;
}
//输出结果
System.out.println("x="+x+",y="+y+",z="+z);
}
}
作者:
18634319112
时间:
2016-6-7 22:47
感觉你这样有点麻烦了,直接把他们放进集合中 ,然后倒序循环输出 这个是我今天的作业题,给你参考下package Test1;
import java.util.Scanner;
/*编写程序,将键盘录入的字符串倒序打印,并测试
*/
public class Test2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串");
String s = sc.nextLine();
char[] ch = s.toCharArray();
for (int i =ch.length-1; i>=0 ; i--) {
System.out.print(ch[i]);
}
}
}
作者:
ancheng
时间:
2016-6-7 22:54
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
sc.close();
String[] s = str.split(" ");
int[] num = new int[s.length];
for (int i = 0; i < s.length; i++) {
num[i] = Integer.parseInt(s[i]);
}
Arrays.sort(num);
for (int i : num) {
System.out.println(i);
}
}
}
复制代码
作者:
ljd
时间:
2016-6-7 23:31
你可以直接放数组中,用sort()方法排序输出
作者:
我是你岁哥❤环
时间:
2016-6-7 23:40
代码很清晰,注释也很好
作者:
碧螺春_
时间:
2016-6-7 23:57
可以有,学习了
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2