自由与自然 2022-03-09 20:37 采纳率: 0%
浏览 25

编译通过,运行出错。输入有问题


 import java.util.Scanner;
 class Goods{
  private String name;
   private double price;
   public void setname(){
     String name;
     Scanner in = new Scanner(System.in);
     name = in.nextLine();
    this.name = name;
    in.close();
   }
   public void setprice( ){
       double price;
       Scanner in = new Scanner(System.in);
       price = in.nextDouble();
       this.price = price;
       in.close();
   }
   public String getname(){
       return this.name;
   }
   public double getprice(){
       return this.price;
   }
 
}
class Store{
private double harvest;
    public void sell(Goods a1){
    System.out.println("请输入要卖的商品:"+a1.getname());
    System.out.println("正在交易中请稍后");
    this.harvest = a1.getprice();
    System.out.println("微信收款"+a1.getprice());
    }
     public void setPriceAndName(Goods a1){
        System.out.println("please set name and price of the goods");
        a1.setname();
        a1.setprice();
          }
    public double getharvest(){
        return this.harvest;
    }
}
public class Tester{
    public static void main(String[] args){
      System.out.println("please start");
      Store yang = new Store();
      Goods a1 = new Goods();
      yang.setPriceAndName(a1);
      yang.sell(a1);
      System.out.println("请输出获得的收入:"+yang.getharvest());
    }

}
  • 写回答

2条回答 默认 最新

  • CSDN专家-link 2022-03-09 20:54
    关注

    Scanner in = new Scanner(System.in);
    输入代码不要放在函数里,放到main函数中,将输入值作为参数传递给set函数

    评论

报告相同问题?

问题事件

  • 创建了问题 3月9日

悬赏问题

  • ¥20 python忆阻器数字识别
  • ¥15 无法输出helloworld
  • ¥15 高通uboot 打印ubi init err 22
  • ¥20 PDF元数据中的XMP媒体管理属性
  • ¥15 R语言中lasso回归报错
  • ¥15 网站突然不能访问了,上午还好好的
  • ¥15 有没有dl可以帮弄”我去图书馆”秒选道具和积分
  • ¥15 semrush,SEO,内嵌网站,api
  • ¥15 Stata:为什么reghdfe后的因变量没有被发现识别啊
  • ¥15 振荡电路,ADS仿真