刚开始学习java,请问java中输入方法是read()吗?为什么下面的程序编译时显示read()方法不对那?
public class P {
public static void main(String args[])
{double a,b;
System.out.println("input a number please:");
System.in.read(a);
b=Math.sqrt(a);
System.out.println("平方根为"+b);
}
}
public class P {
public static void main(String args[])
{double a,b;
System.out.println("input a number please:");
System.in.read(a);
b=Math.sqrt(a);
System.out.println("平方根为"+b);
}
}
你可以把double值作为String读进来,然后将String转换为double,这个很简单的
import java.io.*;public class StringInput {
public static void main(String args[]) {
try {
BufferedReader in = null;
in = new BufferedReader(new InputStreamReader(System.in)); // 从键盘接收了一个字符串的输入,并创建了一个字符输入流的对象
String s = null;
s = in.readLine();// 从输入流in中读入一行,并将读取的值赋值给字符串变量s,然后将s转换为double
} catch (IOException e) {
System.out.println(e);
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;public class P{
public static void main(String args[]) throws IOException
{
double a=0;
double b=0;
System.out.println("input a number please:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
a = Double.parseDouble(br.readLine());
b=Math.sqrt(a);
System.out.println("平方根为"+b);
}
}
可以用read的 ····public class Test {
public static void main(String args[]) throws Exception
{double a=0,b;
System.out.println("input a number please:");
byte[] buf=new byte[100];
int len=System.in.read(buf);
String str=new String(buf,0,len);
a=Double.parseDouble(str);
b=Math.sqrt(a);
System.out.println("平方根为"+b);
}
}