资源预览内容
第1页 / 共4页
第2页 / 共4页
第3页 / 共4页
第4页 / 共4页
亲,该文档总共4页全部预览完了,如果喜欢就下载吧!
资源描述
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 screen 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 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( ) 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 the 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 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. However, 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 used 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 ( true 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 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. 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 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? );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 the 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 showInputDialog( ) 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( )
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号