我的主要局部变量未定义

时间:2016-04-24 15:51:52

标签: java

我参加了我的Java类简介并在本周处理循环。我想我已经构建了循环,但我的主要 CommissionNotifications 中的变量未定义。

我想我必须创建一个对象并引用存储在我其他类中的变量...我是否在正确的轨道上?

该计划要求年度销售,然后根据它们所涉及的括号计算佣金支付。佣金支付是通过课堂上的If语句完成的,然后程序会显示他们可以获得的收益,如果他们的销售额增加5,000到1.5 *那里的销售额。 IE如果他们的销售额达到10万,那么表格应显示初始佣金,然后如果他们将销售额增加到150000(1.5 *)那么他们可以赚取什么

这是我的班级:

public class Calculations {

     double TotalSales;      
     double ComRate = 0.025;         
     double AnnualSal = 80000;       
     double compensation;
     double SalesTarget;
     double Acceleration = 1.5;
     double chart;
     double ComAccFactor;



    public double getCommissionNotifications() {
        return CommissionNotifications;
    }

    public void setCommissionNotifications(double commissionNotifications) {
        CommissionNotifications = commissionNotifications;
    }

    public double CommissionNotifications; {

         if (TotalSales > 120000){

             CommissionNotifications = AnnualSal  + (TotalSales * (ComRate + Acceleration));
         } else if (TotalSales > SalesTarget * .8) {

           CommissionNotifications = AnnualSal + (TotalSales * ComRate);

         } else {;

             CommissionNotifications = AnnualSal;

         }
    }
}

这是我的主要

import java.util.*;
import java.text.*;



public class Paycheck {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

            Scanner input = new Scanner (System.in);

            NumberFormat nf = NumberFormat.getCurrencyInstance();

            System.out.println("Enter Total Commission Sales:  ");  
            double TotalSales = input.nextDouble();                 

            double Base = TotalSales;
            double finish = TotalSales * 1.5;

            System.out.println("Your Total compensation with your annual sales is:  " + getCommissionNotifications);

            int i = Base + 5000;

            while (i <= finish) {

                System.out.println(Base);

                TotalSales += 5000;
         }

    }

}

2 个答案:

答案 0 :(得分:1)

getCommisionNotifications是班级Calculations的成员。要访问它,您需要创建一个新的Calculations对象:

Calculations c = new Calculations();
c.getCommisionNotifications();

答案 1 :(得分:0)

此代码看起来很像带有大写变量名的C#。无论如何,你的 getCommissionNotifications方法应根据return处理TotalSales的内容逻辑。它也不清楚为什么你需要一个setter方法,所以我已经评论过了。

至于使用以下类,您需要Calculations

的实例
Calculations calc = new Calculations();
double TotalSales = input.nextDouble();                 
calc.TotalSales = TotalSales;

// double Base = TotalSales; // Duplicate variable not needed
double finish = TotalSales * 1.5;

System.out.println("Your Total compensation with your annual sales is:  " + calc.getCommissionNotifications());
public class Calculations {

     double TotalSales;      
     double ComRate = 0.025;         
     double AnnualSal = 80000;       
     double compensation;
     double SalesTarget;
     double Acceleration = 1.5;
     double chart;
     double ComAccFactor;

    public double getCommissionNotifications() {
         if (TotalSales > 120000){
             return AnnualSal  + (TotalSales * (ComRate + Acceleration));
         } else if (TotalSales > SalesTarget * .8) {
             return AnnualSal + (TotalSales * ComRate);
         } else {
             return AnnualSal;
         }
    }

    // Not sure why this is needed... You have a dynamic getter method

    //public void setCommissionNotifications(double commissionNotifications) {
    //    CommissionNotifications = commissionNotifications;
    //}
}