这是我目前正在处理的基础课程的代码。我一直收到错误,但我不确定我做错了什么。任何帮助将不胜感激。谢谢!
package miles;
public class Miles {
private int miles;
private int gas;
public static int mpg;
public void setMiles(int miles) {
this.miles = miles;
}
public void setGas(int gas){
this.gas = gas;
}
public int getMpg(){
mpg = miles/gas;
return mpg;
}
}
package miles;
import java.util.Scanner;
public class MilesDriver {
public static void main(String [] args) {
Miles gas = new Miles();
Scanner input = new Scanner(System.in);
System.out.println("Enter Miles: ");
int setGas = input.nextInt();
System.out.println("Enter Gas: ");
int setMiles = input.nextInt();
System.out.printf("MPG: %n%s%n", gas.getMpg() );
}
}
答案 0 :(得分:0)
您实际上忘了设置对象的值:
System.out.println("Enter Miles: ");
int setMiles = input.nextInt();
gas.setMiles(setMiles);
System.out.println("Enter Gas: ");
int setGas = input.nextInt();
gas.setGas(setGas);
尽量不要给变量命名与对象的方法同名。你会迷惑自己并陷入困境。