如何获取用户输入的数字?

skytears 2003-01-04 11:51:04
import java.io.IOException;
class IfElse{
static int ternary(int i){
if (i<10){
i=i*100;}
else{
i=i*(-100);}
return i;
}
public static void main(String args[]){
try{
int i = System.in.read();
//int i=1;
System.out.println("i="+ternary(i));
} catch (IOException e){
System.out.println(e.toString());}

}
}
我输入1时,显示的是4900;
好象是1的ascii码与-100相乘。
...全文
99 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
希偌 2003-01-05
  • 打赏
  • 举报
回复
} catch (IOException e){
-->
} catch (Exception e){
希偌 2003-01-05
  • 打赏
  • 举报
回复
import java.io.*;
public class IfElse{
static int ternary(int i){
if (i<10){
i=i*100;}
else{
i=i*(-100);}
return i;
}
public static void main(String args[]){
try{
int j=0,i = 0;
DataInputStream dis=new DataInputStream(System.in);
j=new Integer(dis.readLine()).intValue();
i=ternary(j);
//int i=1;
System.out.println(j);
System.out.println("i="+ternary(i));
} catch (IOException e){
System.out.println(e.toString());}

}
}
xiaofenguser 2003-01-04
  • 打赏
  • 举报
回复
那就这样用下面的方法(或许有比这个好得多的办法,你可以找一下):
String s=""+(char)System.in.read();
int i = Integer.parseInt(s);
没有判断是否是数字的,你可以加进去.
skytears 2003-01-04
  • 打赏
  • 举报
回复
可我想用ternary()方法
xiaofenguser 2003-01-04
  • 打赏
  • 举报
回复
最后输出的要是那个char型的.
xiaofenguser 2003-01-04
  • 打赏
  • 举报
回复
System.out.println("i="+ternary(i));
改成
System.out.println("i="+i);
你上面的那句又自动转回去了.
skytears 2003-01-04
  • 打赏
  • 举报
回复
结果一样
xiaofenguser 2003-01-04
  • 打赏
  • 举报
回复
char i = (char)System.in.read();

char c=(char)i;

62,614

社区成员

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

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