编写一个超市购物程序,在一家超市有牙刷、毛巾、水杯、苹果和香蕉五种商品:
拥护输入序列号进行购物,并在输入购物数量后算出所需花费及退出购物时的总花费。
一次购买结束后,需用户输入"Y"后者"N",前者代表继续购买,后者代表结束购物!
以下为代码:
package Shopping;
import java.util.Scanner;
public class Project {
//main方法
public static void main(String[] args){
//定义数组
String [] Commodity = {"1.牙刷","2.毛巾","3.水杯","4.苹果","5.香蕉"};
double [] Prize = {8.8,10.0,18.8,12.5,15.5};
double sum1=0,sum2=0,sum3=0,sum4=0,sum5=0;
w1:while(true)
{
//打印出商品列表
System.out.println("***************************************");
for (int i = 0; i < Commodity.length; i++)
System.out.print(Commodity[i] + " ");
System.out.println();
for (int j = 0; j < Prize.length; j++)
System.out.print(Prize[j] + " ");
System.out.println();
System.out.println("***************************************");
//选择商品
System.out.println("请按下数字选择商品:");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("请选择该商品数量:");
sum1 = toothbrush(Prize,sum1);
int a = choice();
if(a==1)
break w1;
else if(a==0)
break;
else { System.out.println("输入错误,请重新开始!");break; }
case 2:
System.out.println("请选择商品数量:");
sum2 = towel(Prize,sum2);
int b = choice();
if(b==1)
break w1;
else if(b==0)
break;
else { System.out.println("输入错误,请重新开始!");break; }
case 3:
System.out.println("请选择商品数量:");
sum3 = cwg(Prize,sum3);
int c = choice();
if(c==1)
break w1;
else if(c==0)
break;
else { System.out.println("输入错误,请重新开始!");break; }
case 4:
System.out.println("请选择商品数量:");
sum4 = apple(Prize,sum4);
int d = choice();
if(d==1)
break w1;
else if(d==0)
break;
else { System.out.println("输入错误,请重新开始!");break; }
case 5:
System.out.println("请选择商品数量:");
sum5 = banana(Prize,sum5);
int e = choice();
if(e==1)
break w1;
else if(e==0)
break;
else { System.out.println("输入错误,请重新开始!");break; }
default:
System.out.println("输入错误!");
System.out.println("请重新输!");
}
}
double all;
all = sum1+sum2+sum3+sum4+sum5;
System.out.println("您本次购物共花费"+all+"元!");
System.out.println("购物结束,祝您生活愉快!");
}
//方法一
public static double toothbrush(double [] Prize,double sum1){
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
double cost1 = number * Prize[0];
sum1+=cost1;
System.out.println("本件商品共已花费" + sum1 + "元!");
return sum1;
}
//方法二
public static double towel(double [] Prize,double sum2){
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
double cost2 = number * Prize[1];
sum2+=cost2;
System.out.println("本件商品共已花费" + sum2 + "元!");
return sum2;
}
//方法三
public static double cwg(double [] Prize,double sum3){
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
double cost3 = number * Prize[2];
sum3+=cost3;
System.out.println("本件商品共已花费" + sum3 + "元!");
return sum3;
}
//方法四
public static double apple(double [] Prize,double sum4){
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
double cost4 = number * Prize[3];
sum4+=cost4;
System.out.println("本件商品共已花费" + sum4 + "元!");
return sum4;
}
//方法五
public static double banana(double [] Prize,double sum5){
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
double cost5 = number * Prize[4];
sum5+=cost5;
System.out.println("本件商品共已花费" + sum5 + "元!");
return sum5;
}
//选择是否继续购物
public static int choice() {
System.out.println("--------------------");
Scanner sc = new Scanner(System.in);
System.out.println("是否继续购物:Y/N ?");
String input = sc.next();
if (input.equals("Y"))
return 0;
else if(input.equals("N"))
return 1;
return 2;
}
}
好了,代码到这里就结束了,我感觉有些冗余,肯定没有大佬敲的好,不过我挺满意的!
欢迎大佬指正!