请帮我看看这个程序,很简单!!!但是我就是不明白!!

LoveLL810625 2003-10-15 10:45:23
import java.io.*;
public class GetTwoNumbers
{
public static void main(String[] args)
{
int a=0;int b=0;int c=0;
System.out.print("Please input two numbers: ");
try{
a=System.in.read();
b=System.in.read();
}
catch(IOException e){}
System.out.println(a);
System.out.println(b);
c=a*b;
System.out.println("a*b="+c);
}
}

程序是用来接受2个数字,然后对其求积
我得到的结果不对,请问为什么?
...全文
46 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
jhzhou882 2003-10-22
  • 打赏
  • 举报
回复
import java.io.*;
public class multiply{
public static void main(String[] args) throws IOException {
BufferedReader in =
new BufferedReader(new InputStreamReader(System.in));

System.out.print("please enter the a: ");
int a = Integer.parseInt(in.readLine());
System.out.print("please enter the b: ");
int b= Integer.parseInt(in.readLine());
int c = a * b;
System.out.println("a * b = " + c);
}
}
这个对
ayayanvren 2003-10-22
  • 打赏
  • 举报
回复
那用try{}catch(){}吧,捕捉造型转换错误。
LoveLL810625 2003-10-20
  • 打赏
  • 举报
回复
楼上的方法的确不错,但是如果我想输入的数没有类型的规定,也就是说,可以输入任何类型的(包括int,float,double等),又该怎么办呢?是不是可以将
Integer.parseInt(in.readLine());
修改为Double.parseDouble(in.readLine())就可以了呢?
然后int,float都看作double来处理,在需要的时候再将它们转化过来
我想这么是不是太复杂了?
meijing 2003-10-20
  • 打赏
  • 举报
回复
是有些烦,不知哪位大侠有更好的方法
imagex 2003-10-19
  • 打赏
  • 举报
回复
不用谢
meijing 2003-10-18
  • 打赏
  • 举报
回复
to imagex(境): 谢谢
imagex 2003-10-17
  • 打赏
  • 举报
回复
import java.io.*;
public class multiply{
public static void main(String[] args) throws IOException {
BufferedReader in =
new BufferedReader(new InputStreamReader(System.in));

System.out.print("please enter the a: ");
int a = Integer.parseInt(in.readLine());
System.out.print("please enter the b: ");
int b= Integer.parseInt(in.readLine());
int c = a * b;
System.out.println("a * b = " + c);
}
}
//调试楼上通过
meijing 2003-10-17
  • 打赏
  • 举报
回复
JAVA中的read和C++中的不一样,要用以下的方法读入数据。
很奇怪没有看到书上提到过,兄弟琢磨了很久才发现。

import java.io.*;
public class multiply{
public static void main(String[] args) throws IOException {
BufferedReader in =
new BufferedReader(new InputStreamReader(System.in));

System.out.print("please enter the a: ");
int a = Integer.parseInt(in.readLine());
System.out.print("please enter the b: ");
int b= Integer.parseInt(in.readLine());
int c = a * b;
System.out.println("a * b = " + c);
}
}//时间关系, 程序未调试
LoveLL810625 2003-10-17
  • 打赏
  • 举报
回复
import java.io.*;
public class GetTwoNumbers
{
public static void main(String[] args)
{
int a=0;int b=0;int c=0;
System.out.print("Please input two numbers: ");
try{
a=System.in.read();
b=System.in.read();
a-=48; //改的部分
b-=48; //
}
catch(IOException e){}
System.out.println(a);
System.out.println(b);
c=a*b;
System.out.println("a*b="+c);
}
}
读出的是ASCII的值0=48,1=49,......9=57.减一下就行了,上面已经通过;




回复:好象你的那个程序还有问题:我只能输入0-9之间的数,如果想输入>9的数好象就不行了,而且2个数字之间不能有空格,这样程序执行时可读性差!!!!
LoveLL810625 2003-10-16
  • 打赏
  • 举报
回复
当然不对了read()方法读出来的不是值,而是读到的字节长度。
得进行转换一下



回复:好象读出来的不是字节长度,是字符的ACSII码值。
liuchuan 2003-10-16
  • 打赏
  • 举报
回复
用InputStream的readLine()方法读取输入
sisleydeng 2003-10-16
  • 打赏
  • 举报
回复
private static int encoder(int code){
int result = 0 ;
char ch[] = {(char)code};

if (Character.isDigit(ch[0])) {
result = Integer.parseInt(new String(ch));
}else {
result = 0;
}
return result;
}

这个函数就是作转换工作的,并且兼容读入的为任意字符形式。
chifengwatch 2003-10-16
  • 打赏
  • 举报
回复
up
sisleydeng 2003-10-16
  • 打赏
  • 举报
回复
TRY:

import java.io.*;
public class GetTwoNumbers
{
public static void main(String[] args)
{
int a=0;int b=0;int c=0;

System.out.print("Please input two numbers: ");
try{
a=System.in.read();
b=System.in.read();
}
catch(IOException e){}

a = encoder(a);
b = encoder(b);

System.out.println(a);
System.out.println(b);
c=a*b;
System.out.println("a*b="+c);
}

private static int encoder(int code){
int result = 0 ;
char ch[] = {(char)code};

if (Character.isDigit(ch[0])) {
result = Integer.parseInt(new String(ch));
}else {
result = 0;
}
return result;
}
}
无欲则钢 2003-10-16
  • 打赏
  • 举报
回复
import java.io.*;
public class GetTwoNumbers
{
public static void main(String[] args)
{
int a=0;int b=0;int c=0;
System.out.print("Please input two numbers: ");
try{
a=System.in.read();
b=System.in.read();
a-=48; //改的部分
b-=48; //
}
catch(IOException e){}
System.out.println(a);
System.out.println(b);
c=a*b;
System.out.println("a*b="+c);
}
}
读出的是ASCII的值0=48,1=49,......9=57.减一下就行了,上面已经通过;
imagex 2003-10-16
  • 打赏
  • 举报
回复
import javax.swing.*;
public class GetTwoNumbers
{
public static void main(String[] args)
{
int a=0;int b=0;int c=0;


String xyy = JOptionPane.showInputDialog("输入第一个数");
a = Integer.parseInt(xyy);

xyy = JOptionPane.showInputDialog("输入第二个数");
b = Integer.parseInt(xyy);

System.out.println(a);
System.out.println(b);
c=a*b;
System.out.println("a*b="+c);
}
}
Mars_wx 2003-10-15
  • 打赏
  • 举报
回复
waiting
LoveRose 2003-10-15
  • 打赏
  • 举报
回复
当然不对了read()方法读出来的不是值,而是读到的字节长度。
得进行转换一下

62,612

社区成员

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

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