用户无法输入字符串变量的值。
这是该计划:
import java.io.*;
public class testing
{
char a;
int b;
double c;
String d;
public void method() throws IOException
{
BufferedReader p=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a variable of type 'char'");
a = (char)p.read();
System.out.println("Enter a variable of type 'string'");
d = p.readLine();
System.out.println("Enter a variable of type 'integer'");
b = Integer.parseInt(p.readLine());
System.out.println("Enter a variable of type 'double'");
c = Double.parseDouble(p.readLine());
System.out.println("Your values are: string-" +d +", integer-" +b +", double-" +c);
}
}
有人可以看到问题吗?
答案 0 :(得分:5)
我测试了它,你的程序按预期工作。唯一的问题是您使用read
来读取char,但在用户按Enter键之前,Java不会看到任何用户输入。看我的测试运行:
Enter a variable of type 'char'
cchar
Enter a variable of type 'string'
Enter a variable of type 'integer'
12
Enter a variable of type 'double'
12
Your values are: string-char, integer-12, double-12.0