use*_*814 0 java variables scope case switch-statement
所以这是我的作业,对我而言,它似乎是一个可变范围的问题.我已经尝试定义一个全局,但也许我做错了,因为这似乎也不满足我的编译器.我目前正在使用SublimeText2进行代码编写,但似乎没什么帮助.我应该使用Eclipse或Aptana Studio等IDE吗?
// EmployeeBonus2.java - This program calculates an employee's yearly bonus.
import javax.swing.*;
public class EmployeeBonus2
{
public class Globals {
public double employeeBonus;
}
public static void main(String args[])
{
// Declare and initialize variables.
String employeeName;
String salaryString;
double employeeSalary;
String ratingString;
int employeeRating;
double employeeBonus;
final double BONUS_1 = .15;
final double BONUS_2 = .10;
final double BONUS_3 = .06;
final double NO_BONUS = 0.00;
final int RATING_1 = 1;
final int RATING_2 = 2;
final int RATING_3 = 3;
// This is the work done in the housekeeping() method
// Get user input.
employeeName = JOptionPane.showInputDialog("Enter employee's name: ");
salaryString = JOptionPane.showInputDialog("Enter employee's yearly salary: ");
ratingString = JOptionPane.showInputDialog("Enter employee's performance rating: ");
// Convert Strings to int or double.
employeeSalary = Double.parseDouble(salaryString);
employeeRating = Integer.parseInt(ratingString);
switch(employeeRating)
{
case 1: employeeBonus = (employeeSalary * BONUS_1);
break;
case 2: employeeBonus = (employeeSalary * BONUS_2);
break;
case 3: employeeBonus = (employeeSalary * BONUS_3);
break;
};
// This is the work done in the endOfJob() method
// Output.
System.out.println("Employee Name " + employeeName);
System.out.println("Employee Salary $" + employeeSalary);
System.out.println("Employee Rating " + employeeRating);
System.out.println("Employee Bonus $" + employeeBonus);
System.exit(0);
}
}
Run Code Online (Sandbox Code Playgroud)
尝试编译时出现此错误.
EmployeeBonus2.java:54: variable employeeBonus might not have been initialized
System.out.println("Employee Bonus $" + employeeBonus);
Run Code Online (Sandbox Code Playgroud)
如果default没有任何case语句匹配,您需要一个案例.
| 归档时间: |
|
| 查看次数: |
2276 次 |
| 最近记录: |