Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java计算求和时的句柄构造函数_Java_Constructor_Java.util.scanner - Fatal编程技术网

使用java计算求和时的句柄构造函数

使用java计算求和时的句柄构造函数,java,constructor,java.util.scanner,Java,Constructor,Java.util.scanner,我想计算一下家庭保险的保险金额。最初的要求是:每平方米2种不同价格的保险产品 1.紧凑型:每平方米650欧元 2.最佳:每平方米700欧元 该程序的2个输入为:“紧凑”或“最佳” 以及以平方米为单位的生活空间。 输出应为保险金额; 必须注意,此程序可能会被其他计算复杂的产品扩展 我是以这种方式进行的,但由于我是java新手,我陷入了如何处理构造函数的困境。在我的代码中,我想修正紧凑和优化的问题,但我想通过用户输入(比如scanner类)获得生活空间的价值。在这种情况下我该怎么做 My Insur

我想计算一下家庭保险的保险金额。最初的要求是:每平方米2种不同价格的保险产品 1.紧凑型:每平方米650欧元 2.最佳:每平方米700欧元 该程序的2个输入为:“紧凑”或“最佳” 以及以平方米为单位的生活空间。 输出应为保险金额; 必须注意,此程序可能会被其他计算复杂的产品扩展

我是以这种方式进行的,但由于我是java新手,我陷入了如何处理构造函数的困境。在我的代码中,我想修正紧凑和优化的问题,但我想通过用户输入(比如scanner类)获得生活空间的价值。在这种情况下我该怎么做

My Insurance class 
public class Insurance {
  private Double Kompakt;
  private Double Optimal;

  public Insurance(Double kompakt, Double optimal) {

    this.Kompakt = kompakt;
    this.Optimal = optimal;
}

public Double getKompakt() {
    return Kompakt;
}

public void setKompakt(Double kompakt) {
    this.Kompakt = kompakt;
}

public Double getOptimal() {
    return Optimal;
}

public void setOptimal(Double optimal) {
    this.Optimal = optimal;
 }
}
我的算术课是

public class Sum extends Insurance{

 private int livingSpace;

  public Summe(Double kompakt, Double optimal, int Wohnfläche) {
    super(1.0, 1.0);
    this.livingSpace=livingSpace;
    this.setCompakt(650.00); // I want to fix value of Compact
    this.setOptimal(700.00); // I want to fix Value of Optimal
    }


public double CompaktSum(){

        double sum= getCompakt()*livingSpace;
        return sum;
    }
    public double OptimalSum(){
        double sum =getOptimal()*livingSpace;
        return sum;
    }
}
现在在我的主类中,我想手动设置livingSpace的值,但如果没有设置方法,我无法调用构造函数

 public static void main(String[] args) {
    Scanner sc1 =new Scanner(System.in);
    Scanner sc2 =new Scanner(System.in);
    System.out.println("1\tCompact: 650 € per m²\n2\tOptimal: 700 € per 
    m²");
    System.out.println("Please choose your 1 for Compact and 2 for product");
    int swichValue=sc1.nextInt();
    Summe hs=new Summe(1.0,1.0,5); // I am having problem here, I just want 
    //to call empty contructor so that I can set livingSpace manually

    switch(swichValue){
    case 1:{
    double res=hs.KompaktSumme();
    System.out.println("Versicherungssumme ist : "+res);
    break;
    }
    case 2:{
        double res=hs.OptimalSumme();
        System.out.println("Versicherungssumme ist : "+res);
        break;
    }
    case 3:{
        System.out.println("Error");

    }
    }
}

这就是如何重载构造函数的方法。但实际上您的代码不会编译,因为类的名称是
Sum
,构造函数的名称是
Summe
。构造函数应该与类具有相同的名称

public Sum() {
    super(650.00, 700.00);
}

这就是如何重载构造函数的方法。但实际上您的代码不会编译,因为类的名称是
Sum
,构造函数的名称是
Summe
。构造函数应该与类具有相同的名称

public Sum() {
    super(650.00, 700.00);
}

坦白地说,我不确定你到底想要实现什么,但如果我的理解是正确的。 要确定Kompakt和Optimal的值,只需执行以下操作:

public class Insurance {
  private Double Kompakt = 700.0; // fixed value
  private Double Optimal = 600.0; // fixed value

  public Insurance() {} // empty constructor for the superclass

 /* you can change the values and get them by using the same getters and setters you already created*/
现在在子类中

public class Summe extends Insurance{

 private int livingSpace;

  public Summe(int Wohnfläche) { // you can use this in future if you wnat
   // no need to call the superclass constructor 
      livingSpace = Wohnfläche;
    }

    // another constructor that overloads the first (i.e empty one)
      public Summe() {
       // no need to call the superclass constrictor 
      }

 // create a setter and a getter for the the livingSpace
 public void setLivingSpace (int livingSpace){
     this.livingSpace = livingSpace;
 }
现在给你

public static void main(String[] args) {
    Scanner sc1 =new Scanner(System.in);
    Scanner sc2 =new Scanner(System.in);
    System.out.println("1\tCompact: 650 € per m²\n2\tOptimal: 700 € per 
    m²");
    System.out.println("Please choose your 1 for Compact and 2 for product");
    int swichValue=sc1.nextInt();
    Summe hs=new Summe(); 
    // set the values here manually as you described
    hs.setLivingSpace (0.5); // or whatever value, you may need to take it from user..etc 

    switch(swichValue){
    case 1:
          double res=hs.KompaktSumme();
          System.out.println("Versicherungssumme ist : "+res);
       break;

    case 2:
          double res=hs.OptimalSumme();
          System.out.println("Versicherungssumme ist : "+res);
        break;

    case 3:
         System.out.println("Error");
    }
}

坦白地说,我不确定你到底想要实现什么,但如果我的理解是正确的。 要确定Kompakt和Optimal的值,只需执行以下操作:

public class Insurance {
  private Double Kompakt = 700.0; // fixed value
  private Double Optimal = 600.0; // fixed value

  public Insurance() {} // empty constructor for the superclass

 /* you can change the values and get them by using the same getters and setters you already created*/
现在在子类中

public class Summe extends Insurance{

 private int livingSpace;

  public Summe(int Wohnfläche) { // you can use this in future if you wnat
   // no need to call the superclass constructor 
      livingSpace = Wohnfläche;
    }

    // another constructor that overloads the first (i.e empty one)
      public Summe() {
       // no need to call the superclass constrictor 
      }

 // create a setter and a getter for the the livingSpace
 public void setLivingSpace (int livingSpace){
     this.livingSpace = livingSpace;
 }
现在给你

public static void main(String[] args) {
    Scanner sc1 =new Scanner(System.in);
    Scanner sc2 =new Scanner(System.in);
    System.out.println("1\tCompact: 650 € per m²\n2\tOptimal: 700 € per 
    m²");
    System.out.println("Please choose your 1 for Compact and 2 for product");
    int swichValue=sc1.nextInt();
    Summe hs=new Summe(); 
    // set the values here manually as you described
    hs.setLivingSpace (0.5); // or whatever value, you may need to take it from user..etc 

    switch(swichValue){
    case 1:
          double res=hs.KompaktSumme();
          System.out.println("Versicherungssumme ist : "+res);
       break;

    case 2:
          double res=hs.OptimalSumme();
          System.out.println("Versicherungssumme ist : "+res);
        break;

    case 3:
         System.out.println("Error");
    }
}

欢迎来到堆栈溢出!我想这段代码的95%与您的问题无关。请创建一个演示您的问题的构造函数。创建另一个构造函数->在java中称为重载->您可以在java中使用两个具有不同参数的构造函数/arguments@John你能解释一下怎么做吗欢迎来到Stack Overflow!我想这段代码的95%与您的问题无关。请创建一个演示您的问题的构造函数。创建另一个构造函数->在java中称为重载->您可以在java中使用两个具有不同参数的构造函数/arguments@John你能解释一下怎么做吗谢谢你的回答。我会尝试你的代码,并让你尽快知道。谢谢你的回答。我将尝试你的代码,并让你尽快知道。