我知道你们不在这里为我做功课,但我似乎无法让这个工作。我在类MPhRunner中收到一个错误,指出类MilesPerHour中的构造函数不能应用于给定的类型。我想我只是不明白这意味着什么。
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();
test.setNums(dist, hrs, mins);
test.calcMPH();
test.print();
}
}
import java.util.Scanner;
import static java.lang.System.*;
import static java.lang.Math.*;
public class MilesPerHour
{
private int distance, hours, minutes;
private double mph;
MPHRunner one= new MPHRunner();
public MilesPerHour(int dist ,int hrs,int mins)
{
setNums(dist,hrs,mins);
mph=0.0;
}
public void setNums(int dist, int hrs, int mins)
{
minutes=mins;
hours=hrs;
distance=dist;
}
public void calcMPH()
{
mph= distance /(hours*60+minutes)/60;
}
public void print()
{System.out.print("Your speed is"+mph);
}
}
答案 0 :(得分:2)
你的MilesPerHour构造函数有3个参数......你没有给它!试试:
MilesPerHour test = new MilesPerHour(dist, hrs, mins);
...并删除以下行(因为构造函数已经这样做了,所以它是多余的):
test.setNums(dist, hrs, mins);
答案 1 :(得分:0)
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);
test.setNums(dist, hrs, mins);
test.calcMPH();
test.print();
}
}
class MilesPerHour
{
private int distance, hours, minutes;
private double mph;
MPHRunner one= new MPHRunner();
public MilesPerHour(int dist ,int hrs,int mins)
{
setNums(dist,hrs,mins);
mph=0.0;
}
public void setNums(int dist, int hrs, int mins)
{
minutes=mins;
hours=hrs;
distance=dist;
}
public void calcMPH()
{
mph= distance /(hours*60+minutes)/60;
}
public void print()
{System.out.print("Your speed is"+mph);
}
}
我修改了你的代码并且工作正常。希望这会对你有帮助。
答案 2 :(得分:0)
构造函数与类具有相同的名称。
类MilesPerHour
public MilesPerHour< - constructor
对于方法,您也可以创建带参数的构造函数。
喜欢
public MilesPerHour(int dist,int hrs,int mins)
如果您想使用MilesPerHour,请使用MilesPerHour milePerHour = new MilesPerHour(dis, hrs, mins);