我有一個測試人員課程,我只是不確定如何使用我的藍圖工作。以下是我想要做的事情:在Java中查找網格中的位置和移動方向
「你存在於一個無限的街道和街道網格中,位置表示爲一個整數(街道,街道)的耦合,你可以使用底片,現在考慮一個隨機的酒鬼在一個十字路口選擇四個方向之一,然後絆倒到下一個十字路口進行相同的操作,等等。寫一個Drunkard類來模擬這個酒鬼出發點的方式。你的Drunkard類應該有實例變量drunkard當前的大道(x位置)和當前街道(y位置),你的班級應該有一個名爲step()
的方法,它將醉漢移動到下一個隨機選擇的相鄰交叉點,你的班級應該有另一個方法fastForward(int steps)
,它接受一個整數作爲輸入)並且從他的酒鬼移動交叉點 當前位置。你的班級應該有一個方法getLocation()
,它返回一個字符串,指示醉漢的當前位置。最後,你的類應該有一個名爲howFar()
方法,報告從那裏,他開始計算使用曼哈頓距離度量塊的醉鬼距離。」
你有什麼建議?任何幫助都行,謝謝。
這裏我有我的藍圖至今。
java.util.Random;
class Drunkard
{
private int avenue;
private int street;
Drunkard(int avenue, int street) {
this.avenue = avenue;
this.street = street;
}
Random rand = new Random();
void moveUp(){
this.street -= 1;
}
void moveDown(){
this.street += 1;
}
void moveRight(){
this.avenue += 1;
}
void moveLeft(){
this.avenue -= 1;
}
void getLocation(){
int avenue = parseInt("avenue");
int street = parseInt("street");
System.out.println("Location: " + avenue + ", " + street);
}
void fastForward(int steps)
{
for (int i=0; i < steps.length; i++}{
}
}
void step(){
}
void howFar(){
int distance = Math.abs(x1-x0) + Math.abs(y1-y0);
}
}
,我需要它與這個測試類相對應。
import java.util.Scanner;
public class DrunkardTester {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the starting avenue integer: ");
int avenue = input.nextInt();
System.out.println("Please enter the starting street integer: ");
int street = input.nextInt();
// instantiate
Drunkard jeff = new Drunkard(avenue,street);
//move 100 intersections
jeff.fastForward(100);
//find current location
String location = jeff.getLocation();
// get distance from start
int distance = jeff.howFar();
System.out.println("Current location: " + location);
System.out.println("This is " + distance + " blocks from your origin.");
}
}
如果你能更詳細地或準確地解釋你需要什麼會更好? – 2015-04-02 05:38:45
在執行Step,fast forward和howFar()方法時需要幫助嗎? – 2015-04-02 05:40:12
@sathiyaseelan我需要我的第一堂課與其下面的Tester課程中的所有內容相對應。我的第一堂課有錯誤。我的用戶應該複製一個醉酒,所以他們只會隨機移動。我需要向上,向下,向左或向右移動。他們需要被記錄。記錄是針對我所在的每個路口。所以每一個選擇都會導致新的街道+大道組合。如果要求,除了默認的交叉路口之外,必須有一個選項可以移動特定數量的交叉路口。需要爲每個實例計算和記錄移動量。謝謝! – Lorber 2015-04-02 05:44:54