import java.io.*; //从键盘输入一个整数,求100除以它的商,并显示。
public class Test1 {
public static void main(String[] args)throws Exception {
try{
byte buf[] = new byte[50];
System.out.print("input:"); //输入小于100整数
int count = System.in.read(buf);
String strBuf = new String(buf,0,count);
System.out.println(strBuf);
int n=Integer.parseInt(strBuf);
System.out.println("n:"+n);
int m = 100/n;
System.out.println(m);/**/
}catch(Exception e){System.out.print("Error:"+e.getMessage());}
}
}
public class Test1 {
public static void main(String[] args)throws Exception {
try{
byte buf[] = new byte[50];
System.out.print("input:"); //输入小于100整数
int count = System.in.read(buf);
String strBuf = new String(buf,0,count);
System.out.println(strBuf);
int n=Integer.parseInt(strBuf);
System.out.println("n:"+n);
int m = 100/n;
System.out.println(m);/**/
}catch(Exception e){System.out.print("Error:"+e.getMessage());}
}
}
java.lang.下的包是自动导入的,你导入它干吗?
in.read()方法读取的是键盘输入的值 ASC||码
我觉得是这样 呵呵 一个学生的看法 不知道对不?
int n=Integer.parseInt(strBuf);
int n=Integer.parseInt(strBuf.trim());
tring strBuf = new String(buf, 0, count);//不可以,因为 会把输入流中的\r\n (即回车和换行算进来)细心看这个错误会发现 100后面有个换行 ,问题就出在 这儿。
For input string: "100
"接分了
int count = System.in.read(buf);
String strBuf = new String(buf,0,count);
你的count在读取的时候,得到的不是你输入数据的长度,这就导致你的strBuf在初始化时,后面有可能带上的一些空格,这样的话,在int n=Integer.parseInt(strBuf)时就会出现NumberFormatException了。
public static void main(String[] args) throws Exception {
try {
byte buf[] = new byte[50];
System.out.print("input:"); // 输入小于100整数
int count = System.in.read(buf);
System.out.println(count);
String strBuf = new String(buf, 0, count);
int n = Integer.parseInt(strBuf.trim());
System.out.println("n:" + n);
int m = 100 / n;
System.out.println(m);/**/ } catch (Exception e) {
e.printStackTrace();
}
}
import java.io.*; //从键盘输入一个整数,求100除以它的商,并显示。
public class Test1 {
public static void main(String[] args)throws Exception {
try{
byte buf[] = new byte[50];
System.out.print("input:"); //输入小于100整数
int count = System.in.read(buf);
String strBuf = new String(buf,0,count - 2);
System.out.println("strBuf = " + strBuf);
int n=Integer.parseInt(strBuf);
System.out.println("n:"+n);
int m = 100/n;
System.out.println(m);/**/ }catch(Exception e)
{
e.printStackTrace();
}
}
}