尝试将项目添加到列表中

时间:2016-01-14 00:29:01

标签: java arrays object

您好我正在尝试制作文字游戏,现在我很难将字母添加到字母数组中。错误在第8行,是

  

不兼容的类型

     

发现:char

     

必需:java.lang.String

import java.util.Scanner;

public class Smartgame {
    public static void main(String[] args) {
      System.out.println("Complete the puzzle!\nMake sure each row and each column consists of the letters 'S','M','A','R' and 'T' in any order.\nNo letter shall be repeated in any row or any column");
      char[][] table = new char[5][5];
      charTable(table);
      String[] preset = {table[0][0] = 'S', table[0][1] = 'M',table[0][2] = 'A',table[0][3] = 'R',table[0][4] = 'T',table[1][1] = 'T',table[1][2] = 'S',table[1][3] = 'M',table[2][2] = 'R',table[2][4] = 'S',table[3][1] = 'S',table[3][2] = 'M',table[4][2] = 'T',table[4][3] = 'S'};
      Scanner input = new Scanner(System.in);
      int num = 0;
      int[] sums = new int[25];
      int sum = 0;
      for (int i = 0;i<5;i++){
        for (int j = 0; j<5; j++) {
          if (table[i][j] == '\0')
            num = 0;
          else
            sum = sum + ((int)table[i][j]);}}
      while (sum < 1955){
        System.out.println("Please enter a row. (1-5)");
        int row = input.nextInt();
        if (row > 5)
          System.out.println("Invalid entry, please enter a row number from 1-5.");
        else
          System.out.println("Please a column. (1-5)");
          int column = input.nextInt();
          if (column > 5)
            System.out.println("Invalid entry, please enter a column number from 1-5.");
          else
            System.out.println("Please enter any of the following letters: 'S','M','A','R' or 'T'");
            char letter = input.next().charAt(0);
            if (letter == 'S')
              preset.push("table["+row+"]["+column+"] = 'S'");
            if (letter == 'M')
              letter = table[row-1][column-1];
            else if (letter == 'A')
              letter = table[row-1][column-1];
            else if (letter == 'R')
              letter = table[row-1][column-1];
            else if (letter == 'T')
              letter = table[row-1][column-1];
             }
    }

public static void charTable(char[][] table){ 
      System.out.println("    1   2   3   4   5  ");
      System.out.println("  +---+---+---+---+---+");
      for (int i = 0;i<5;i++){
        System.out.print(i+1 + " | ");
        for (int j = 0; j<5; j++) {
          if (table[i][j] == '\0')
            System.out.print("  | ");
          else
            System.out.print("" + table[i][j] + " | ");
        }
        System.out.println();
        System.out.println("  +---+---+---+---+---+");
      }
      }} 

2 个答案:

答案 0 :(得分:1)

所以,这......

String[] preset = {
    table[0][0] = 'S', 
    table[0][1] = 'M', 
    table[0][2] = 'A',         
    table[0][3] = 'R', 
    table[0][4] = 'T', 
    table[1][1] = 'T', 
    table[1][2] = 'S', 
    table[1][3] = 'M', 
    table[2][2] = 'R', 
    table[2][4] = 'S', 
    table[3][1] = 'S', 
    table[3][2] = 'M', 
    table[4][2] = 'T',         
    table[4][3] = 'S'};

等于......

String[] preset = {
    'S', 
    'M',
    'A', 
    'R', 
    'T', 
    'T', 
    'S', 
    'M', 
    'R', 
    'S', 
    'S', 
    'M', 
    'T', 
    'S'};

正如您所看到的,您将char添加到String数组中,这显然不起作用,它们是不同的“类型”,苹果和梨

相反,也许你应该做更像......的事情。

char[] preset = {table[0][0] = 'S', table[0][1] = 'M', table[0][2] = 'A', table[0][3] = 'R', table[0][4] = 'T', table[1][1] = 'T', table[1][2] = 'S', table[1][3] = 'M', table[2][2] = 'R', table[2][4] = 'S', table[3][1] = 'S', table[3][2] = 'M', table[4][2] = 'T', table[4][3] = 'S'};

或者,您需要将char转换为String

String[] preset = {Character.toString(table[0][0] = 'S'), ... };

答案 1 :(得分:0)

String[] preset更改为char[] preset,它将解决第8行的问题。您仍然有一个问题,您希望扩展数组。在java中,数组具有恒定的长度。它们无法扩展,但您可以使用具有相同内容的新阵列替换它们。最简单的方法是使用Arrays类:

if (letter == 'S') {
    // The message you wan to add
    String toAdd = "table[" + row + "][" + column + "] = 'S'";

    // Create a copy of the array with a new length
    preset = Arrays.copyOf(preset, toAdd.length());

    // Gothrough the characters to add
    for (int i = 0; i < toAdd.length(); i++) {

        // Insert them at the right position
        preset[preset.length - toAdd.length() + i] = toAdd.charAt(i);
    }
}

另一种选择是使用StringBuilder - 类。