1,我的意思是读取一个字符,然后判断是不是y,如果是y的话,就进入getage函数
import java.io.*;
public class student {
public static void main(String args[]){
stu s=new stu();
System.out.println("想修改年龄?y/n");
char c=' ';
try{
c=(char)System.in.read();
if(c=='y') {s.getage();System.out.println("好了");System.exit(1);}
else {System.out.println("已退出");System.exit(1);}
}catch(IOException e){}
}
}class stu
{
void getage()
{
int age;
String s="";
try{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
s=in.readLine();
age=Integer.parseInt(s);
}catch(IOException e){}
}
}错误提示是:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at stu.getage(student.java:23)
at student.main(student.java:9)2,如果将上面代码的char c改成String c,然后在try里面用BufferReader进来一串字符,假设是"abc",如果用if(c=="abc") s.getage();判断的话,将什么都不发生,为什么?
import java.io.*;
public class student {
public static void main(String args[]){
stu s=new stu();
System.out.println("想修改年龄?y/n");
char c=' ';
try{
c=(char)System.in.read();
if(c=='y') {s.getage();System.out.println("好了");System.exit(1);}
else {System.out.println("已退出");System.exit(1);}
}catch(IOException e){}
}
}class stu
{
void getage()
{
int age;
String s="";
try{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
s=in.readLine();
age=Integer.parseInt(s);
}catch(IOException e){}
}
}错误提示是:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at stu.getage(student.java:23)
at student.main(student.java:9)2,如果将上面代码的char c改成String c,然后在try里面用BufferReader进来一串字符,假设是"abc",如果用if(c=="abc") s.getage();判断的话,将什么都不发生,为什么?
public class A {
/**
* Creates a new instance of <code>A</code>.
*/
public A() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
stu s=new stu();
System.out.println("y/n");
char c=' ';
try{
c=(char)System.in.read();
if(c=='y') {s.getage();System.out.println("ok");System.exit(1);}
else {System.out.println("quit");System.exit(1);}
}catch(IOException e){}
}
}class stu
{
void getage()
{
int age;
String s="";
try{
System.out.println("ready!");
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
in.skip(2);
s=in.readLine();
age=Integer.parseInt(s);
System.out.println(age);
}catch(IOException e){e.printStackTrace();}
}
}
c=(char)System.in.read();
改成:
Scanner sca = new Scanner(System.in);
c = sca.nextLine().charAt(0);
应该就没有什么问题了。2. String是一个对象不是基本类型不能采用“==”来比较的,比较相等应该采用 c.equals("abc"); 或者是 c.equalsIgnoreCase("abc") 来比较的。在对象中“==”只能比较这两个对象是否相同(或者说是它们的内存地址是否相同),并不是比较其所指向的内容。
to:bao110908(Baobao)
谢谢你的回答,我用了一下Scanner,出错了,主要还没接触Scanner,以后会好一些的吧,我觉得grant999(民)在三楼的解答不错
关于你的第二个解答,我试了一下,很好用,呵呵,谢谢了
===================
to:grant999(民)
恩,谢谢啦,懂了
p.s. in.skip(???)这个数字有几种方式,比如有in.skip(3) or 其他吗?
Scanner是一个很好用的类,是JDK5.0中新增的类,里面有好多 nextXXX()的方法,直接读取不同格式的字符很方便的,不用再进行转换了。