1.查询商品价格
训练要点: while循环结构;程序调试
需求说明: 循环输入商品编号,显示对应的商品价格;输入“n”结束循环
实现思路: 1、声明变量 2、循环体 3、循环条件
难点指导: 循环体内使用switch
package com.etc.three.day;
import java.util.Scanner;
public class Query {
public static void main(String[] args) {
System.out.println("MyShopping管理系统 > 购物结算");
System.out.println("");
System.out.println("****************************************");
System.out.println("请选择购买的商品编号:");
System.out.println("1.T恤\t2.网球鞋\t 3.网球拍");
System.out.println("****************************************");
double a = 245;
double b = 570;
double c = 320;
boolean flag = true;
int number = 0;
while (flag) { //while循环语句
System.out.print("请输入商品编号:");
Scanner input = new Scanner(System.in);
if (input.hasNextInt()) { //if语句,判断输入值是否为数字
number = input.nextInt();
flag = false;
switch (number) {
case 1:
System.out.println("T恤" + "\t¥" + a);
break;
case 2:
System.out.println("网球鞋" + "\t¥" + b);
break;
case 3:
System.out.println("网球拍" + "\t¥" + c);
break;
default:
System.out.println("请输入正确数字:");
break;
}
System.out.println("是否继续(y/n)");
String number2 = input.next();
if (number2.equals("n")) { //if语句,判断输入值是否为n
System.out.println("程序结束!");
} else {
flag = true;
}
} else {
System.out.println("请输入商品编号:");
}
}
}
}
2.升级购物结算
需求说明: 循环输入商品编号和购买数量 ,当输入n时结账, 结账时计算应付金额并找零。
package com.etc.three.day;
import java.util.Scanner;
public class Checkout {
public static void main(String[] args) {
System.out.println("MyShopping管理系统 > 购物结算");
System.out.println("");
System.out.println("****************************************");
System.out.println("请选择购买的商品编号:");
System.out.println("1.T恤\t2.网球鞋\t 3.网球拍");
System.out.println("****************************************");
System.out.println();
double a = 245;
double b = 570;
double c = 320;
double money1 = 0;
double money2 = 0;
double money3 = 0;
boolean flag = true;
int number = 0;
while (flag) {
System.out.print("请输入商品编号:");
Scanner input = new Scanner(System.in);
if (input.hasNextInt()) {
number = input.nextInt();
flag = false;
System.out.print("请输入购买数量:");
boolean flag2 = true;
int number3 = 0;
int number4 = 0;
while (flag2) {
input = new Scanner(System.in);
if (input.hasNextInt()) {
number3 = input.nextInt();
flag2 = false;
if (number == 1) {
money1 = money1 + a * number3;
} else if (number == 2) {
money2 = money2 + b * number3;
} else if (number == 3) {
money3 = money3 + c * number3;
}
switch (number) {
case 1:
System.out.println("T恤" + "\t¥" + a + "\t数量:" + number3 + "合计:¥" + a * number3);
break;
case 2:
System.out.println("网球鞋" + "\t¥" + b + "\t数量:" + number3 + "合计:¥" + b * number3);
break;
case 3:
System.out.println("网球拍" + "\t¥" + c + "\t数量:" + number3 + "合计:¥" + c * number3);
break;
default:
System.out.print("请输入正确数字:");
break;
}
double money4 = money1 + money2 + money3;
System.out.print("是否继续(y/n)");
String number2 = input.next();
if (number2.equals("n")) {
System.out.println("\n折扣:0.8");
System.out.println("应付金额:" + money4 * 0.8);
System.out.print("实收金额:");
boolean flag3 = true;
double money5 = 0;
while (flag3) {
input = new Scanner(System.in);
if (input.hasNextInt()) {
money5 = input.nextInt();
flag = false;
double money6 = money5 - money4 * 0.8;
System.out.println("找钱:" + money6);
} else {
System.out.print("实收金额:");
}
}
} else {
flag = true;
}
} else {
System.out.print("请输入购买数量:");
}
}
}
}
}
}
3.升级菜单切换
package com.etc.three.day;
import java.util.Scanner;
public class Menu {
public static void main(String[] args) {
System.out.println("欢迎使用MYShopping管理系统");
System.out.println("\n");
System.out.println("********************************");
System.out.println("\t1.客户信息管理\t");
System.out.println("\t2.购物结算\t");
System.out.println("\t3.真情回馈\t");
System.out.println("\t4.注销\t");
System.out.println("********************************");
boolean flag = true;
int number = 0;
System.out.print("请输入数字:");
while (flag) {
Scanner input = new Scanner(System.in);
if (input.hasNextInt()) {
number = input.nextInt();
flag = false;
switch (number) {
case 1:
System.out.println("执行客户信息管理\n\n程序结束");
break;
case 2:
System.out.println("执行购物结算\n\n程序结束");
break;
case 3:
System.out.println("执行真情回馈\n\n程序结束");
break;
default:
System.out.print("输入错误,请重新输入数字:");
break;
}
flag = true;
} else{
System.out.print("输入错误,请重新输入数字:");
}
}
}
}
希望有大佬能够教教我第二题,我只能想成这样了。
我想抱大腿,抱大腿啊!!!