JAVA中有这样的语法吗:
一个字符串变量接受键盘的输入:
String input;
input=ConSole.readString();
一个字符串变量接受键盘的输入:
String input;
input=ConSole.readString();
解决方案 »
- 为什么下面的代码执行完毕,结果是12test呢?搞不明白!
- Java中的对象类型转换问题求教
- 一道题目
- 复制Jar包时少了META-INFO/MANIFEST.MF
- 求一表格单元格编辑器(密码框编辑,密码框渲染,取值范围[001,998])。
- 写了一段很简单的实用程序,现在挂在这,望大家多给板砖!!!
- ====有奖竟猜====小牛Vs火箭(5)=== 截止时间:5月03日-8点59分 (是人就该来支持!!)
- 有一个用例实现规约,生成什么样的用例图
- 为何用java调用mysql中表的列名(rs.getString("column_name"))出错?
- 笨鸟求救数组问题(在线等候)
- 如何删除D:\a下的所有文件?但要保留a文件加。他下面的文件都要删除?谢
- java类能嵌套定义吗?请大家看看这个程序
String line = br.readLine();
String str = JOptionPane.showInputDialog("put number");
System.out.println(str);
The console window is the [black] window that is automatically launched when you run a program from within CodeWarrior. Console input is any input that is entered in the console window instead of typing it into a field or dialog box that pops up in a window. For example, when the readLine method is called, the program waits for the user to enter information. Whatever the user types is returned to the program in the form of a String object.
We will use the System.in object to create an instance of the InputStreamReader class and then use that object to create an instance of the BufferedReader class.Steps for console based user input:
Use the System.in object to create an InputStreamReader object.
Use the InputStreamReader object to create a BufferedReader object.
Display a prompt to the user for the desired data.
Use the BufferedReader object to read a line of text from the user.
Do something interesting with the input received from the user.
Would you like to see some code? I thought so. Here it is:
// 1. Create an InputStreamReader using the standard input stream.
InputStreamReader isr = new InputStreamReader( System.in ); // 2. Create a BufferedReader using the InputStreamReader created.
BufferedReader stdin = new BufferedReader( isr ); // 3. Don't forget to prompt the user
System.out.print( "Type some data for the program: " ); // 4. Use the BufferedReader to read a line of text from the user.
String input = stdin.readLine(); // 5. Now, you can do anything with the input string that you need to.
// Like, output it to the user.
System.out.println( "input = " + input );
That's a lot of code for one line of input. Is there a shorter way?
Yes, most Java programmers combine steps 1 & 2 and create only one instance of the BufferedReader for use throughout their entire program. All keyboard operations will use that single shared BufferedReader object. The code below is placed with other class data members and is not inside any method.
// 1&2. Create a single shared BufferedReader for keyboard input.
private static BufferedReader stdin = new BufferedReader(
new InputStreamReader( System.in ) );import java.io.*; // needed for BufferedReader, InputStreamReader, etc. /** A Java program that demonstrates console based input and output. */
public class MyConsoleIO
{
// Create a single shared BufferedReader for keyboard input
private static BufferedReader stdin =
new BufferedReader( new InputStreamReader( System.in ) ); // Program execution starts here
public static void main ( String [] args ) throws IOException
{
// Prompt the user
System.out.print( "Type some data for the program: " );
// Read a line of text from the user.
String input = stdin.readLine(); // Display the input back to the user.
System.out.println( "input = " + input ); } // end main method } // end MyConsoleIO class
Scanner f=new Scanner(System.in)
input=System.in.read();