Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
7-1 /HVVRQ«,QSXWIURPWKH.H\ERDUG We will consider how to input from the keyboard the three data types…. LQW, GRXEOH, and 6WULQJ. ,QSXWWLQJDQLQWHJHU Use the QH[W,QW method to input an LQWHJHU from the keyboard: import java.io.*; //VHH³,PSRUWVQHFHVVDU\´RQQH[WSDJH import java.util.*; public class Tester { public static void main( String args[] ) { Scanner kbReader = new Scanner(System.in); //VHH³0\VWHULRXV REMHFWV´RQQH[WSDJH System.out.print(“Enter your integer here. ”); //enter int i = kbReader.nextInt( ); System.out.println(3 * i); //prints } } ,QSXWWLQJDGRXEOH Use the QH[W'RXEOH method to input a GRXEOH from the keyboard: import java.io.*; import java.util.*; public class Tester { public static void main( String args[] ) { Scanner kbReader = new Scanner(System.in); System.out.print(“Enter your decimal number here. ”); // double d = kbReader.nextDouble( ); System.out.println( 3 * d ); //prints } } ,QSXWWLQJD6WULQJ Use the QH[W method to input a 6WULQJ from the keyboard: import java.io.*; import java.util.*; public class Tester{ public static void main( String args[] ) { Scanner kbReader = new Scanner(System.in); System.out.print(“Enter your String here. ”); //Enter 2QH7ZR String s = kbReader.next( ); //inputs up to first white space System.out.println( “This is the first part of the String,… ” + s); s = kbReader.next( ); System.out.println( “This is the next part of the String,… ” + s); } } 7-2 Output would be as shown below: Enter your String here. One Two This is first part of the String,... One This is next part of the String,... Two 0XOWLSOHLQSXWV In a similar way QH[W,QW and QH[W'RXEOH can be used multiple times to parse data input from the keyboard. For example, if is input from the keyboard, then QH[W,QW can be applied four times to access these four integers separated by white space. ,QSXWWLQJDQHQWLUHOLQHRIWH[W Inputting a 6WULQJ (it could contain spaces) from the keyboard using QH[W/LQH: import java.io.*; import java.util.*; public class Tester { public static void main( String args[] ) { Scanner kbReader = new Scanner(System.in); System.out.print(“Enter your String here. ”); //Enter 2QH7ZR String s= kbReader.nextLine( ); System.out.println( “This is my string,… ” + s); } } Output would be as shown below: Enter your String here. One Two This is my string,... One Two ,PSRUWVQHFHVVDU\ We must LPSRUW two classes,….MDYDLRDQGMDYDXWLO that provide methods for inputting integers, GRXEOHs, and 6WULQJs. See Appendix I for more on the meaning of “importing”. 0\VWHULRXVREMHFWV In the above three examples we used the following code: Scanner kbReader = new Scanner(System.in); It simply creates the keyboard reader REMHFW (we arbitrarily named it NE5HDGHU) that provides access to the QH[W,QW, QH[W'RXEOH, QH[W, and QH[W/LQH methods. For now just accept the necessity of all this…it will all be explained later. The 6FDQQHU class used here to create our keyboard reader object only applies to1.5.0_xx or higher versions of Java. For older versions, see Appendix M for an alternate way to obtain keyboard input. An anomaly 8VLQJDsingleScannerREMHFWWKHPHWKRGV nextInt( )nextDouble( )next( )DQG nextLine( )PD\EHXVHGLQany sequenceZLWKWKHIROORZLQJH[FHSWLRQ ,WLVQRWSHUPLVVLEOHWRIROORZnextInt( )RUnextDouble( )ZLWKnextLine( ),ILWLV QHFHVVDU\WRGRWKLVWKHQDQHZScannerREMHFWPXVWEHFRQVWUXFWHGIRUXVHZLWK nextLine( )DQGDQ\VXEVHTXHQWLQSXWV Project… Going in Circles 7KHDUHDRIDFLUFOHLVJLYHQE\ DUHD SU 1RZVXSSRVHZHNQRZWKHDUHDDQGZLVKWRILQGU6ROYLQJIRUUIURPWKLVHTXDWLRQ \LHOGV :ULWHDSURJUDPSURMHFWDQGFODVVERWKQDPHGRadiusOfCircleWKDWXVHVsqrt( )DQGPI IURPWKHMathFODVVWRVROYHIRUWKHUDGLXVRIDFLUFOH8VHNH\ERDUGLQSXWWRVSHFLI\WKH DUHDSURYLGHIRUWKHSRVVLELOLW\RIDUHDEHLQJDGHFLPDOIUDFWLRQ :ULWHRXW\RXUVROXWLRQE\KDQGDQGWKHQHQWHULWLQWRWKHFRPSXWHUDQGUXQ%HIRUH LQSXWWLQJWKHDUHDSXWDSURPSWRQWKHVFUHHQOLNHWKLV What is the area?B«WKHXQGHUVFRUHLQGLFDWHVWKHFXUVRUZDLWLQJIRULQSXW 3UHVHQW\RXUDQVZHUOLNHWKLV Radius of your circle is 139.4. Project… What’s My Name? )URPWKHNH\ERDUGHQWHU\RXUILUVWDQGWKHQ\RXUODVWQDPHHDFKZLWKLWVRZQSURPSW 6WRUHHDFKLQDVHSDUDWHStringDQGWKHQFRQFDWHQDWHWKHPWRJHWKHUWRVKRZ\RXUIXOO QDPH&DOOERWKWKHSURMHFWDQGWKHFODVVFullName:KHQ\RXUSURJUDPLVILQLVKHG UXQQLQJWKHRXWSXWVKRXOGDSSHDUVLPLODUWRWKDWEHORZ What is your first name? Cosmo What is your last name? Kramer Your full name is Cosmo Kramer.