简体   繁体   English

我在我的驱动程序类中不断出错,但我不确定我做错了什么

[英]I keep getting an error in my driver class, but I'm not sure what I've done wrong

This is a code for a foundations class that I'm working on currently.这是我目前正在研究的基础课程的代码。 I keep getting an error, but I'm not sure what I've done incorrectly.我不断收到错误消息,但我不确定我做错了什么。 Any help would be greatly appreciated.任何帮助将不胜感激。 Thanks!谢谢!

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() );


    }
}

You actually forgot to set the values of your object :您实际上忘记设置对象的值:

    System.out.println("Enter Miles: ");
    int setMiles = input.nextInt();
    gas.setMiles(setMiles);

    System.out.println("Enter Gas: ");
    int setGas = input.nextInt();
    gas.setGas(setGas);

Try not to give a variables name the same name as a method of your object.尽量不要给变量名与对象的方法同名。 You'll end confusing yourself and get stuck.你最终会迷惑自己并陷入困境。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java:尝试调用shooterExperiments方法时,我总是收到错误消息。 我不确定我在做什么错或确切如何调用方法 - Java: I keep getting an error when trying to call the shooterExperiments method. I'm not sure what I'm doing wrong or exactly how to call a method 我的代码有问题,但是我不确定这是什么吗? - There is something wrong in my code but i'm not sure what it is? 我不确定分区功能出了什么问题 - I'm not sure what's wrong with my partition function 编译器不断吐出“类接口或枚举预期” 谁能发现我做错了什么? - Compiler keeps spitting "Class Interface or Enum Expected" can anyone spot what I've done wrong? 我的代码有问题,我不确定出了什么问题 - I'm having trouble with my code and I'm not sure what's wrong 我不确定要放在try语句中的内容,并且不断收到“ Ecxeption在线程“ main”中的错误” - I'm unsure what to put in my try statement and I keep getting an “Ecxeption in thread ”main“” error 不知道为什么我会收到NullPointerException错误 - Not sure why I'm getting a NullPointerException error 我已经审查了这个代码几个小时,但我不确定它有什么问题 - I've been reviewing this code for hours but I'm not sure whats wrong with it 我的程序产生找不到符号错误,我不确定我哪里出错了 - My program produces cannot find symbol error and I'm not sure where I am going wrong 我已经安装了Gecko驱动程序,但是仍然出现错误 - I've installed Gecko driver but still getting an error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM