可能是我沒有寫完整吧,還是我沒有理解好
1 接口
public interface SetAndGetWeight
{
public void setW(double weight);
public double getW();
}
2 類
class Train
{
SetAndGetWeight[] things;
public void Train(SetAndGetWeight[] things)
{
this.things=things;
}
public void returnTotalWeight()
{
double totalweight=0;
for(int i=0;i
{
totalweight=totalweight+things[i].getW();
}
System.out.println("Train總的質量是:"+totalweight);
}
}
第3個類
import java.util.Scanner;
class TrainComWeight
{
public static void main(String[] arg)
{
Scanner rd=new Scanner(System.in);
System.out.println("請輸入船上人數:");
int peonum=rd.nextInt();
System.out.println("請輸入船上Tv數:");
int tvnum=rd.nextInt();
System.out.println("請輸入船上COM數:");
int comnum=rd.nextInt();
System.out.println("請輸入船上wash數:");
int washnum=rd.nextInt();
int totalnum=peonum+tvnum+comnum+washnum;
SetAndGetWeight[] things=new SetAndGetWeight[totalnum]; Train tr=new Train(things);
System.out.println("請輸入各個人的體重!");
for(int i=0;i
{
System.out.println("請輸入第"+i+"peo的重量");
things[i].setW(rd.nextDouble());
}
System.out.println("請輸入TV的重量");
for(int j=peonum;j
{
System.out.println("請輸入第"+j+"tv的重量");
things[j].setW(rd.nextDouble());
}
System.out.println("請輸入COM的重量");
for(int k=peonum+tvnum;k
{
System.out.println("請輸入第"+k+"CIN的重量");
things[k].setW(rd.nextDouble());
}
System.out.println("請輸入WASH的重量");
for(int l=peonum+tvnum+comnum;l
{
System.out.println("請輸入第"+l+"WASH的重量");
things[l].setW(rd.nextDouble());
}
//System.out.println(train)
}
}
其中紅色的地方Train tr=new Train(things);會報錯"找不到符號"