編譯問題。編譯器是說即使我用它上線的變量不使用25在java中遇到變量問題
Error: [line: 21] Warning: The value of the local variable pay is not used
// CreatePayroll.java takes wages and hours from file and creates pay file.
import java.util.*;
import java.io.*;
public class CreatePayroll {
public static void main(String[] args) {
try{
//Scanner for input file
File inputFile = new File("employees.txt");
Scanner fin = new Scanner(inputFile);
//Printwriter for output file
File outputFile= new File("payroll.txt");
PrintWriter fout = new PrintWriter(outputFile);
//read input file creates new file
while (fin.hasNextLine()) {
String firstName = fin.next();
String lastName = fin.next();
double wage = fin.nextDouble();
double hours = fin.nextDouble();
//Calculates pay
if (hours > 40) {
double pay = (wage*40)+((hours-40)*(wage*1.5));
} else {
double pay = wage*hours;
//last line to print to out file
fout.println(firstName + " " + lastName + " $" + pay);
}
}
//cleanup
fin.close();
fout.close();
System.out.print("DONE! See 'payroll.txt'.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
閱讀有關變量的作用域。 – Tunaki
您在'if'塊中的'pay'聲明在'else'塊中不可見。 –
請不要用_ [FIXED] _標記您的問題,而應接受幫助您解決問題的最佳答案。如果沒有任何現有答案有幫助,請發佈您自己的解決方案作爲答案並接受。 –