```java
import java.util.Scanner;
public class ShoppingDemo {
public static void main(String[] args) {
Shopping shop = new Shopping();
System.out.println("你购买的衣服总数为:" + shop.buyClothes());
}
}
class Shopping {
int store1 = 0; // 记录第一家商铺购买的衣服数量
int store2 = 0; // 记录第二家商铺购买的衣服数量
int store3 = 0; // 记录第三家商铺购买的衣服数量
int store4 = 0; // 记录第四家商铺购买的衣服数量
int store5 = 0; // 记录第五家商铺购买的衣服数量
int choice = 0; // 选择进入的商铺
int num = 0; // 购买的衣服件数
Scanner scan = new Scanner(System.in);
public int buyClothes() {
do {
System.out.println("请选择输入要进入的商铺(1-5):");
choice = scan.nextInt();
System.out.println("请选择购买的衣服件数(0-3):");
num = scan.nextInt();
} while (choice < 1 || choice > 5 || num < 0 || num > 3);
switch (choice) {
case 1:
store1 += num;
break;
case 2:
store2 += num;
break;
case 3:
store3 += num;
break;
case 4:
store4 += num;
break;
case 5:
store5 += num;
break;
}
return store1 + store2 + store3 + store4 + store5;
}
}
```
代码说明:
主类 `ShoppingDemo`
包含 `main` 方法,用于创建 `Shopping` 对象并调用 `buyClothes` 方法。
类 `Shopping`
包含五个整数变量 `store1` 到 `store5`,分别记录五家商铺购买的衣服数量。
包含整数变量 `choice` 和 `num`,分别用于选择进入的商铺和购买的衣服件数。
包含 `Scanner` 对象 `scan`,用于从控制台读取用户输入。
包含 `buyClothes` 方法,用于处理用户输入并更新各商铺购买的衣服数量。
使用方法:
1. 运行 `ShoppingDemo` 类的 `main` 方法。
2. 根据提示输入要进入的商铺编号(1-5)。
3. 根据提示输入要购买的衣服件数(0-3)。
4. 程序将输出购买的衣服总数。
这个示例代码实现了基本的购买流程,可以根据需要进行扩展和修改,例如添加更多的商铺、商品种类和购买限制等。