如何将int数组更改为字符串数组?

时间:2012-09-13 20:54:00

标签: java arrays string multidimensional-array

好的,标题可能是骗人的。我想做的就是拿我的宾果游戏程序,当打印第二张宾果卡时,我想用“X”代替所有“0”。我以为我必须将数组更改为字符串,但我不确定从哪里开始。

以下是Bingo计划:

import java.util.*;
import java.io.*;
import java.util.Arrays;

 public class Bingo
 {
public static final int ROWS = 5;
public static final int COLS = 5;
public static final int VERTICAL = 1;
public static final int DIAGONAL = 2;
public static final int HORIZONTAL = 3;
public static int winFound;
public static int currPick = 0;
public static int randomPick = 0;
public static int WinFound;

public static void main(String[] args)
{   
int Totcards;
int[][] card = new int[ROWS][COLS];
int[] picks = new int[25];

fillCard (card);
printCard(card);
playGame(card);
printCard(card);
finalCard(card);

   }

private static void fillCard (int[][] card)
{
//  FileReader fileIn = new FileReader("Bingo.in");
//  Bufferreader in = new Bufferreader(fileIn);

    try {
    Scanner scan = new Scanner(new File("bingo.in"));
      for (int i=0; i<card.length; i++){
               for (int j=0; j<card[0].length; j++){
                card[i][j] = scan.nextInt();
    }
    }
    } catch(FileNotFoundException fnfe) {
      System.out.println(fnfe.getMessage());
    }

}

private static void printCard (int[][] card)
{
    System.out.println("\n\tYOUR BINGO CARD : ");
    System.out.println("\n\tB    I    N    G    O");
    System.out.println("\t----------------------");

    for (int i=0; i<card.length; i++){
          for (int j=0; j<card[0].length; j++){
              System.out.print("\t" + card[i][j]);
              }
    System.out.print("\n");
    }
}

private static void playGame (int[][] card)
{

    int numPicks = 0;       

    System.out.println("\n\tBINGO NUMBERS PICKED AT RANDOM FROM BIN: ");    
    while (true)
            {
               markCard (card);   // Generate a random num & zero-it out
       winFound = checkForWin(card);  //  Look for zero sums
       numPicks++;

               if (winFound != 0)
               {
        if (winFound == 1)
        {
            System.out.print("\n\n\tYOU WIN WITH A VERTICAL WIN AFTER " + numPicks + " PICKS\n");
        }
        else if (winFound == 2){
            System.out.print("\n\n\tYOU WIN WITH A DIAGONAL WIN AFTER " + numPicks + " PICKS\n");
        }
        else if (winFound == 3){
            System.out.print("\n\n\tYOU WIN WITH A HORIZONTAL WIN AFTER " + numPicks + " PICKS\n");
        }

                 announceWin (numPicks);
                 return;
               }
            }


   }        
private static void markCard (int[][] card)
{
  int randomPick = (int) (Math.random() * 74) + 1;


  for (int j = 0;  j < ROWS;  j++){
        for (int k = 0;  k < COLS;  k++){
            if (card[j][k]==randomPick)
                    card[j][k] = 0;}    
    }
    System.out.print("\t " + randomPick + " ");
System.out.print("");
}

private static int checkForWin(int[][] card)
{
  int sum=0;

   for (int i = 0;  i < ROWS;  i++)
       {
        sum = 0;
        for (int j = 0;  j < COLS;  j++)
        sum += card[i][j];

        if (sum == 0)
            return HORIZONTAL;
       }

       for (int j = 0;  j < COLS;  j++)
       {
         sum = 0;
         for (int i = 0;  i < ROWS;  i++)
            sum += card[i][j];

        if (sum == 0)
            return VERTICAL;
       }

       sum = 0;
       for (int i = 0;  i < ROWS;  i++)
            sum += card[i][ROWS-i-1];
       if (sum == 0)
            return DIAGONAL;

       sum = 0;
       for (int i = 0;  i < ROWS;  i++)
          sum += card[i][i];

       if (sum == 0)
            return DIAGONAL;

       return WinFound;
    } 

private static void makeCard(int[][] card, int[] picks)
{
            int count = 100;
            int currPick = 0;
            for (int i=0; i<count; i++){
             currPick = (int)(Math.random() * 74) + 1;
             System.out.print(" " + currPick + "\n");
    picks[i] = currPick;
            }
}

private static void announceWin(int numPicks)
{

}

private static boolean duplicate (int currPick, int[] picks, int numPicks)
{
    for (int i = 0;  i < numPicks;  i++){
                    if (picks[i] == currPick){
                            return true;}
        }
            return false;

}

private static void finalCard (int[][] card)
{ 
    Arrays.sort(card);
    final String stringRep = Arrays.toString(card);
    final String[] out =
        stringRep.substring(1, stringRep.length() - 1).split("\\s*,\\s*");

    System.out.println(Arrays.toString(out));
}
   }

2 个答案:

答案 0 :(得分:1)

为什么不从一开始就使用String[][]字段?您仍然可以将String值与int s(例如Integer.valueOf)进行比较,这样您就不必切换类型运行时...

答案 1 :(得分:1)

试试这个:

System.out.print("\t" + (card[i][j] == 0 ? "X" : card[i][j]))