簡體   English   中英

使用循環和隨機生成器的程序(從Java開始)

[英]Program using loops and random generator (beginning java)

街道網格中的酒鬼隨機地選擇四個方向之一,然后絆倒到下一個路口,然后再次隨機地選擇四個方向之一,依此類推。 您可能會認為,平均而言,醉漢不會走得太遠,因為選擇會相互抵消,但事實並非如此。 將位置表示為整數對(x,y)。 實施酒鬼從(0,0)開始的100個交叉路口的走行並打印結束位置

有人可以幫忙嗎? 我在同一個程序中使用隨機生成器和循環完全迷失了

以下是我所擁有的。 它符合要求,但不會打印任何內容,而且我不確定我是否隨機得到了100個交叉點

import java.util.*;

class Drunkard {
    int x, y; 
    Drunkard(int x, int y) {
    this.x = x; 
    this.y = y;
    } 
    void moveNorth() {
    this.y -= 1; 
    }
    void moveEast() {
    this.x += 1; 
    }
    void report() {
    System.out.println("Hiccup: " + x + ", " + y); 
    } 
} 

class Four {
    public static void main(String[] args) {
    Random generator = new Random(); 
    Drunkard drunkard = new Drunkard(100, 100); 
    int direction; 
    for (int i = 0; i < 100; i++) {
        direction = Math.abs(generator.nextInt()) % 4; 
        if        (direction == 0) { // N
        drunkard.moveNorth();
        } else if (direction == 1) { // E
        drunkard.moveEast(); 
        } else if (direction == 2) { // S
        System.out.println("Should move South."); 
        } else if (direction == 3) { // W
        System.out.println("Should move West."); 
        } else {
        System.out.println("Impossible!"); 
        } 
        System.out.drunkard.report(); 
    } 
    }
} 

您的程序將是:

初始化

循環:1到100,請執行以下操作:

 i = random() if i<=0.25 : go north if i>0.25 and i<=0.5 : go south if i>0.5 and i<= 0.75 : go east if i>0.75 and i<= 1 : go west 

結束循環

顯示最后一點。

我看到各種問題:

  • 您正在將酒鬼的頭寸初始化為100,100。 賦值表示初始化為0,0。
  • System.out.drunkard.report()絕對不會編譯。 只需調用drunkard.report()
  • 說明說明要打印最終位置,因此您需要將對drunkard.report()的調用下移一行,以使其位於for循環之外。
  • 您尚未為moveSouth或moveWest編寫方法。 編寫它們,並在適當的位置添加對其的調用。
  • 為了直接運行第四類,必須將其公開。
  • 良好的Java編程實踐表明,每個類都應位於其自己的文件中,但這可能與您的講師要求您執行的操作背道而馳。

但是,我認為這不是您的問題。 我認為您嘗試如何/在何處運行程序存在問題。 您說它可以正常編譯,但不輸出任何輸出。 您知道在編譯之后還有另一個步驟可以運行該程序,對嗎?

要清楚,這是您應該做的。 在命令行中,確保您位於.java文件所在的目錄中。 我將假設它名為Four.java 鍵入以下內容,在每一行之后按Enter。 (不要鍵入$提示符)

$ javac *.java
$ java Four

我復制了上面發布的代碼,解決了我突出顯示的問題,並按照上面的指示進行了操作; 它完美地工作。

您可以使用

int direction = (new Random()).nextInt(4);

並使用此方向變量來確定他要行走的位置。 在這種情況下,我將使用遞歸而不是循環。

從0,0開始。 生成隨機數以確定位置並更新位置。

  • 不確定您生成隨機數的方式,這對我來說似乎很好。

     Point currentLocation = new Point(); currentLocation.setLocation(0, 0); Point newLocation = new Point(0,0); Random random = new Random(); //make 100 moves for(int i=0; i<100; i++) { int k = random.nextInt(4); if(k == 0) { //use your drunkard method here newLocation.setLocation(currentLocation.getX(), currentLocation.getY() + 5); } else if (k == 1) { //go south newLocation.setLocation(currentLocation.getX(), currentLocation.getY() - 5); } else if (k == 2) { //go east newLocation.setLocation(currentLocation.getX() + 5, currentLocation.getY()); } else if(k == 3) { //go west newLocation.setLocation(currentLocation.getX() - 5, currentLocation.getY()); } currentLocation.setLocation(newLocation); } System.out.println(currentLocation); 

    }

您沒有完全實現隨機生成器。

Random generator = new Random();
int    direction = generator.nextInt(4); // This will return a random int 
                                         // between 0 and 3

使用Random()時,其他一些有用的技巧如下:

int i = generator.nextInt(4)+2; // This will return a random int 
                                // between 2 and 5

我強烈建議你看看這個 ,如果你真的想學習的所有巧妙的技巧,你可以使用隨機類做的。

我為此所做的全部工作就是創建一個循環,該循環生成一個介於-1和1之間的隨機數,並將其值相加100倍。 為x和y執行該操作。

    int x = 0;
    int y = 0;

    //intial = (0,0)
    //North = (0, 1)
    //South = (0, -1)
    //East = (1, 0)
    //West = (-1, 0)

    for(int i = 0; i < 100; i++)
    {
        x += (int) (Math.random() * 3) + (-1);
        y += (int) (Math.random() * 3) + (-1);
    }
    System.out.printf("The Drunkard is now located at: (%d, %d)", x, y);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM