斯坦福 IT

月盈亏

月月盈
最大赞力
0.00
当前赞力
100.00%
我就迷惑,AB spacship在game class里
currplanet 和moveto在 spaceship class里
这互相交叉call,就乱了,怎么把cuurrplanet跟各自的spaceship捆绑起来
 

月盈亏

月月盈
最大赞力
0.00
当前赞力
100.00%
你这个可能有问题
setCurrPlanet(currPlanet);

一个船飞不能影响另一个船的位置。

请问你这是final project还是一次assignment?
final 啊!!!
就是这个问题啊,互相影响,可是不明白怎么能把currPlanet跟飞船捆绑起来
 
最大赞力
0.00
当前赞力
100.00%
public void moveTo(String name){
int x = Planet.findPlanet(name,Spaceship.planets);
String Name = getName();

Planet currPlanet = planets.get(x);
String currName = currPlanet.getName();
System.out.println("Spaceship " +Name +" moved to " + currName);
setCurrPlanet(currPlanet);
}

这段代码逻辑不正确,moveTo 传入的参数是spaceShip 要移动去的星球,这段代码正确写法应该是:

public void moveTo(String destName){
// get current planet name
String currName = this.currPlanet.getName();
// find the destination planet index
int x = Planet.findPlanet(destName,Spaceship.planets);
System.out.println("Spaceship " +currName +" moved to " + destName);
// get destination planet object
Planet currPlanet = planets.get(x);
setCurrPlanet(currPlanet);
}
 

月盈亏

月月盈
最大赞力
0.00
当前赞力
100.00%
以前用了好久C,学了面向对象以后,就不写程序了。所以有点乱哦

如果要我写的话就是

定义3个类 game,spaceship, solar
spaceship 里有一个当前位置currentPosition,还有一个改变上前位置的方法move to。
solar里有9大行星的名字的代指9个位置。

其中game里有开始方法startgame, AB 两个宇宙飞船 和 一个太阳系solar attribute

main 里启动startgame 方法
然后在game的startgame方法里,设置2循环,9大行星一正一反用于AB宇宙飞船。

嘿嘿,对于面向对象的程序员来说,可能我这还是C的路子。不太明白啥叫交替move?
就是A先飞,比如从地球到火星,call move to 的方法,move to里要返回currenPosition,因为下一次飞行要从这个currenPosition开始飞。
但是第二步是B飞了,也call moveto, 但是用了A的currenPositon (火星),飞到水星,返回currenPosition=水星
下一次A再飞,又用了水星做currenPosition。
正确的方法应该是各自按照各自的currenPosition。
 

月盈亏

月月盈
最大赞力
0.00
当前赞力
100.00%
[/QUOTE]
public void moveTo(String destName){
// get current planet name
String currName = this.currPlanet.getName();
// find the destination planet index
int x = Planet.findPlanet(destName,Spaceship.planets);
System.out.println("Spaceship " +currName +" moved to " + destName);
// get destination planet object
Planet currPlanet = planets.get(x);
setCurrPlanet(currPlanet);
}[/QUOTE]

那你最后这个currplanet会固定跟A绑定吗?
因为如果A call这个moveTo,得到的currPlanet(比如火星),下一次B call 这个moveTo,那B就从火星开始飞了,就不对了
 

月盈亏

月月盈
最大赞力
0.00
当前赞力
100.00%
我知道attribute应该跟实例走,可是就是搞不明白怎么写,才能做到。
因为飞船的构造函数里没有currentposition
 

月盈亏

月月盈
最大赞力
0.00
当前赞力
100.00%
飞船的飞行路线是随机的,由random得出一个数,决定飞船下一次飞哪里。
但AB都要从自己上次飞到的星球离开,
AB轮流飞,都使用一个moveTo method
 

月盈亏

月月盈
最大赞力
0.00
当前赞力
100.00%
Spaceship类里面有一个方法moveTo(String dest), 你用这个类创建A, B两个实例,它们再各自A.moveTo(), B.moveTo()....
明白这个道理,就是实现不了啊
A.moveTo(), B.moveTo().. 是在Game class里
moveto在spaceship class里
 

月盈亏

月月盈
最大赞力
0.00
当前赞力
100.00%
public class Game {
private Spaceship A;
private Spaceship B;}

public class Spaceship{
private String name;
private Planet currPlanet;}
 

Similar threads

家园推荐黄页

家园币系统数据

家园币池子报价
家园币最新成交价
家园币总发行量
加元现金总量
家园币总成交量
家园币总成交价值

池子家园币总量
池子加元现金总量
池子币总量
1池子币现价
池子家园币总手续费
池子加元总手续费
入池家园币年化收益率
入池加元年化收益率

微比特币最新报价
毫以太币最新报价
微比特币总量
毫以太币总量
家园币储备总净值
家园币比特币储备
家园币以太币储备
比特币的加元报价
以太币的加元报价
USDT的加元报价

交易币种/月度交易量
家园币
加元交易对(比特币等)
USDT交易对(比特币等)
顶部