当然不对了read()方法读出来的不是值,而是读到的字节长度。
得进行转换一下
得进行转换一下
解决方案 »
- id cannot be resolved or is not a field怎么解决?
- 字符串 <blue>blue</blue>
- 问个byte数组的问题
- 有关数组的索引的问题
- 初学请教:为什么会出现这样的错误。谢谢。
- 请教JDK在LINUX下的安装问题
- 请问,我如何实现JAVA与SYBASE数据库的连接!!!
- 在Windows 2000Server上安装J2EE出错了??
- 请教高手:在request对象中取得multipart/form-data格式的二进制数据是不是用request.getInputStream呀?
- java中char和Character...的转换问题
- 求eclipse中开发SERVLET的控件的下载地址
- 新手看书没看懂,请各位大哥帮解释一下,好么?
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);
}
}
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;
}
}
得进行转换一下回复:好象读出来的不是字节长度,是字符的ACSII码值。
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.减一下就行了,上面已经通过;
int result = 0 ;
char ch[] = {(char)code}; if (Character.isDigit(ch[0])) {
result = Integer.parseInt(new String(ch));
}else {
result = 0;
}
return result;
}这个函数就是作转换工作的,并且兼容读入的为任意字符形式。
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个数字之间不能有空格,这样程序执行时可读性差!!!!
很奇怪没有看到书上提到过,兄弟琢磨了很久才发现。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);
}
}//时间关系, 程序未调试
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);
}
}
//调试楼上通过
Integer.parseInt(in.readLine());
修改为Double.parseDouble(in.readLine())就可以了呢?
然后int,float都看作double来处理,在需要的时候再将它们转化过来
我想这么是不是太复杂了?
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);
}
}
这个对