新手关于system.in的问题

dxc65659531 2010-01-26 07:53:51
请问:(1)在上机操作时如果有程序内有system.in这样的语句,在编译通过后应怎样输入一串INT形的数字呢?数字之间需要用什么来间隔呢?是空格吗?
(2)还有是不是system.in返回的是输入的数字的ACSLL码呢?
(3)有一个程序就是要求利用system.in来编的,题目是:“利用输入台输入5个整数,输出它们的最大数”我不知道该怎样编,请各位高手帮帮我解释这三个问题谢谢大家
...全文
130 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
存在666 2010-01-27
  • 打赏
  • 举报
回复
import java.io.IOException;

public class Average {
public static void main(String args[]){
int a[]=new int[5];
System.out.println("Please input:");
try {
for(int i=0;i<5;i++){
a[i] = System.in.read()-48;
}
arraySort(a);

} catch (IOException e) {
e.printStackTrace();
}
}


private static void arraySort(int[] a) {
int i,j,temp;
for(i=0;i<a.length-1;i++)
{
for(j=i+1;j<a.length;j++)
{
if(a[j]<a[i])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}

}
System.out.println("最大数:max="+a[a.length-1]);

}


}


只能计算出0-9的数的大小。。。我明天在来改进下!
Please input:
45892
最大数:max=9
yhl_star 2010-01-27
  • 打赏
  • 举报
回复
在以上基础上
比如输入261
最后出的结果就是
1
2
6
yhl_star 2010-01-27
  • 打赏
  • 举报
回复
import java.io.IOException;
import java.util.Arrays;
public class AAA{
public static void main(String args[]) throws IOException {

System.out.println("Please input(0~7):");
int [] input = new int[3];

for (int i = 0 ; i < input.length ; i ++) {
char n = (char)System.in.read();
input[i] = Integer.parseInt(String.valueOf(n));
}

//排序
Arrays.sort(input);

for (int i : input) {
System.out.println(i);
}

}
}
  • 打赏
  • 举报
回复
看8楼
Dazzlingwinter 2010-01-27
  • 打赏
  • 举报
回复

public class InputTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String accept = scanner.nextLine();
String[] str = accept.split(",");
int[] a = new int[str.length];
int max = Integer.parseInt(str[0]);
for(int i=0; i<a.length; i++) {
a[i] = Integer.parseInt(str[i]);
if(max < a[i]) {
max = a[i];
}
}
for(int i=0; i<a.length;i++) {
System.out.print(a[i] + "\t\t");
}
System.out.println();
System.out.println(max);
}
}

//input 12,45,454,45,45,12
//output 12 45 454 45 45 12
//output 454
哎,作为参考吧
Qin_Tianxiang 2010-01-27
  • 打赏
  • 举报
回复
不太懂!来学习一下!system.in()
BearKin 2010-01-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 java1109 的回复:]
用Scanner类
[/Quote]

用这个可以 但是我不建议用里面的readInt 我建议用readLine 然后5个数用空格分开 获得这样一个字符串 然后获得到该字符串的时候 根据空格分为数组并将其转换为INT(当然也可以用readInt) 然后排序 输出
存在666 2010-01-27
  • 打赏
  • 举报
回复
system.in返回的是输入的数字的ACSLL码?

是ACSII码!

首先我们要理解System.in.read(); 的工作原理-->

读入的是ASCII码,0的ASCII码是48开始,所以需要减去48.

当我们在屏幕中输入数据后,是直接放在缓冲区中,记录你每一次的输入!!
而System.in.read();的工作就是依次从这个缓冲区中读取下一个字符的ascii码!

比如现在我们在终端输入23,2和3是分开的,这样我们输出一下read()的值为50,而后我们再read()一次会是51,再read()一次就是13(回车键的ascii码值)了!! 这样我们就应该理解了~~ 呵呵 以前也搞错,现在借这问题学习了,好问题!

而你的程序只read()了一次,所以你输入2位数以上,都只取第一个出现的数字打印出来,那我们现在来测试下!
====================================================================

import java.io.IOException;
class ByteTest{
public static void main(String args[]) throws IOException {
int i, n, sum=1;
System.out.println("Please input(0~7):");
n = System.in.read();
System.out.println("输入时的n:"+n);
n -= 48; //这到底是什么意思??为什么一定非要是48呢?
System.out.println("减48后的n:"+n);
for (i = 1; i <= n; i++)
sum *= i;
System.out.println(n + "!= " + sum);
}
}


我输入2次结果如下:

输入10的时候:
Please input(0~7):
10
输入时的n:49
减48后的n:1
1!= 1

输入4的时候:
Please input(0~7):
4
输入时的n:52
减48后的n:4
4!= 24
dxc65659531 2010-01-27
  • 打赏
  • 举报
回复
你好6楼,我也按类型转换做了,可是不行啊,我还没有看套接的问题,所以不会做的,请各位高手能否对我的问题详细解答吗?谢谢
bayougeng 2010-01-26
  • 打赏
  • 举报
回复
System.in是字节流。是将输入的字符串作为字节数组读入的。
可以输入汉字,汉字也是被解析成字节数组的。
至于数字,其实就是将字符串转换成数值。
如果不套接别的流,就必须自己作类型转换。
昵称很不好取 2010-01-26
  • 打赏
  • 举报
回复
需要对System.in用处理流进行包装
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
reader.readLine();
晴天v1 2010-01-26
  • 打赏
  • 举报
回复
System.in.read()读入的确实是ascii码,不试不知道,输入1,输出的却是另外一个数。

public static void main(String args[]) throws Exception {

BufferedReader reader = null ;

//构造输入流
reader = new BufferedReader(new InputStreamReader(System.in));

int [] input = new int[3];

for (int i = 0 ; i < input.length ; i ++) {
input[i] = Integer.parseInt(reader.readLine());
}

//排序
Arrays.sort(input);

for (int i : input) {
System.out.println(i);
}

reader.close();
}
晴天v1 2010-01-26
  • 打赏
  • 举报
回复
system.in返回的是输入流,要对输入流进行包装。
dxc65659531 2010-01-26
  • 打赏
  • 举报
回复
啥?不懂。。。我的问题是要求用system.in来解决啊
java1109 2010-01-26
  • 打赏
  • 举报
回复
用Scanner类

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧