Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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_Loops_Math - Fatal编程技术网

如何找出哪位玩家的出价最接近游戏java?

如何找出哪位玩家的出价最接近游戏java?,java,loops,math,Java,Loops,Math,我的价格是正确的程序仍然是不完整的,因为我需要找出谁是我的赢家每次 游戏的赢家必须有最接近对象价值的最高出价,但不能超过对象的价值 如何将获胜者添加到我的计划中? 以下是我目前的代码: public class PriceisRight { public static void main(String args[]) { new PriceisRight(); } public PriceisRight() { System.out.println("

我的价格是正确的程序仍然是不完整的,因为我需要找出谁是我的赢家每次

游戏的赢家必须有最接近对象价值的最高出价,但不能超过对象的价值

如何将获胜者添加到我的计划中? 以下是我目前的代码:

public class PriceisRight
{

  public static void main(String args[])
  {

    new PriceisRight();
  }

  public PriceisRight()
  {

    System.out.println("Welcome to the Price is Right!\n");

    String name1 = IBIO.inputString("Name of contestant #1: ");
    String name2 = IBIO.inputString("Name of contestant #2: ");
    String name3 = IBIO.inputString("Name of contestant #3: ");
    String name4 = IBIO.inputString("Name of contestant #4: ");
    System.out.println("");

  char again = 'y';
  while (again == 'Y' || again == 'y')
   {
    String THING = item ();
    System.out.println("The item to bid on is a "+ THING +".");
    System.out.println("The contestant who is the closest without going");
    System.out.println("over wins. The maximum bid is $1000.\n");

    int bid1 = IBIO.inputInt ( name1 +", what is your bid? ");
    int bid2 = IBIO.inputInt ( name2 +", what is your bid? ");
    int bid3 = IBIO.inputInt ( name3 +", what is your bid? ");
    int bid4 = IBIO.inputInt ( name4 +", what is your bid? ");

    again = IBIO.inputChar ("Play again? (y/n) ");
    System.out.println ("");
   }    
  }


  public String item ()
   {
    int num = (int)(Math.random() * 5);
    int price = 0;
    String object = "";
     if (num == 1)
      {
        object = ("sofa");
        price = 987;
      } 

     else if (num == 2)
     {
        object = ("TV");
        price = 560;
     }

     else if(num == 3)
     {
        object = ("bed");
        price = 226;
      } 

     else if(num == 4)
      {
        object = ("table");
        price = 354;
      } 

     else
     {
        object = ("chair");
        price = 70;
      }
      return object;
    }

}

首先,你应该有一个类来保存玩家的相关信息。榜样

class Player
{
    public String name;
    public int currentBid = 0;

    public Player(String name)
    {
        this.name = name;
    }

    public void bid(int amount)
    {
        this.currentBid = amount;
    }
}
现在,创建实例

player1 = new Player("Player 1");
player2 = new Player("Player 2");
player3 = new Player("Player 3");
出价吧

在你的主课上,试试这个

public Player highestBidder()
{
    Player currentHighestBidder;

    //I am assuming you would logically keep track of the number of players, in an ArrayList in this case.
    for(int p = 0; p < players.size(); p++)
    {
        //Could easily combine this 'if' and the next one, but I prefer to keep my conditionals separated. It is probably a better practice to combine them using '||'
        if(currentHighestBidder == null)
        {
            currentHighestBidder = players.get(p);
        }

        if(players.get(p).currentBid > currentHighestBidder.currentBid)
        {
            currentHighestBidder = players.get(p);
        }
    }
    return currentHighestBidder;
}
public Player highest bidder()
{
玩家目前出价最高;
//我假设你会在逻辑上跟踪玩家的数量,在这种情况下,在ArrayList中。
对于(int p=0;pcurrentHighestBider.currentBid)
{
CurrentHighestBider=玩家。获取(p);
}
}
返回电流最高的投标人;
}

我更喜欢直接访问非重要字段,但是非常欢迎您添加一个方法来获取您的播放器的
名称
,并将字段
设置为私有
。我只是懒洋洋地打字;)

您是否已尝试实现此功能?你有设定价格的代码,但你不使用它。你需要以某种方式返回价格,然后用它与出价进行比较。是的,这是我的问题。我该怎么做?先试试什么,什么都行。您需要将价格输入到main方法中,但现在只返回一个带有对象名称的
字符串。考虑创建一个类/EnUM来表示对象吗?java是一种面向对象语言;太多的学生和初学者总是倾向于从原始人的角度思考问题。最好像这样封装。