1. Design Ideas
By finding patterns, there are two cases when the purchase quantity is greater than 5. The first: if the last digit of the number is 8, then this eight is calculated according to two four, the remaining by five of the first division to calculate the lowest; the second: The last value of the number is not 8, then the number of five for the remainder, the operator, according to the size of the residue of the corresponding price, the lowest price per 5.
2. Code implementation
1 ImportJava.util.Scanner;2 Importjava.util.InputMismatchException;3 Public classCHEICP {4 5 Public Static voidMain (string[] args) {6 intCount,yu,shang;//number of Books Yu remainder shang quotient7 DoubleMoney// Money8Scanner sca=NewScanner (system.in);9System.out.println ("Enter number of purchases (integers)");Ten Try{ OneCount=sca.nextint (); A while(count<=0) - { -SYSTEM.OUT.PRINTLN ("Please re-enter the quantity purchased (integer)"); theCount=sca.nextint (); - } -Yu=count%10; - if(count<=5) + { - if(count==1) + { ASYSTEM.OUT.PRINTLN ("Lowest price is" +8+ "Yuan"); at } - if(count==2) - { -SYSTEM.OUT.PRINTLN ("Lowest price is" +2*8*0.95+ "Yuan"); - } - if(count==3) in { -SYSTEM.OUT.PRINTLN ("Lowest price is" +3*8*0.9+ "Yuan"); to } + if(count==4) - { theSYSTEM.OUT.PRINTLN ("Lowest price is" +4*8*0.8+ "Yuan"); * } $ if(count==5)Panax Notoginseng { -SYSTEM.OUT.PRINTLN ("Lowest price is" +5*8*0.75+ "Yuan"); the } + } A Else if(count>5&&yu==8) the { +Shang=count/10; -money=51.2+60*Shang; $SYSTEM.OUT.PRINTLN ("Lowest price is" +money+ "Yuan"); $ } - Else if(count>5&&yu!=8) - { theYu=count%5; -Shang=count/5;Wuyi if(yu==0) the { -money=30*Shang; WuSYSTEM.OUT.PRINTLN ("Lowest price is" +money+ "Yuan"); - } About if(yu==1) $ { -money=8+30*Shang; -SYSTEM.OUT.PRINTLN ("Lowest price is" +money+ "Yuan"); - } A if(yu==2) + { themoney=15.2+30*Shang; -SYSTEM.OUT.PRINTLN ("Lowest price is" +money+ "Yuan"); $ } the if(yu==3) the { themoney=21.6+30*Shang; theSYSTEM.OUT.PRINTLN ("Lowest price is" +money+ "Yuan"); - } in if(yu==4) the { themoney=25.6+30*Shang; AboutSYSTEM.OUT.PRINTLN ("Lowest price is" +money+ "Yuan"); the } the } the } + Catch(inputmismatchexception e) - { theSYSTEM.OUT.PRINTLN ("Input does not meet the requirements, please enter as required");Bayi } the the } -}
The Main Code
3. Implement
4. Personal Summary
This exercise is not difficult, the key is to find the law, and then the code to achieve it is very easy.
Classroom Practice-Calculate the lowest price for a batch of books