-2

我完全被难住了。我是java编程的初学者。我有以下代码,但无法理解为什么在我的公共静态无效行上出现“非法类型开始”错误。请帮忙。

另外:我怎样才能将这个不断增长的程序分成多个类?

`package annual.salary;

 /*Annual Salary Calculator
 * Version 1.1
 */

 import java.util.Scanner; //Program uses Scanner
 import java.text.DecimalFormat; //Configures output for dollar amounts
 import java.util.InputMismatchException; //Checks for invalid user input

 public class AnnualSalary
 {      
 public void displayMessage()
 {
    //display intro message to calculator
    System.out.println("Annual Compensation Calculator");
 }        
 public static void main (String[] args,)
 {

    double salary; //This is the annual sales value
    double rate; //Commission rate once minimum sales reached
    double commission; //This is the amount of commision made
    double pay; //Salesperson's total pay
    double sales; //annual sales, prompted user input
    double incentive; //bonus rate once sales target is exceeded
    double max; //sets maximum sales amount to calculate for potential earnings table
    boolean isValidNumber=false; //default flag for numeric sales input
    Scanner input = new Scanner( System.in ); //allows user input from command window

    DecimalFormat dollar = new DecimalFormat("#,##0.00");
    salary = 50000.00;
    rate = 0.05;
    incentive = 0.0625;
    sales = 0;
    max = sales * 1.5;

    System.out.println(" Enter The Annual Sales "); //prompt for input from user

    while(!isValidNumber) //Continue to prompt user until a valid number is entered
    {    
        try //try/catch block to handle invalid characters.
        {  
            sales = input.nextDouble(); //Read number from user, if alpha character is               keyed then an exception is thrown
            isValidNumber = true; //Set our flag so we can exit the loop      
        }
        catch(InputMismatchException ioe) //Catch input not matching excpected type.
        {                  
            System.out.println("Please enter a numeric value."); //re-prompt user
            input.next(); //If Scanner translates successfully it advances, however if it fails it must be manually advanced.
        }
    }    

    while (sales < 96000.00) //conditional statement for sales below 80% of goal
    {
        pay = salary ;
        System.out.println("Total Annual Compensation is $"+dollar.format(pay));
    }
    while (sales >= 96000.00 && sales <= 120000.00) //conditional statement for sales 80%-100% of goal
    {
        commission = sales * rate;
        pay = commission + salary;
        System.out.println("Total Annual Compensation is $"+dollar.format(pay));
    }
    while (sales > 120000.00) //conditional statement for sales exceeding goal
    {    
        commission = sales * incentive;
        pay = commission + salary;
        System.out.println("For: $" + sales + "   The Total Compensation is: $" +dollar.format(pay));
    }            

    while(sales < max) //This sets the while loop to start at minimum & end at the max
    { 
        sales = sales + 5000;
        if (sales < 96000.00) //conditional statement for sales below 80% of goal
        {
        pay = salary;
        }
        else if (sales >= 96000.00 && sales <= 120000.00)
        {
        commission = sales * rate;
        pay = commission + salary;
        }
        while (sales > 120000.00) //conditional statement for sales exceeding goal
        {    
        commission = sales * incentive;
        pay = commission + salary;
        System.out.println("For: $" + sales + "   The Total Compensation is: $" +dollar.format(pay));
        }  
        } //Ends the body of the while loop.
    }
}    //end calculator'
4

5 回答 5

6

删除,. 这一定是导致该错误的原因。

public static void main (String[] args) // No comma

至于您的第二个问题,您必须问自己,您要进行哪些幺正运算。

你们每个人的 while 循环都会做一个特殊的处理。也许您想用特定的方法将这些处理分开?

我看到有一个用于从用户那里获取数据。您可以创建一个方法,其目标是请求这些数据,然后将它们返回到主程序等。

此外,您可能希望为计算器本身创建一个特定的类,并在另一个类中使用它来进行处理。

既然你说你开始了。我认为这将是一个好的开始。

于 2013-05-07T07:42:01.510 回答
0

从. ,_ public static void main (String[] args,)这将解决您当前的问题。

附带说明一下,对于您将来可能面临的问题(并且可能导致创建另一个 SO 问题),

在下面提到的语句中更改为whileif

while (sales < 96000.00)

while (sales >= 96000.00 && sales <= 120000.00)

while (sales > 120000.00)

while (sales < max)

while (sales > 120000.00)
于 2013-05-07T07:48:14.307 回答
0
public static void main (String[] args,)

应该

public static void main (String[] args)
于 2013-05-07T07:42:11.657 回答
0

改变

public static void main (String[] args,)

                              --------^  //extra comma here 

public static void main (String[] args)
于 2013-05-07T07:43:20.880 回答
0

在您对 main 的论证之后有一个逗号。

public static void main(String[] args,)

尝试删除逗号。

public static void main(String[] args)

要将您的程序拆分为多个类,您应该阅读有关重构和设计模式的内容。伟大的“四人帮”写了优秀的书来学习。设计模式是一种为程序提供良好结构的方法,使其对程序员很方便。重构就是改变你的代码结构,让它变得更好。

于 2013-05-07T07:46:33.907 回答