我正在编写一个将公里数转换为英里数的java程序。我的代码是:
public class Ex7Program {
public void start() {
String userKilometres;
double kilometres = getKilometresFromUser();
double miles = convertKilometresToMiles(kilometres);
printResult(kilometres, miles);
}
private double getKilometresFromUser() { // Insert your 3 methods here.
System.out.print("Enter kilometres: ");
String userKilometres = Keyboard.readInput();
double kilometres = Double.parseDouble(userKilometres);
return kilometres;
}
private double convertKilometresToMiles(double kilometres) {
double miles = 0.621371 * kilometres;
return miles;
}
private void printResult(double kilometes, double miles) {
System.out.print(kilometres + " Kilometres is " + miles + " miles ");
}
}
我在打印结果的最后一个语句中收到错误,其中显示:
Error: kilometres cannot be resolved to a variable
我花了很长时间尝试修复它,经过多次失败尝试后我决定来这里。我出错的任何建议?帮助将非常感激。
答案 0 :(得分:7)
printResult(double,double)的方法参数称为千米,需要将其重命名为千米。
答案 1 :(得分:3)
有一个拼写错误。在参数中它是kilometes
,你在里面写了kilometres
在两个地方将其更改为kilometres
。
private void printResult(double kilometres, double miles) {
System.out.print(kilometres+ " Kilometres is " + miles + " miles ");
-------------------^
}