Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Java imitates Wechat how to realize the simple function of giving change.

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how Java imitates Wechat to achieve change communication simple function, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article, I hope you can get something.

1. Requirement description

Use Java to develop change exchange project, imitate Wechat to achieve simple functions, you can complete income entry, consumption, view details, exit the system and other functions, first write according to the general method, and later improve to OOP

Expected interface: (it may actually be different)

two。 Demand analysis

In the face of such a demand, first reduce complexity to simplicity.

Write a menu

Complete the change in detail.

Complete income recording

Consumption

Quit

When the user quits by typing 4, the prompt is "are you sure you want to quit? YBO". You must enter the correct yPao, otherwise enter the instruction in a loop until you enter y or n

When the income is recorded and consumed, judge whether the amount is reasonable, and give the corresponding hint.

3. Realize the main function of change 3.1 write a menu

First complete the display menu, and you can select the menu, and give the corresponding prompt

Public static void main (String [] args) {/ / define related variables Scanner scanner = new Scanner (System.in); String key = ""; boolean loop = true; do {System.out.println ("= Small Change Menu="); System.out.println ("\ t\ t\ T1 show change details"); System.out.println ("\ t\ t\ T2 income entry") / / use switch to control switch (key) {case "1": System.out.println ("1 show change details"); break; case "2": System.out.println ("2 income entry"); break Case "3": System.out.println ("3 consumption"); break; case "4": System.out.println ("4 exit"); System.out.println ("you have exit the SmallChange"); loop = false Break; default: System.out.println ("err please choose again");}} while (loop);} 3.2 change details

Train of thought

(1) income and consumption can be saved to the array.

(2) objects can be used

(3) String splicing can be used if it is simple.

The third way is taken directly here.

Change switch's case1.

String details = "- change details -"; case "1": System.out.println (details); break;3.3 income is recorded

Complete income recording

Define a new variable

Double money = 0; double balance = 0; Date date = null; / / date is the java.util.Date type, indicating the date / / if you don't like the default format of displaying date, change it with sdf SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm")

Modify case2 in switch

System.out.print ("Income recorded amount:"); money = scanner.nextDouble (); / / the range of money should be limited / / give the hits of the illegal money value directly break balance + = money; / / splice revenue accounting information to details date = new Date () / / Get the current time details + = "\ nRevenue entry\ t +" + money + "\ t" + sdf.format (date) + "\ t" + balance; break

Demonstration of the effect:

Guarantee entry > 0

3.4 consumption

Define a new variable

String note = ""

Modify case3 in switch

Case "3": System.out.print ("Consumption amount:"); money = scanner.nextDouble (); / / the range of money should be limited System.out.print ("Consumption Description:"); note = scanner.next (); balance-= money / / Splicing consumption information to details date = new Date (); / / Get the current time details + = "\ n" + note + "\ t -" + money + "\ t" + sdf.format (date) + "\ t" + balance; break

Demonstration of the effect:

3.5 user exit improvement

Give confirmation of whether or not to exit

When the user quits by typing 4, the prompt is "are you sure you want to quit? yPop". You must enter the correct yPop.

Otherwise, enter the instruction in a loop until you enter y or n

(1) define a variable choice to receive user input

(2) use while + break to process received input y or n

(3) after quitting while, determine whether choice is y or n, and then decide whether to quit or not.

(4) it is recommended that a piece of code should be completed and not mixed together.

Case "4": String choice = "; while (true) {/ / The user is required to enter Y / N, otherwise it will cycle all the time System.out.println (" are you sure you want to quit? ); choice = scanner.next (); if ("y" .equals (choice) | | "n" .equals (choice)) {break } / / scheme 2 if / if ("y" .equals (choice)) {/ / loop = false;// break / /} else if ("n" .equals (choice)) {/ / break;//}} if (choice.equals ("y")) {loop = false } break

Demonstration of the effect:

3.6 improve the judgment of amount

Income time

If (money

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report