价格是正确的程序.需要将字符串分成几部分,并将失败的玩家替换为数组中的其他玩家



我对这个程序有几个问题(它还没有完成(。我有一个名为 player 的字符串,它打印从 20 个玩家的数组中选择的 4 个随机玩家。我需要一种方法将它们分成单独的名称,而无需使用 split 命令(尚未在课堂上学习(。此外,我需要一种方法来用新玩家替换价格正确的游戏的失败者。因此,如果总共有 4 名玩家,并且 1 名是赢家,我需要用数组中的 3 名新人替换 3 名输家。最后,我需要一种方法,当数组从 20 个人中随机选择时,不会重复玩家。提前感谢大家!

import java.io.*;
import java.util.*;
public class PriceIsRight
{
   //calls the start method
   public static void main(String[] args) throws FileNotFoundException
   {
      Random rand = new Random();
      start(rand);
   }
   //reads names from a text file of 20 names and stores them in an array
   public static String[] getNames() throws FileNotFoundException
   {
      String[] name = new String[20];
      Scanner input = new Scanner(new File("names.txt"));
      int count = 0;
      while (input.hasNext())
      {
         name[count]=input.next();
         count++;
      }
      return name;
   }
   //reads items from a text file of 20 items and stores them in an array
   public static String[] getItems() throws FileNotFoundException
   {
      String[] item = new String[40];
      Scanner input = new Scanner(new File("items.txt"));
      int count = 0;
      while (input.hasNext())
      {
         item[count]=input.next();
         count++;
      }
      return item;
   }
   //selects 4 random names and stores them in an array
   public static String selectNames(Random rand) throws FileNotFoundException
   {
      int count=0;
      String[] name = getNames();
      String players = "";
      while(count < 4)
      {
      int intRand = rand.nextInt(19);
      players = players + name[intRand] + " ";
      count ++;
      }
      return players;
   }
   //selects 1 random item and stores it in an array
   public static String selectItem(Random rand) throws FileNotFoundException
   {
      String[] item = getItems();
      String items = "";
      int intRand = rand.nextInt(19);
      items = item[intRand];
      return items;
   }
   //displays the name of the selected item. Then gets players' bids and stores them in a size 4 array called bids.
   public static void displayItem(Random rand) throws FileNotFoundException
   {
      Scanner kb = new Scanner(System.in);
      String players = selectNames(rand);
      double guess[] = new double[3];
      String item = selectItem(rand);
      System.out.println("Item: " + item);
      System.out.print

   }
   //accepts player's bids array and the actual price of the item. Finds the closest bid to the price and returns the index of the best bid. Displays all the bids made by the player, the price of the item, and the winner of the round
   public static void priceIsRight() throws FileNotFoundException
   {
   }
   //checks if there are any players left in the array name
   public static void checkPlayers() throws FileNotFoundException
   {
   }
   //calls all the methods to start the game
   public static void start(Random rand) throws FileNotFoundException
   {
      System.out.println("You are about to play price is right game.nAn item will be displayed on the secreen nThe players will enter their bids, whoever has the closest bid to the actual price will win.nnnnWait let me choose the playersnnHere is the list of the players: ");
      getNames();
      String players = selectNames(rand);
      System.out.println(players);
      getItems();
      selectItem(rand);
      System.out.println();
      displayItem(rand);
   }
}

您可以使用它来拆分播放器字符串。

    String 4players = Arrays.toString(playerarray);
    String[] splited = 4players.split(","); // use the symbol that seperates the players 
    for(String s : splited)
    System.out.println(s.trim());

最新更新