检查以下代码是否有逻辑错误,应该如何修改package money; import java.util.Scanner; public class Money { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); int fen = in.nextInt(); Moneys x = new Moneys(fen); x.conversion(); x.show(); in.close(); } } class Moneys{ private int fen; private int jiao; private int yuan; public void conversion(){ this.fen = fen%10; this.jiao = fen/10%10; this.yuan = fen/100; } public void show() { System.out.println(yuan+"yuan"+jiao+"jiao"+fen+"fen"); } public Moneys(int fen) { this.fen = fen; } public Moneys(int yuan,int jiao,int fen) { this.fen = fen; this.jiao = jiao; this.yuan = yuan; } }
时间: 2023-06-14 14:07:49 浏览: 116
代码存在逻辑错误,因为在Moneys类的conversion方法中,fen没有被初始化,因此无输入多少钱数,都会0元0角0分应该修改为以下:
```
class Moneys{
private int;
private int jiao;
private int;
public void conversion(){
this.fen this.fen%10;
this.jiao = this.fen/10%;
this.yuan =.fen/100;
public void show() {
System.out.println(yuan+"yuan"+jiao+"jiao"+fen+"fen");
}
public Moneys(int fen) {
this.fen = fen;
}
public Moneys(int yuan,int jiao,int fen) {
this.fen = fen;
this.jiao = jiao;
this.yuan = yuan;
}
}
```
在conversion方法中,应该使用this.fen来指代类的属性fen,而不是局部变量fen。
相关问题
检查package money; import java.util.Scanner; public class Money { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); int fen = in.nextInt(); Moneys x = new Moneys(fen); x.conversion(); x.show(); in.close(); } } class Moneys{ private int fen; private int jiao; private int yuan; public void conversion(){ this.fen = fen%10; this.jiao = fen/10%10; this.yuan = fen/100; } public void show() { System.out.println(yuan+"yuan"+jiao+"jiao"+fen+"fen"); } public Moneys(int fen) { this.fen = fen; } public Moneys(int yuan,int jiao,int fen) { this.fen = fen; this.jiao = jiao; this.yuan = yuan; } }
这段代码的功能是将输入的分转换成元、角、分的形式,并输出转换后的结果。其中,程序通过Scanner类实现输入,然后创建Moneys类的对象x,将输入的分作为参数传入Moneys类的构造函数中,然后调用Moneys类的conversion()方法将分转换成元、角、分的形式,并将转换后的结果输出。Moneys类中包括了两个构造函数,一个是只接收分作为参数的构造函数,另一个是接收元、角、分三个参数的构造函数。在conversion()方法中,使用了整数除法和取余运算,将分转换成元、角、分的形式,并赋值给类的成员变量yuan、jiao、fen。在show()方法中,通过输出语句将转换后的结果输出。最后,关闭了Scanner对象in。
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();应为类或软件包
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() + "部手机");
}
}
```
阅读全文