Steps to write a program

Source: Internet
Author: User

In the Day04_javase video, feel the teacher is super detailed, easy to understand how to do, it is hand-written notes

A program first:

1 Analyze Your program flow first

2. Write the code money write in a note what each step executes (a broad framework)

3. Installation note Write the code on the following line, clear and unambiguous

(Because of that demand analysis PPT Insert does not come in, so the average person still does not quite understand me to say)

1. Analyze the process of this program first ,

2. Write the lines of code written in text notation,

3. Write the code according to the comment

1  Packageday04_javase;2 /**3 * Simple household income and expenditure statistics System4  *5  */6  Public classFamilyaccount {7 8      Public Static voidMain (string[] args) {9         //the initial value for declaring the balance variable is 10000Ten         intbalance=10000; One         //declare the bookkeeping, the initial value is the table header AString detail= "revenue \ t account amount \ t \ revenue amount \ t description \ n"; -         //declares a Boolean value used to make a loop condition -Boolean loopflag=true; the         //1 in the loop, print the main menu -          while(loopflag) { -System.out.println ("\n******************* Family accounting software ******************\n"); -System.out.println (""); +System.out.println ("1. Payment Details")); -System.out.println ("2. Registered income"); +System.out.println ("3. Grade expenditure"); ASystem.out.println ("4. Exit \ n"); atSystem.out.println (""); -System.out.print ("Please enter your choice (1-4):"); -         //2 Get the user's keyboard input utility.readmenuselection (); -             Charuserinput=utility.readmenuselection (); -         //3 Getting the branch of user input -             Switch(userinput) { in             //user input 1 o'clock, hit the imprint Ledger; -              Case' 1 ': toSystem.out.println ("**************** Current revenue record ****************"); + System.out.println (detail); -System.out.println ("*******************************************"); the                  Break; *             //user Input 2 o'clock, registered revenue $              Case' 2 ':Panax NotoginsengSystem.out.print ("Please enter the amount of your income:"); -                 //user input amount is saved with int money the                 intmoney=Utility.readnumber (); +System.out.print ("Please enter the source of income:"); AString info=utility.readstring (); the                 //An increase in the balance, which would have been equal to the latest balance of income +balance+=Money ; -                 //declares a string, stitching the current operation details into a string $String string= "revenue" + "\ T" +balance+ "\ T" +money+ "\ T" +info+ "\ n"; $                 //Stitch This string into the detail. -detail+=string; -System.out.println ("************************ This entry completed ***************"); the                  Break; -             //user Input 3 o'clock, register expensesWuyi                  Case' 3 ': theSystem.out.print ("Please enter the amount you have spent:"); -                         //user input amount is saved with int money Wu                         intmoney1=Utility.readnumber (); -System.out.print ("Please enter the purpose of the expenditure:"); AboutString info1=utility.readstring (); $                         //An increase in the balance, which would have been equal to the latest balance of income -balance-=Money1; -                         //declares a string, stitching the current operation details into a string -String string1= "expense" + "\ T" +balance+ "\ T" +money1+ "\ T" +info1+ "\ n"; A                         //Stitch This string into the detail. +detail+=string1; theSystem.out.println ("************************ This entry completed ***************"); -                          Break; $                  the             //user Input 4 o'clock, set Boolean variable to False, exit program the              Case' 4 ': theSystem.out.print ("Do you want to quit?") Please select (y/n) "); the                 Charconfirm=utility.readconfirmselection (); -                 if(confirm== ' y '){ inloopflag=false; the                 } the                  Break; About             } the          the         } the     } +  -}
1  Packageday04_javase;2 ImportJava.util.*;3 4  Public classUtility {5     Private StaticScanner Scanner =NewScanner (system.in);6     7      Public Static Charreadmenuselection () {8         CharC;9          for (; ; ) {TenString str = readkeyboard (1); Onec = Str.charat (0); A             if(c! = ' 1 ' && c! = ' 2 ' && c! = ' 3 ' && c! = ' 4 ') { -System.out.print ("Select Error, re-enter:"); -}Else  Break; the         } -         returnC; -     } -  +      Public Static intReadnumber () { -         intN; +          for (; ; ) { AString str = Readkeyboard (4); at             Try { -n =integer.parseint (str); -                  Break; -}Catch(NumberFormatException e) { -System.out.print ("Digital input error, please re-enter:"); -             } in         } -         returnN; to     } +  -      Public StaticString readString () { theString str = Readkeyboard (8); *         returnstr; $     }Panax Notoginseng  -      Public Static Charreadconfirmselection () { the         CharC; +          for (; ; ) { AString str = readkeyboard (1). toUpperCase (); thec = Str.charat (0); +             if(c = = ' Y ' | | c = = ' N ')) { -                  Break; $}Else { $System.out.print ("Select Error, re-enter:"); -             } -         } the         returnC; -     }Wuyi  the     Private StaticString Readkeyboard (intlimit) { -String line = ""; Wu  -          while(Scanner.hasnext ()) { Aboutline =scanner.nextline (); $             if(Line.length () < 1 | | line.length () >limit) { -System.out.print ("Input length (cannot be greater than" + limit + ") error, please re-enter:"); -                 Continue; -             } A              Break; +         } the  -         returnLine ; $     } the}
Utility

Steps to write a program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.