package touge; public class MobileShop{ /*************Begin***************/ private int mobileAmount;// 手机数量 public int moneyValue; MobileShop(int m) { moneyValue = m; } void setMobileAmount(int m) { mobileAmount = m; } int getMobileAmount() { return mobileAmount; } class purchaseMoney { int moneyValue; void buyMobile() { int num = moneyValue / 5000; mobileAmount = mobileAmount - num; System.out.println("用价值" + moneyValue + "的内部购物券买了"+num+"部手机"); } } /*************End***************/ }package touge; import java.util.Scanner; public class Test2{ public static void main(String args[]) { Scanner sc = new Scanner(System.in); int m = sc.nextInt(); MobileShop shop = new MobileShop(m); shop.setMobileAmount(30); System.out.println("手机专卖店目前有"+shop.getMobileAmount()+"部手机"); shop.purchaseMoney.buyMobile(); System.out.println("手机专卖店目前还有"+shop.getMobileAmount()+"部手机"); } shop.purchaseMoney.buyMobile();应为类或软件包
时间: 2023-07-01 10:22:51 浏览: 121
the second question of the touge
shop.purchaseMoney.buyMobile();应该放在main函数内部,因为purchaseMoney是MobileShop的内部类,需要通过MobileShop对象来进行调用。修改后的代码如下:
```java
package touge;
import java.util.Scanner;
public class Test2 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
MobileShop shop = new MobileShop(m);
shop.setMobileAmount(30);
System.out.println("手机专卖店目前有" + shop.getMobileAmount() + "部手机");
MobileShop.purchaseMoney purchase = shop.new purchaseMoney();
purchase.buyMobile();
System.out.println("手机专卖店目前还有" + shop.getMobileAmount() + "部手机");
}
}
```
阅读全文