【问题标题】:The first code here does not run through the calculations. Run-time error此处的第一个代码不会运行计算。运行时错误
【发布时间】:2016-10-25 03:56:47
【问题描述】:

需要一些帮助,它应该计算计算并返回所需的值。相反,它返回一个零。

public class TipCalc {

    int satisfactionRating;
    double tip;

    public TipCalc(int satisfactionRating) {

    }

    public double calcTip(double bill) {

        if (satisfactionRating==1) {
            tip = bill*0.20;
        }
        if (satisfactionRating==2) {
            tip = bill*0.15;
        }
        if (satisfactionRating==3) {
            tip = bill*0.10;
        }
        return tip;
    }
}

///////////////////////////////////////////////////////////////////////////////////////////// //////////////////////p>

import java.util.Scanner;

public class tiptester {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Please put in the total cost of your meal "); 
        double mealPrice = input.nextFloat();
        System.out.println("Please input your satisfaction rating ");  
        int satisfaction = input.nextInt();
        TipCalc meal = new TipCalc(satisfaction);
        double tipz = meal.calcTip(mealPrice);
        double totalMeal = tipz+mealPrice;
        System.out.println("The tip is " + tipz);
        System.out.println("The cost of the meal is " + mealPrice);
        System.out.println("The total cost is " + totalMeal);
    }
}

【问题讨论】:

  • 你有什么意见?
  • 注意:不要对int satisfactionRatingdouble tip 使用成员变量。分别声明为方法参数和局部变量。
  • 您没有将值分配给satisfactionRating,而是将其传递给TipCalc 类构造函数。试试这个public TipCalc(int satisfactionRating) { this.satisfactionRating = satisfactionRating; }

标签: java time return


【解决方案1】:

您应该在 TipCalc 的构造函数中分配满意评级,如下所示,

public TipCalc(int satisfactionRating)
{
    this.satisfactionRating = satisfactionRating;
}

目前由于satisfactionRating的值没有赋值,默认为0,反过来calcTip()方法中的if条件都不满足,返回0。

【讨论】:

    【解决方案2】:

    成员变量满意评级未初始化。您需要为从 Tester 类传入构造函数的值分配满意评级。

    public TipCalc(int satisfactionRating)
    {
        this.satisfactionRating = satisfactionRating
    }
    

    这应该可行!

    【讨论】:

      【解决方案3】:

      正如@Jerin @Khuzi 告诉你没有在构造函数中初始化满意评级并查看下面的代码,一个典型的类应该是这样的:

      public class TipCalc {
      private int satisfactionRating; //should be private, should be accessed using getter and setter methods
      private double tip;             //should be private, should be accessed using getter and setter methods
      
      public TipCalc(int satisfactionRating) {
          this.satisfactionRating = satisfactionRating; //This was your actual problem.
      }
      
      public double calcTip(double bill) {
          if (satisfactionRating == 1) {
              tip = bill * 0.20;
          }
          if (satisfactionRating == 2) {
              tip = bill * 0.15;
          }
          if (satisfactionRating == 3) {
              tip = bill * 0.10;
          }
          return tip;
      }
      
      public int getSatisfactionRating() {
          return satisfactionRating;
      }
      
      public void setSatisfactionRating(int satisfactionRating) {
          this.satisfactionRating = satisfactionRating;
      }
      
      public double getTip() {
          return tip;
      }
      
      public void setTip(double tip) {
          this.tip = tip;
      }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-03
        • 1970-01-01
        • 2022-06-30
        • 1970-01-01
        • 2020-05-18
        • 1970-01-01
        • 1970-01-01
        • 2022-01-11
        相关资源
        最近更新 更多