控制结构综合案例

1 package com.unit2.test; 2 3 import java.util.Scanner; 4 5 public class Test10 { 6 7 public static void main(String[] args) { 8 // 模拟ATM机 9 Scanner input=new Scanner(System.in); 10 11 String cardNum1="1001"; 12 String pwd1="123"; 13 double balance1=10000; 14 15 String cardNum2="1002"; 16 String pwd2="123"; 17 double balance2=20000; 18 19 String cardNum3="3001"; 20 String pwd3="123"; 21 double balance3=30000; 22 23 System.out.println("欢迎来到银行!"); 24 double balance=0; 25 int count=0; 26 27 while(1==1) { 28 System.out.println("请输入卡号:"); 29 String cardNum=input.next(); 30 31 System.out.println("请输入密码:"); 32 String pwd=input.next(); 33 34 if("1001".equals(cardNum) && "123".equals(pwd)) { 35 balance=balance1; 36 System.out.println("登录成功,您的账户余额为:"+balance); 37 }else if("1002".equals(cardNum) && "123".equals(pwd)) { 38 balance=balance2; 39 System.out.println("登录成功,您的账户余额为:"+balance); 40 }else if("1003".equals(cardNum) && "123".equals(pwd)) { 41 balance=balance2; 42 System.out.println("登录成功,您的账户余额为:"+balance); 43 }else { 44 count++; 45 if(count<=3) { 46 System.out.println("账户或密码错误!您还有"+(4-count)+"机会"); 47 continue; 48 }else { 49 System.out.println("您的账号已被锁住!"); 50 break; 51 } 52 53 } 54 55 while(1==1) { 56 System.out.println("请输入要办理的业务:1存款,2取款,3退出"); 57 String choice=input.next(); 58 59 if("1".equals(choice)) { 60 System.out.println("请输入您的存款金额:"); 61 double num=input.nextDouble(); 62 63 if(num>0) { 64 balance+=num; 65 System.out.println("您存入的金额为:"+num+"。剩余金额为:"+balance); 66 }else { 67 System.out.println("存款金额输入错误!"); 68 } 69 70 }else if("2".equals(choice)) { 71 System.out.println("请输入您的取款金额:"); 72 double num=input.nextDouble(); 73 74 if(num>0 && num<=balance) { 75 balance-=num; 76 System.out.println("您取出的金额为:"+num+"。剩余金额为:"+balance); 77 }else { 78 System.out.println("取款金额输入错误!"); 79 } 80 81 }else if("3".equals(choice)) { 82 System.out.println("办理结束,请收好卡片!"); 83 break; 84 }else { 85 System.out.println("输入有误,请重新输入!"); 86 continue; 87 } 88 } 89 } 90 } 91 92 }
1 package com.unit2.test; 2 3 import java.util.Scanner; 4 5 public class Test11 { 6 7 public static void main(String[] args) { 8 // 购物系统 9 Scanner input=new Scanner(System.in); 10 11 System.out.println("==============51购物管理系统============"); 12 System.out.println("==================1.登录================"); 13 System.out.println("==================2.退出================"); 14 System.out.println("请选择:"); 15 int choice1=input.nextInt(); 16 17 if(choice1==1) { 18 int count=0; 19 while(1==1) { 20 System.out.println("请输入登录名:"); 21 String name=input.next(); 22 System.out.println("请输入密码:"); 23 int pwd=input.nextInt(); 24 25 if("aaa".equals(name) && pwd==123) { 26 while(1==1) { 27 System.out.println("==============主菜单============"); 28 System.out.println("1.客户信息管理"); 29 System.out.println("2.购物结算"); 30 System.out.println("3.真情回馈"); 31 System.out.println("4.注销"); 32 System.out.println("请选择:"); 33 int choice2=input.nextInt(); 34 35 if(choice2==1) { 36 System.out.println("===========客户信息管理========"); 37 System.out.println("1.添加用户"); 38 System.out.println("2.年龄统计"); 39 System.out.println("3.返回主菜单"); 40 System.out.println("请选择:"); 41 int choice21=input.nextInt(); 42 43 if(choice21==1) { 44 String list=""; 45 while(1==1) { 46 System.out.println("请输入用户名:"); 47 String name1=input.next(); 48 System.out.println("请输入年龄:"); 49 int age=input.nextInt(); 50 list+=name1+"----"+age+"\n"; 51 System.out.println("添加成功!继续添加请输入1,输入其他内容停止:"); 52 int choice3=input.nextInt(); 53 if(choice3==1) { 54 continue; 55 }else { 56 System.out.println(list); 57 break; 58 } 59 } 60 }else if(choice21==2) { 61 int upage=0; 62 int downage=0; 63 int i=1; 64 while(i<=3) { 65 System.out.println("请输入用户名:"); 66 String name2=input.next(); 67 System.out.println("请输入年龄:"); 68 int age2=input.nextInt(); 69 if(age2>=30) { 70 upage++; 71 }else { 72 downage++; 73 } 74 i++; 75 } 76 System.out.println("30岁以上比例:"+upage/3.0*100+"%"); 77 System.out.println("30岁以上比例:"+downage/3.0*100+"%"); 78 }else{ 79 continue; 80 } 81 82 }else if(choice2==2) { 83 System.out.println("===========购物结算========"); 84 System.out.println("商品列表:101.苹果 5元 102.梨子 6元 103.香蕉 3元"); 85 double total=0; 86 while(true) { 87 System.out.println("请输入商品编号:"); 88 int pid=input.nextInt(); 89 System.out.println("请输入商品数量:"); 90 int pnum=input.nextInt(); 91 String pName=""; 92 double pPrice=0; 93 if(pid==101) { 94 pName="苹果"; 95 pPrice=5; 96 }else if(pid==102) { 97 pName="梨子"; 98 pPrice=6; 99 }else if(pid==103) { 100 pName="香蕉"; 101 pPrice=3; 102 }else { 103 System.out.println("没有此商品!"); 104 } 105 106 double t=pnum*pPrice; 107 total+=t; 108 System.out.println("当前添加的是:"+pName+"数量:"+pnum+"总价:"+t); 109 110 System.out.println("结算请输入1,输入其他值则继续添加:"); 111 int choice=input.nextInt(); 112 if(choice==1) { 113 break; 114 } 115 } 116 117 System.out.println("合计应付款:"+total); 118 System.out.println("请输入付款金额:"); 119 double money=input.nextDouble(); 120 if(money>=total) { 121 System.out.println("实付款:"+money+",找零:"+(money-total)); 122 System.out.println("欢迎下次再来!"); 123 }else { 124 System.out.println("金额不足!"); 125 } 126 127 }else if(choice2==3) { 128 System.out.println("==============真情回馈============"); 129 double r=Math.random(); 130 int n=(int)(r*100+1); 131 System.out.println("幸运数字是:"+n); 132 if(n<=5) { 133 System.out.println("恭喜获得一等奖:500元购物券!"); 134 }else if(n<=15) { 135 System.out.println("恭喜获得二等奖:10元话费!"); 136 }else { 137 System.out.println("谢谢惠顾!"); 138 } 139 140 }else if(choice2==4) { 141 continue; 142 }else { 143 System.out.println("输入错误"); 144 continue; 145 } 146 } 147 }else { 148 count++; 149 if(count>3) { 150 break; 151 }else { 152 System.out.println("用户名或密码错误,您还有:"+(4-count)+"次机会"); 153 } 154 } 155 } 156 }else if(choice1==2) { 157 System.out.println("退出系统!"); 158 }else { 159 System.out.println("输入错误!"); 160 } 161 162 } 163 164 }
posted on 2020-06-03 21:48 cherry_ning 阅读(130) 评论(0) 收藏 举报
浙公网安备 33010602011771号