gpt4 book ai didi

java - 我收到了关于计算每小时英里数的作业,但我的运行程序似乎无法找到我的时间变量

转载 作者:行者123 更新时间:2023-12-01 12:25:04 25 4
gpt4 key购买 nike

import java.util.Scanner;
import static java.lang.System.*;
import static java.lang.Math.*;

public class MPHRunner
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(in);

out.print("Enter the distance :: ");
int dist = keyboard.nextInt();

out.print("Enter the hours :: ");
int hrs = keyboard.nextInt();

out.print("Enter the minutes :: ");
int mins = keyboard.nextInt();

MilesPerHour test = new MilesPerHour(dist, hrs, mins,time);
test.calcMPH(dist,time);
test.print();
}
}

当我尝试编译它时,它告诉我它无法找到变量time。它应该从其他类中找到,包括在下面......

import java.util.Scanner;
import static java.lang.System.*;
import static java.lang.Math.*;

public class MilesPerHour
{
public int distance, hours, minutes;
public double mph, time;

public MilesPerHour()
{
setNums(0,0,0,0);
mph=0.0;
}

public MilesPerHour(int dist, int hrs, int mins)
{
Scanner keyboard = new Scanner(System.in);
out.println("enter distance");
out.println("enter hours");
out.println("enter minutes");
}

public void setNums(int dist, int hrs, int mins, double time)
{
distance = dist;
hours = hrs;
minutes = mins;
time = ((hrs*60)+mins)/60;
}

public void calcMPH(int dist, double time)
{
mph = dist/time;
}

public void print(int mph, int dist, double time)
{
System.out.println("at "+mph+"MPH, you can go "+dist+" miles in "+time+"hours.");
}
}

如果您能提供任何帮助,我们将不胜感激。

最佳答案

Scanner keyboard = new Scanner(in);

out.print("Enter the distance :: ");
int dist = keyboard.nextInt();

out.print("Enter the hours :: ");
int hrs = keyboard.nextInt();

out.print("Enter the minutes :: ");
int mins = keyboard.nextInt();

对象中发生的事情并不重要。在这里,您声明并定义 disthrsmins,然后在下一行中:

MilesPerHour test = new MilesPerHour(dist, hrs, mins,time);

这不会编译,因为 time 尚未声明。

编辑

经过仔细检查,我注意到时间是根据其他字段计算的。在这种情况下,您不需要在参数中指定它,因为您不需要传入它。更改

public void setNums(int dist, int hrs, int mins, double time)

public void setNums(int dist, int hrs, int mins)

并在调用 setNums 方法时将其从参数列表中删除。

注意:如果您的老师无法诊断这里的编译问题,那么我肯定会将其报告给您的教育机构。这是新手程序员应该能够发现的。

关于java - 我收到了关于计算每小时英里数的作业,但我的运行程序似乎无法找到我的时间变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26427312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com