Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的寻宝程序_Java_Computer Science - Fatal编程技术网

Java中的寻宝程序

Java中的寻宝程序,java,computer-science,Java,Computer Science,对于我所在的一个类,我们要编写一个程序,要求玩家猜测x和y坐标,直到找到宝藏。该项目由两个名为Treasure.java和TreasureHunt.java的文件组成,它们使用Treasure类来玩游戏。到目前为止我掌握的代码是 package treasurehunt; public class Treasure { public int POSITIONS_PER_ROW = 20; public int MAX_DISTANCE = 401; private String name;

对于我所在的一个类,我们要编写一个程序,要求玩家猜测x和y坐标,直到找到宝藏。该项目由两个名为Treasure.java和TreasureHunt.java的文件组成,它们使用Treasure类来玩游戏。到目前为止我掌握的代码是

package treasurehunt;

public class Treasure {

public int POSITIONS_PER_ROW = 20;
public int MAX_DISTANCE = 401;
private String name;
private int xLocation;
private int yLocation;
private Boolean found;


    public Treasure(){
     name = "";
     xLocation = 0;
     yLocation = 0;
     found = false;
    }

    public Treasure(String name){
        this.name = name;
        xLocation = 0;
        yLocation = 0;
        found = false;
    }

    public int getXLocation(){
        return xLocation;
    }

    public int getYLocation(){
        return yLocation;
    }

    public boolean isFound(){
        return found; 
    }

    public void hideTreasure(){
        xLocation = 1+(int)(Math.random()*POSITIONS_PER_ROW);
        yLocation = 1+(int)(Math.random()*POSITIONS_PER_ROW);
    }

    public int treasureStatus(int xStick , int yStick){
     if (xStick == xLocation && yStick == yLocation){
        return 0;
    } else if (xStick != xLocation || yStick != yLocation) {
        return Math.abs((yStick-yLocation)+(xStick-xLocation));           
    } else return MAX_DISTANCE;



}


}
---------------------------------------及-------------------------------------------------

package treasurehunt;

import java.util.Scanner;

public class TreasureHunt {

public static final int MAX_DISTANCE = 401;
public static final int POSITIONS_PER_ROW = 20;

public static void main(String[] args) {
    Treasure gold = new Treasure();
    Treasure diamond = new Treasure();
    char playAgain = 0;

    gold.hideTreasure();
    diamond.hideTreasure();

    Scanner keyboard = new Scanner(System.in);

    System.out.println("Treasures have been hidden.");

    do{
    System.out.println("");
    System.out.print("Enter x and y coordinates to search: ");
    int xStick = keyboard.nextInt();
    int yStick = keyboard.nextInt();

    int dist1 = diamond.treasureStatus(xStick, yStick);
    int dist2 = gold.treasureStatus(xStick , yStick);

    if (dist1 == 0 || dist1 == MAX_DISTANCE){
        System.out.println("Diamonds: FOUND!");
    }else{
        System.out.println("Diamonds: " + dist1 + " away");
    }

    if (dist2 == 0 || dist2 == MAX_DISTANCE){
        System.out.println("Gold: FOUND!");
    }else{
        System.out.println("Gold: " + dist2 + " away");
    }

    }while(!diamond.isFound() && !gold.isFound());




    do{
    System.out.println("Play again? y/n:");
    playAgain = keyboard.next().charAt(0);
    }while(diamond.isFound() && gold.isFound());

}
}
当程序正确运行时,它应该是这样的

Treasures have been hidden.
Enter x and y coordinates to search: 17 11
Diamonds: 2 away
Gold: 9 away

Enter x and y coordinates to search: 17 13
Diamonds: FOUND!
Gold: 7 away

Enter x and y coordinates to search: 21 13
Diamonds: FOUND!
Gold: 9 away

Enter x and y coordinates to search: 17 16
Diamonds: FOUND!
Gold: 4 away

Enter x and y coordinates to search: 17 19
Diamonds: FOUND!
Gold: 1 away

Enter x and y coordinates to search: 18 19
Diamonds: FOUND!
Gold: FOUND!

It took you 6 tries to find 2 treasures.

Play again? y/n: n
我的问题是,一旦你找到了第一个宝藏,当你猜到第二个宝藏的坐标时,你如何防止它从“发现”变为“未发现”。我现在使用的代码删除了“FOUND!”并在每次猜到第二个宝藏的坐标时将其更改为“away”。谢谢你的帮助

试试这样的方法(与您最初的帖子几乎相同)。您需要更新您的Treasure类,添加一个像这样的setFound方法,并按如下所示更改hideTreasure方法

// Set's the found 
public void setFound(boolean found) {
  this.found = found;
}

// Setting found to false on hide!
public void hideTreasure() {
  this.found = false;
  xLocation = 1 + (int) (Math.random() * POSITIONS_PER_ROW);
  yLocation = 1 + (int) (Math.random() * POSITIONS_PER_ROW);
}

// Finally update the main method to use the changes above.
public static void main(String[] args) {
    Treasure gold = new Treasure();
    Treasure diamond = new Treasure();
    char playAgain = 0;

    gold.hideTreasure();
    diamond.hideTreasure();

    Scanner keyboard = new Scanner(System.in);
    System.out.println("Treasures have been hidden.");

    try {
      do {
        System.out.println("");
        System.out
            .print("Enter x and y coordinates to search: ");
        int xStick = keyboard.nextInt();
        int yStick = keyboard.nextInt();

        if (!diamond.isFound()) {
          int dist1 = diamond.treasureStatus(xStick,
              yStick);
          if (dist1 == 0 || dist1 == MAX_DISTANCE) {
            diamond.setFound(true);
            System.out.println("Diamonds: FOUND!");
          } else {
            System.out.println("Diamonds: " + dist1
                + " away");
          }
        }

        if (!gold.isFound()) {
          int dist2 = gold.treasureStatus(xStick,
              yStick);
          if (dist2 == 0 || dist2 == MAX_DISTANCE) {
            gold.setFound(true);
            System.out.println("Gold: FOUND!");
          } else {
            System.out.println("Gold: " + dist2
                + " away");
          }
        }
        if (diamond.isFound() && gold.isFound()) {
          System.out.println("Play again? y/n:");
          playAgain = keyboard.next().charAt(0);
          if (playAgain != 'Y' && playAgain != 'y') {
            System.exit(0);
          } else {
            diamond.hideTreasure();
            gold.hideTreasure();
          }
        }
      } while (true);
    } finally {
      keyboard.close();
    }
  }
尝试类似的方法(与您最初的帖子几乎相同)。您需要更新您的Treasure类,添加一个像这样的setFound方法,并按如下所示更改hideTreasure方法

// Set's the found 
public void setFound(boolean found) {
  this.found = found;
}

// Setting found to false on hide!
public void hideTreasure() {
  this.found = false;
  xLocation = 1 + (int) (Math.random() * POSITIONS_PER_ROW);
  yLocation = 1 + (int) (Math.random() * POSITIONS_PER_ROW);
}

// Finally update the main method to use the changes above.
public static void main(String[] args) {
    Treasure gold = new Treasure();
    Treasure diamond = new Treasure();
    char playAgain = 0;

    gold.hideTreasure();
    diamond.hideTreasure();

    Scanner keyboard = new Scanner(System.in);
    System.out.println("Treasures have been hidden.");

    try {
      do {
        System.out.println("");
        System.out
            .print("Enter x and y coordinates to search: ");
        int xStick = keyboard.nextInt();
        int yStick = keyboard.nextInt();

        if (!diamond.isFound()) {
          int dist1 = diamond.treasureStatus(xStick,
              yStick);
          if (dist1 == 0 || dist1 == MAX_DISTANCE) {
            diamond.setFound(true);
            System.out.println("Diamonds: FOUND!");
          } else {
            System.out.println("Diamonds: " + dist1
                + " away");
          }
        }

        if (!gold.isFound()) {
          int dist2 = gold.treasureStatus(xStick,
              yStick);
          if (dist2 == 0 || dist2 == MAX_DISTANCE) {
            gold.setFound(true);
            System.out.println("Gold: FOUND!");
          } else {
            System.out.println("Gold: " + dist2
                + " away");
          }
        }
        if (diamond.isFound() && gold.isFound()) {
          System.out.println("Play again? y/n:");
          playAgain = keyboard.next().charAt(0);
          if (playAgain != 'Y' && playAgain != 'y') {
            System.exit(0);
          } else {
            diamond.hideTreasure();
            gold.hideTreasure();
          }
        }
      } while (true);
    } finally {
      keyboard.close();
    }
  }
尝试类似的方法(与您最初的帖子几乎相同)。您需要更新您的Treasure类,添加一个像这样的setFound方法,并按如下所示更改hideTreasure方法

// Set's the found 
public void setFound(boolean found) {
  this.found = found;
}

// Setting found to false on hide!
public void hideTreasure() {
  this.found = false;
  xLocation = 1 + (int) (Math.random() * POSITIONS_PER_ROW);
  yLocation = 1 + (int) (Math.random() * POSITIONS_PER_ROW);
}

// Finally update the main method to use the changes above.
public static void main(String[] args) {
    Treasure gold = new Treasure();
    Treasure diamond = new Treasure();
    char playAgain = 0;

    gold.hideTreasure();
    diamond.hideTreasure();

    Scanner keyboard = new Scanner(System.in);
    System.out.println("Treasures have been hidden.");

    try {
      do {
        System.out.println("");
        System.out
            .print("Enter x and y coordinates to search: ");
        int xStick = keyboard.nextInt();
        int yStick = keyboard.nextInt();

        if (!diamond.isFound()) {
          int dist1 = diamond.treasureStatus(xStick,
              yStick);
          if (dist1 == 0 || dist1 == MAX_DISTANCE) {
            diamond.setFound(true);
            System.out.println("Diamonds: FOUND!");
          } else {
            System.out.println("Diamonds: " + dist1
                + " away");
          }
        }

        if (!gold.isFound()) {
          int dist2 = gold.treasureStatus(xStick,
              yStick);
          if (dist2 == 0 || dist2 == MAX_DISTANCE) {
            gold.setFound(true);
            System.out.println("Gold: FOUND!");
          } else {
            System.out.println("Gold: " + dist2
                + " away");
          }
        }
        if (diamond.isFound() && gold.isFound()) {
          System.out.println("Play again? y/n:");
          playAgain = keyboard.next().charAt(0);
          if (playAgain != 'Y' && playAgain != 'y') {
            System.exit(0);
          } else {
            diamond.hideTreasure();
            gold.hideTreasure();
          }
        }
      } while (true);
    } finally {
      keyboard.close();
    }
  }
尝试类似的方法(与您最初的帖子几乎相同)。您需要更新您的Treasure类,添加一个像这样的setFound方法,并按如下所示更改hideTreasure方法

// Set's the found 
public void setFound(boolean found) {
  this.found = found;
}

// Setting found to false on hide!
public void hideTreasure() {
  this.found = false;
  xLocation = 1 + (int) (Math.random() * POSITIONS_PER_ROW);
  yLocation = 1 + (int) (Math.random() * POSITIONS_PER_ROW);
}

// Finally update the main method to use the changes above.
public static void main(String[] args) {
    Treasure gold = new Treasure();
    Treasure diamond = new Treasure();
    char playAgain = 0;

    gold.hideTreasure();
    diamond.hideTreasure();

    Scanner keyboard = new Scanner(System.in);
    System.out.println("Treasures have been hidden.");

    try {
      do {
        System.out.println("");
        System.out
            .print("Enter x and y coordinates to search: ");
        int xStick = keyboard.nextInt();
        int yStick = keyboard.nextInt();

        if (!diamond.isFound()) {
          int dist1 = diamond.treasureStatus(xStick,
              yStick);
          if (dist1 == 0 || dist1 == MAX_DISTANCE) {
            diamond.setFound(true);
            System.out.println("Diamonds: FOUND!");
          } else {
            System.out.println("Diamonds: " + dist1
                + " away");
          }
        }

        if (!gold.isFound()) {
          int dist2 = gold.treasureStatus(xStick,
              yStick);
          if (dist2 == 0 || dist2 == MAX_DISTANCE) {
            gold.setFound(true);
            System.out.println("Gold: FOUND!");
          } else {
            System.out.println("Gold: " + dist2
                + " away");
          }
        }
        if (diamond.isFound() && gold.isFound()) {
          System.out.println("Play again? y/n:");
          playAgain = keyboard.next().charAt(0);
          if (playAgain != 'Y' && playAgain != 'y') {
            System.exit(0);
          } else {
            diamond.hideTreasure();
            gold.hideTreasure();
          }
        }
      } while (true);
    } finally {
      keyboard.close();
    }
  }

我认为您可以使用一个标志(int值或布尔值)值。您可以根据找到的宝藏状态或到tresure的距离更改标志的值

我想你可以声明一个变量,比如

int flag=0;
一旦你找到宝藏设置标志=1

 if( flag=0)
 {
     String pout="write distance value here"
 }

 if(flag==1)
 {
    String pout="found !!";
 }

我认为您可以使用一个标志(int值或布尔值)值。您可以根据找到的宝藏状态或到tresure的距离更改标志的值

我想你可以声明一个变量,比如

int flag=0;
一旦你找到宝藏设置标志=1

 if( flag=0)
 {
     String pout="write distance value here"
 }

 if(flag==1)
 {
    String pout="found !!";
 }

我认为您可以使用一个标志(int值或布尔值)值。您可以根据找到的宝藏状态或到tresure的距离更改标志的值

我想你可以声明一个变量,比如

int flag=0;
一旦你找到宝藏设置标志=1

 if( flag=0)
 {
     String pout="write distance value here"
 }

 if(flag==1)
 {
    String pout="found !!";
 }

我认为您可以使用一个标志(int值或布尔值)值。您可以根据找到的宝藏状态或到tresure的距离更改标志的值

我想你可以声明一个变量,比如

int flag=0;
一旦你找到宝藏设置标志=1

 if( flag=0)
 {
     String pout="write distance value here"
 }

 if(flag==1)
 {
    String pout="found !!";
 }

实际上,您只需要在状态方法中将
found
设置为一个永久停止点。现在您有了一个“Found”变量,但它对您没有任何好处,除非您在代码中将其设置为不可更改的停止点

在状态方法中类似这样的内容

 public int treasureStatus(int xStick , int yStick){
 if ((xStick == xLocation && yStick == yLocation) || found == true){
    return 0;
 } else if ...
这将使方法,
if-found==true
,每次只返回0,将宝藏标记为找到


(请注意,第一次回答堆栈溢出问题时,我为我糟糕的格式表示歉意。)

您只需在状态方法中设置
found
永久停止点。现在您有了一个“Found”变量,但它对您没有任何好处,除非您在代码中将其设置为不可更改的停止点

在状态方法中类似这样的内容

 public int treasureStatus(int xStick , int yStick){
 if ((xStick == xLocation && yStick == yLocation) || found == true){
    return 0;
 } else if ...
这将使方法,
if-found==true
,每次只返回0,将宝藏标记为找到


(请注意,第一次回答堆栈溢出问题时,我为我糟糕的格式表示歉意。)

您只需在状态方法中设置
found
永久停止点。现在您有了一个“Found”变量,但它对您没有任何好处,除非您在代码中将其设置为不可更改的停止点

在状态方法中类似这样的内容

 public int treasureStatus(int xStick , int yStick){
 if ((xStick == xLocation && yStick == yLocation) || found == true){
    return 0;
 } else if ...
这将使方法,
if-found==true
,每次只返回0,将宝藏标记为找到


(请注意,第一次回答堆栈溢出问题时,我为我糟糕的格式表示歉意。)

您只需在状态方法中设置
found
永久停止点。现在您有了一个“Found”变量,但它对您没有任何好处,除非您在代码中将其设置为不可更改的停止点

在状态方法中类似这样的内容

 public int treasureStatus(int xStick , int yStick){
 if ((xStick == xLocation && yStick == yLocation) || found == true){
    return 0;
 } else if ...
这将使方法,
if-found==true
,每次只返回0,将宝藏标记为找到


(请注意,第一次回答堆栈溢出问题时,我为我糟糕的格式表示歉意。)

treasehunt
类中,可以包含两个布尔变量来跟踪寻宝的状态。例如,
boolean-isDiamondFound
boolean-isGoldFound
。找到宝藏后,将相应的布尔变量设置为true。打印时,如果布尔值为true,则打印找到,否则打印到宝藏的距离。实际上,宝藏实现了
FOUND
boolean。为什么不直接设置宝藏的
found=true
,然后,如果找到,则按说明打印找到的内容?在
treasehunt
类中,可以包含两个布尔变量来跟踪寻宝的状态。例如,
boolean-isDiamondFound
boolean-isGoldFound
。找到宝藏后,将相应的布尔变量设置为true。打印时,如果布尔值为true,则打印找到,否则打印到宝藏的距离。实际上,宝藏实现了
FOUND
boolean。为什么是d