[Java Basics] Loop structure 2
Wrote a few loop structure exercises ~ record It ~ ~
1:99 multiplication Table
1 /**2 file path: G:\JavaByHands\ Loop statement3 file name: Gameforfor.java4 Write Time: 2016/6/75 Author: Zheng-hui6 Write a description: For loop Sample code example7 write a 99 multiplication table8 */9 Public classGameforfor {Ten Public Static voidMain (string[] args) { One //Target: Output 99 multiplication table A for(inti = 1;i <= 9;i++){ - - for(intj = 1;j <= i;j++) { theSystem.out.print (i + "*" + j + "=" + (i*j) + "\ T"); - } - System.out.println (); - + } - } +}
2: Tease you to play a simple lottery
1 /**2 file path: G:\JavaByHands\ Loop statement3 file name: Testforfor.java4 Write Time: 2016/6/115 Author: Zheng-hui6 Write a description: Entertainment ~ ~ Simple Lottery7 tease you to play the lottery system ~ ~ Use of circular statements, conditional statements to achieve a simple lottery function ~ ~8 Super simple version ...9 */Ten One //Guide Package A //Scanner - ImportJava.util.Scanner; - //Random the ImportJava.util.Random; - - - + Public classTestforfor { - + Public Static voidMain (string[] args) { A at //Scanner Object -Scanner sc =NewScanner (system.in); - //Random Object -Random ran =NewRandom (); - -System.out.println ("Welcome to tease you play system ~ ~"); inSYSTEM.OUT.PRINTLN ("Please select: 1: Sweepstakes; 2: Exit"); - //Incoming Options to inti =sc.nextint (); + //to judge an incoming item - if(i = = 1) { theSystem.out.println ("Welcome to the lottery system ~ ~"); *System.out.println ("Would you like to start the lottery? [y/n] "); $ /*Panax Notoginseng Here I used a byte b = sc.nextbyte result error - inputmismatchexception the if the next tag does not match an Integer regular expression, or is out of range + */ AString s =Sc.next (); the //Judging + if(S.equals ("Y")) { -SYSTEM.OUT.PRINTLN ("---start Lottery---"); $ $ intRannum = Ran.nextint (11); - //to judge the obtained value - Switch(rannum) { the Case(1): -System.out.println ("Congratulations on your first prize ~ ~ ~");Wuyi Break; the Case(2): - Case(3): WuSystem.out.println ("Congratulations on your second prize ~ ~ ~"); - Break; About Case(4): $ Case(5): - Case(6): -System.out.println ("Congratulations to the third prize ~ ~"); - Break; A Case(7): + Case(8): the Case(9): - Case(10): $System.out.println ("Very sorry ... You didn't win ... "); the Break; the } the}Else if(S.equals ("N")) { theSystem.out.println ("Give Up the lottery"); -System.out.println ("End of program"); in}Else { theSYSTEM.OUT.PRINTLN ("Command error ..."); theSYSTEM.OUT.PRINTLN ("Program End ..."); About } the the}Else if(i = = 2) { theSYSTEM.OUT.PRINTLN ("You have exited the system ~ ~"); +SYSTEM.OUT.PRINTLN ("Look forward to your next use ~ ~"); -}Else { theSYSTEM.OUT.PRINTLN ("Command error ...");BayiSYSTEM.OUT.PRINTLN ("Program End ..."); the } the } -}
I want to write with eclipse ... But in order to improve ... Endure...
[Java Basics] Loop structure 2