我完全难过了。我是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'
答案 0 :(得分:6)
删除,
。它必定是导致错误的原因。
public static void main (String[] args) // No comma
至于你的第二个问题,你必须问问自己,你想做的单一操作是什么。
你们每个人都在进行特殊处理。也许你想用特定的方法将这些治疗分开?
我看到有一个用来从用户那里获取数据。您可以创建一个方法,其目标是请求这些数据,然后将它们返回到主程序等。
此外,您可能希望为计算器本身创建一个特定的类,而在另一个类中,使用它来进行治疗。
既然你说你刚开始。我认为这将是一个良好的开端。
答案 1 :(得分:0)
public static void main (String[] args,)
应该是
public static void main (String[] args)
答案 2 :(得分:0)
更改
public static void main (String[] args,)
--------^ //extra comma here
到
public static void main (String[] args)
答案 3 :(得分:0)
在你的争论之后有一个逗号。
public static void main(String[] args,)
尝试删除逗号。
public static void main(String[] args)
要将程序拆分为多个类,您应该阅读有关重构和设计模式的内容。伟大的“四人帮”写了很好的书来学习。 设计模式是一种为程序提供良好结构的方法,使程序员可以使用它。 重构正在改变代码的结构,使其更好。
答案 4 :(得分:0)
从,
中删除public static void main (String[] args,)
。这将解决您当前的问题。
另一方面,对于您将来可能面临的问题(并可能导致另一个SO问题的创建),
在下面提到的陈述中将while
更改为if
。
while (sales < 96000.00)
while (sales >= 96000.00 && sales <= 120000.00)
while (sales > 120000.00)
while (sales < max)
while (sales > 120000.00)