system.out.println上的语法错误?

时间:2015-06-12 22:19:37

标签: java system

这是常量类型double。

我的system.out.println代码出现此语法错误:

令牌上的语法错误&#34 ;;",@ expected

谢谢!

public final class Netpay {

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

public static final double FEDERAL_TAX_PERCENT = 10.00;
public static final double STATE_TAX_PERCENT = 4.5;
public static final double SS_PERCENT = 6.2;
public static final double MEDICARE_PERCENT = 1.45;
public static final double PAY_PER_HOUR = 7.25;



    int hoursPerWeek = 40;

    double grossPay = hoursPerWeek * PAY_PER_HOUR;
    double federalTax = grossPay * FEDERAL_TAX_PERCENT / 100;
    double stateTax = grossPay * STATE_TAX_PERCENT / 100;
    double medicare = grossPay *  MEDICARE_PERCENT / 100;
    double socialSecurity = grossPay * SS_PERCENT / 100;
    double netPay = grossPay -  (federalTax + stateTax + medicare + socialSecurity);

    system.out.println("Hours Per Week = 40");
    system.out.println("Gross Pay= grossPay");
    system.out.println("Net Pay = netPay");

    system.out.println("Deductions:");
    system.out.println("Federal= federalTax");
    system.out.println("State = stateTax");
    system.out.println("Social Security = socialSecurity");
    system.out.println("Medicare = medicare");


}

2 个答案:

答案 0 :(得分:1)

将常量移到main方法之外:

public final class Netpay {

    public static final double FEDERAL_TAX_PERCENT = 10.00;
    public static final double STATE_TAX_PERCENT = 4.5;
    public static final double SS_PERCENT = 6.2;
    public static final double MEDICARE_PERCENT = 1.45;
    public static final double PAY_PER_HOUR = 7.25;

    public static void main(String[] args) {
        // the rest of your code goes here
    }
}

如前所述,make系统有一个大写的S:System.out.println

答案 1 :(得分:0)

理解这个概念 - 对于方法中的变量,使用publicstatic是没有意义的。因为在方法中所有变量都是本地的,所以没有公共,私有,受保护等等。现在,Java中的静态意味着将保留在JVM生命周期的堆区域中。现在,由于内部方法,变量和对象是本地的,并且一旦方法执行完成就会被垃圾收集(直到你传递它们的引用等),所以没有静态点。 你不能使用静态内部方法,无论是静态方法还是实例方法。

1.始终将应用程序不同部分可能需要的常量保存在一个常量文件中。如果您不需要它,那么最好将它作为方法内的局部变量或实例变量(如果需要) 可以使用{classname}。{variable_name}来从应用程序的任何位置访问静态实例变量 因此,您的常量Java文件可能是:

public class ApplicationConstants {
    public static final double FEDERAL_TAX_PERCENT = 10.00;
    public static final double STATE_TAX_PERCENT = 4.5;
    public static final double SS_PERCENT = 6.2;
    public static final double MEDICARE_PERCENT = 1.45;
    public static final double PAY_PER_HOUR = 7.25;
}

2. System.out.println()是打印到控制台的东西,它接受的参数是String。所以,如果你将任何内容放在引号内,那么它将是一个字符串(这就是你得到Gross Pay= grossPay结果的原因),你要打印一个动态值然后使用“+”运算符附加它。

public static void main(String[] args) {
            int hoursPerWeek = 40;

            double grossPay = hoursPerWeek * ApplicationConstants.PAY_PER_HOUR;
            double federalTax = (grossPay * ApplicationConstants.FEDERAL_TAX_PERCENT) / 100;    //Corrected your formula - (A+B)/C
            double stateTax = (grossPay * ApplicationConstants.STATE_TAX_PERCENT) / 100;        //Corrected your formula - (A+B)/C
            double medicare = (grossPay * ApplicationConstants.MEDICARE_PERCENT) / 100;         //Corrected your formula - (A+B)/C
            double socialSecurity = (grossPay * ApplicationConstants.SS_PERCENT) / 100;         //Corrected your formula - (A+B)/C
            double netPay = grossPay
                    - (federalTax + stateTax + medicare + socialSecurity);

            System.out.println("Hours Per Week = " + hoursPerWeek);
            System.out.println("Gross Pay = " + grossPay);
            System.out.println("Net Pay = " + netPay);

            System.out.println("Deductions:");
            System.out.println("Federal = " + federalTax);
            System.out.println("State = " + stateTax);
            System.out.println("Social Security = " + socialSecurity);
            System.out.println("Medicare = " + medicare);
        }