新黄金矿工普罗旺斯25:请问JAVA中如何接受用户输入数据?

来源:百度文库 编辑:高考问答 时间:2024/04/27 23:03:23
例如C中用scanf("%d",&a);JAVA中用什么命令啊?
那请问如何将用户输入的值付给某个变量啊?
Scanner Class 是自定义的类还是JAVA包里有的啊??

有界面什么的,根本不需要这样。
举个例子,像C一样黑屏中输出用户输入的内容。
try
{
InputStreamReader temp=new InputStreamReader(System.in);
BufferedReader input=new BufferedReader(temp);
System.out.println(input.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}

用Scanner Class
import java.util.Scanner;(一定要import!),Scanner 是Java自带的
Scanner Scanner_Object_Name = new Scanner(System.in);

String Variable_1 = Scanner_Object_Name.next();-- return String: the first characters between two delimiters.

Scanner_Object_Name.nextInt();--return int: the first integer between two delimiters.

Scanner_Object_Name.nextDouble();--return double:

Scanner_Object_Name.nextBoolean();--return Boolean:String"true" or "false"

Scanner_Object_Name.nextLine()--return String:the characters before the \n.

将用户输入的值付给某个变量:
对于primitive type:

Type_Name Variable_Name = Scanner_Object_Name.nextType_Name();

对于 String

String Variable_Name = Scanner_Object_Name.nextLine() 或 next();

JOptionPane.showInputDialog(String prompt)

我建议你细细看看Java输入输出流

function checkinput()