input output in javajava中的输入输出

上传人:自*** 文档编号:80044186 上传时间:2019-02-18 格式:DOCX 页数:4 大小:66.83KB
返回 下载 相关 举报
input  output in javajava中的输入输出_第1页
第1页 / 共4页
input  output in javajava中的输入输出_第2页
第2页 / 共4页
input  output in javajava中的输入输出_第3页
第3页 / 共4页
input  output in javajava中的输入输出_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

《input output in javajava中的输入输出》由会员分享,可在线阅读,更多相关《input output in javajava中的输入输出(4页珍藏版)》请在金锄头文库上搜索。

1、Input / Output in JavaInput/Output in Java can be done using the keyboard and screen, using files, or some combination of these methods. Input typed at the keyboard and output displayed on the screen are often referred to as console input/output. Interactive input/output using the keyboard and scree

2、n is the default way of doing input/output in jGRASP. It is also possible to use a dialog box to get input from the user and to display a message. Java refers to these as an input dialog and message dialog respectively. An object is always needed to perform an input/output operation in Java.Display

3、Output standard output Displaying screen output in Java is done using the System.out object. You do not need to create the System.out object. It is always available for you use in a Java program. There are 3 methods that can be used to display output: print( ), printf( ), and println( ).o The print(

4、 ) method is often used to display a prompt or a portion of a line. It does not move the cursor to the beginning of a new line automatically.o The println( ) method displays output exactly the same as the print( ) method except that it will always move the cursor to the beginning of a new line as th

5、e last action it performs.o The printf( ) method is used to output formatted text and numbers. Like the print( ) method, It does not move the cursor to the beginning of a new line automatically.Keyboard Input standard input Accepting keyboard input in Java is done using a Scanner object. There is a

6、System.in object in Java that can be used to get input in Java, but it is not very functional. Instead, Java programmers usually prefer to use a Scanner object that has been mapped to the System.in object. The System.in object ( like the System.out object ) already exists and is available for use. H

7、owever, you must create a Scanner object. Once you have created a Scanner object and mapped it to the System.in object you can use the following methods to test for and get input. Input always comes in to a Java program as a string of characters ( Java String type ). There are methods that can be us

8、ed to convert the String to a character, an integer number, a floating-point number, etc. Here are commonly used Scanner methods:hasNext()/is something available to read? Returns true or false.nextInt()/get an int nextFloat()/get a floatnextDouble()/get a doublenextBoolean()/get a boolean value ( tr

9、ue or false )next()/get a String ( delimited by whitespace )nextLine()/get the rest of the line as a StringNote: There is no method to read in a character! Use a string variable instead. Examples of Interactive input/output in JavaUse a Scanner object mapped to the System.in object for input and the

10、 standard output object System.out for output. Input:1. Use the Scanner class to create an input object using the standard input object in the System class ( System.in ). The Scanner class is in the package java.utilScanner input = new Scanner( System.in ); /input object is informally called input2.

11、 Use Scanner class methods to get values the user enters using the keyboard. int num1; double num2; String str;num1 = input.nextInt(); /get an integer and assign it to num1 num2 = input.nextDouble(); /get a real number and assign it to num2 str = input.nextLine(); /get a String and assign it to str

12、Output:1. Use the standard output object in the System class ( System.out ).2. Use PrintStream class methods ( printf(), print(), println() ) to display the output to the screen using the System.out object. The System.out object is an object of type PrintStream.System.out.print(What is your name? );

13、String name = input.nextLine();System.out.println(Hello there + name + , nice to meet you!);double purchasePrice = 178.34;System.out.printf(Your total is $%.2fn, purchasePrice);Input/Output Using a Dialog Box include import javax.swing.JOptionPane; When you use a dialog box in Java, it pops up in th

14、e middle of the screen and waits for you to enter input or displays a message and waits for you to click OK. Input/output using a dialog box is done in Java using methods of the JOptionPane class. You do not have to create a JOptionPane object in Java to work with the dialog box methods showInputDia

15、log( ) used to get input and showMessageDialog( ) used to display output . If you are using an input dialog box, the input comes in as a String. You must then use a method to convert the input to the data type you need it to be. The method you would use to get the input is showInputDialog( ). Once you have the input stored in a String variable, you can use a Java wrapper class method to convert the String.Here are the most frequently used wrapper class methods: all classes are in package java.lang Integer.parseInt( )

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 其它办公文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号