1. Console receiving characters
The main principle is through callback. SC = new centers (System. in); Create a scanner, and then use String line = SC. line (); get the user, and finally "+ line. length () + "read digits
Import java. util. extends; public class InputCode {public static void main (String [] args) {Using SC = new using (System. in); // create a Scanner System. out. println ("Enter your ID card number:"); // you are prompted to enter String line = SC. nextLine (); // obtain the user's input text System. out. println ("your ID card was" + line. length () + "digits"); // print }}
650) this. width = 650; "src =" http://img1.51cto.com/attachment/201309/222724633.jpg "title =" jg9ym4(j@yr'upde.pdf (cd000012.jpg "alt =" 222724633.jpg"/>
2. Redirect the output stream to implement program logs
Package com. lixiyu; import java. io. fileNotFoundException; import java. io. printStream; public class RedirectOutputStream {public static void main (String [] args) {try {PrintStream out = System. out; // Save the original output stream PrintStream a = new PrintStream (". /log.txt "); // create the file output stream System. setOut (a); // set to use the new output stream int age = 18; // define an integer System. out. println ("age defined successfully, initial value: 18"); String sex = "female"; System. out. println ("Gender defined successfully, initial value: female"); // integrate the two variables S Tring info = "this is a" + sex + "child. It should be" + age + "years old. "; System. out. println ("the two variables are integrated into the info string variable, and the result is:" + info); System. setOut (out); // restore the original output stream System. out. println ("the program is running successfully. Check the log file. ");} Catch (FileNotFoundException e) {e. printStackTrace ();}}}
In this example, a log.txt file is generated in the folder, showing:
Age defined successfully. Initial Value: 18
Gender defined successfully. Initial Value: Female
Integrate the two variables into the info string variable. The result is: this is a girl, and it should be 18 years old.
Done!
This article is from the "ToBeContinued" blog, please be sure to keep this source http://leexy.blog.51cto.com/4136883/1302364