一个很简单的输入输出问题,但~~
import java.io.*;
public class Try
{
static BufferedReader cin
= new BufferedReader(new InputStreamReader(System.in));
private int n;
static int getNo()throws IOException
{
System.out.print("Give New Student no : ");
return cin.read();
}
public static void main(String[] agro)
{
Try test= new Try();
n= test.getNo();
System.out.println(n);
}
}
问题点数:50、回复次数:7Top
1 楼javafaq2004(I will survive)回复于 2004-12-04 03:32:17 得分 20
静态方法访问实例变量
可改成:
import java.io.*;
public class Try
{
static BufferedReader cin
= new BufferedReader(new InputStreamReader(System.in));
private int n;
static int getNo()throws IOException
{
System.out.print("Give New Student no : ");
return cin.read();
}
public static void main(String[] agro)throws IOException
{
Try test= new Try();
test.n = test.getNo();
System.out.println(test.n);
}
}
还可改成:
import java.io.*;
public class Try
{
static BufferedReader cin
= new BufferedReader(new InputStreamReader(System.in));
private String n;
static String getNo()throws IOException
{
System.out.print("Give New Student no : ");
return cin.readLine();
}
public static void main(String[] agro)throws IOException
{
Try test= new Try();
test.n = test.getNo();
System.out.println(test.n);
}
}Top
2 楼letaon(只影)回复于 2004-12-04 03:43:18 得分 0
为什么要test.n?不是已经private 了吗?Top
3 楼letaon(只影)回复于 2004-12-04 03:51:22 得分 0
如果我用上面这个代码,输入200,输出却是50,为什么?
but the second one this correct. why?Top
4 楼letaon(只影)回复于 2004-12-04 03:53:38 得分 0
但是我要的是输入一个interger,不是String,这样还是可以吗?Top
5 楼Avampire()回复于 2004-12-04 08:42:10 得分 10
要用test.n这个不是太清楚,应该是因为你在main方法里引用的;
输入200输出50是因为你上面用的是read()没读完,下面的readline()读完了;
输入的是interger的话,读取进来后把string转换成interger就是了。
Top
6 楼classjava(原始野人)回复于 2004-12-04 09:07:40 得分 19
import java.io.*;
public class Test
{
static BufferedReader cin
= new BufferedReader(new InputStreamReader(System.in));
private int n;
public int getNo()throws IOException
{
System.out.print("Give New Student no : ");
n=Integer.parseInt(cin.readLine());
return n;
}
public static void main(String[] agro)
{
Test test= new Test();
System.out.println(test.n);
}
}
Top
7 楼fzq426(天涯海角 不见不散)回复于 2004-12-15 17:41:27 得分 1
基本的问题你还是要多看看书啊Top




