1

我正在用 C 语言编写一个名为 Thambola 的印度游戏(类似于 Bingo)。在这个游戏中,用户得到一张 3x9 的票,计算机要求用户输入第二个程序(从 1-90 中随机选择数字)选择的数字。如果输入的号码存在于车票中,则应将号码更改为“x”,表示该号码已被删除。我在这里需要帮助。如何用'x'替换已经打印的数字?我读了这个C - 删除和替换打印的项目,但它没有帮助,因为我有 27 个数字要更改。请帮助我。这是代码的一部分:

int number,i,j;
const char x='x';

printf("\nEnter the number:");
scanf("%d",&number); // number entered by the user from the Picker

for (i=0;i<3;i++)
    for (j=0;j<9;j++)
        {if (ticket[i][j]==number)
            ticket[i][j]=x;
printf("%d",ticket[i][j]); //if the input number is present in the ticket, this should change it to 'x

    }

getchar();

}

4

2 回答 2

0

Main problem of your code snippet is your logic behind assigning values to variables.

As anyone can see, you have an array of integers but you want to assign a char to one of its elements. Actually you can do it by casting, but the result will be the ASCII value of the 'x'.

{if (ticket[i][j]==number) // number is an integer
            ticket[i][j]=x; // x is a char

Since 'x' has the ASCII value of 120,which is out of range from the possible numbers in the ticket, you can safely cast 'x' to its integer value and then assign it to its array element. In printing, if you see 120 print 'x'.

In the situations where your char's integer value is in the range of the other possible integer values, pick another integer value and treat this value as 'x' in logic flow (for example, pick 0 for the corresponding integer, if 0 comes print 'x').

You can use ncurses or simply print a ticket state then clear the screen just before printing modified ticket for output.

于 2012-06-09T07:05:53.687 回答
0
public class sample1 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        List<int[]> outer = new ArrayList<int[]>(9);
        List<int[]> inner = new ArrayList<int[]>(9);
        int[][] final_arr = new int[9][18];
        int[][][] final_arr2 = new int[6][3][9];


        int[][] multi = new int[][]{
            {2,3,5,7,9,12,13,15,17},
            {1,3,4,5,6,7,11,14,16,18},
            {1,2,4,6,8,9,12,13,17,18},
            {3,5,7,9,10,11,12,14,15,16},
            {1,4,6,7,8,10,11,13,14,18},
            {2,3,5,8,9,12,13,15,16,17},
            {1,2,4,6,7,10,11,13,17,18},
            {1,3,5,8,9,12,13,15,16,18},
            {2,4,6,7,9,10,11,14,15,16,17}
        };

        for( int i=0;i<9;i++){
            outer.add(multi[i]);
        }

        for(int x=0;x<9;x++){
            int [] temp = new int[18];
                for(int k=0;k<outer.get(x).length;k++){
                    //System.out.print(outer.get(x)[k]-1 +" ");
                    int row = outer.get(x)[k]-1;
                    temp[row]=1;
                }
                //System.out.println();
            inner.add(temp);
            }
        System.out.println();
        int count=1;
        for(int i=0;i<9;i++){
            List<Integer> temp = new ArrayList<Integer>();
            for(int k=0;k<outer.get(i).length;k++){
                temp.add(count);
                count++;
            }
            Collections.shuffle(temp);
            int index_of_temp=0;
            for(int j=0;j<18;j++){
                if(inner.get(i)[j]==1){
                    inner.get(i)[j]=temp.get(index_of_temp);
                    index_of_temp++;
                }
                //System.out.print(inner.get(i)[j]+ " ");
            }
            //System.out.println();
        }

        for(int i=0;i<9;i++){
            for(int j=0;j<18;j++){
                final_arr[i][j]=inner.get(i)[j];
            }
        }
        System.out.println();

        for(int i=0;i<18;i++){
            for(int j=0;j<9;j++){
              //System.out.print(final_arr[j][i]+ " ");
                final_arr2[i/3][i%3][j]=final_arr[j][i];
            }

        }
        for(int k=0;k<6;k++){
            for(int i=0;i<3;i++){
                for(int j=0;j<9;j++){
                    System.out.print(final_arr2[k][i][j]+ " ");
                }
                System.out.println();
            }
            System.out.println();
        }
    }

}
于 2017-05-16T08:49:57.717 回答