Java 字母不是';输出中是否出现t?

Java 字母不是';输出中是否出现t?,java,Java,因此,我正在创建一个基于文本的游戏,在游戏板上用字母“S,M,a,R,T”填充空格。要打败这个游戏,每行和每列只能包含5个给定字母中的1个。(有点像数独游戏)游戏运行得很好,但是当用户输入他们想要的字母所在的行和列时,它不会出现在黑板上。谁能帮我一下吗?先谢谢你。(注意:我对java编码还是新手) import java.util.Scanner; 公共课智能拼图计划{ 静态扫描仪键盘=新扫描仪(System.in); 静态字符[][]表=新字符[5][5]; 公共静态void main(字符串

因此,我正在创建一个基于文本的游戏,在游戏板上用字母“S,M,a,R,T”填充空格。要打败这个游戏,每行和每列只能包含5个给定字母中的1个。(有点像数独游戏)游戏运行得很好,但是当用户输入他们想要的字母所在的行和列时,它不会出现在黑板上。谁能帮我一下吗?先谢谢你。(注意:我对java编码还是新手)

import java.util.Scanner;
公共课智能拼图计划{
静态扫描仪键盘=新扫描仪(System.in);
静态字符[][]表=新字符[5][5];
公共静态void main(字符串[]args){
板(表);
int i=0;
而(i<5){
如果((int)表[i][0]+(int)表[i][1]+(int)表[i][2]+(int)表[i][3]+(int)表[i][4])!=391){
Move();
板(表);
}
其他的
i++;
} 
int j=0;
而(i<5){
如果((int)表[0][j]+(int)表[1][j]+(int)表[2][j]+(int)表[3][j]+(int)表[4][j])!=391){
Move();
板(表);
}
其他的
j++;
}
System.out.println(“恭喜!你一定很聪明。”);
}
公共静态无效移动(){
//征求用户的意见
系统输出打印(“输入一行(1-5):”;
int r=keyboard.nextInt();
系统输出打印(“输入一列(1-5):”;
int c=keyboard.nextInt();
系统输出打印(“输入字母(S、M、a、R或T):”;
charl=键盘.next().toUpperCase().charAt(0);
如果(L='S'| L='M'| L='A'| L='R'| L='T'){
表[r-1][c-1]=L;
}
其他的
//错误检查
System.out.println(“无效字母。使用'S',M','A','R'或'T'”);
}
公共静态无效板(char[][]t){
//给定变量
t[0][0]='S';
t[0][1]='M';
t[0][2]=“A”;
t[0][3]='R';
t[0][4]='t';
t[1][0]='';
t[1][1]=‘t’;
t[1][2]='S';
t[1][3]=‘M’;
t[1][4]='';
t[2][0]='';
t[2][1]='';
t[2][2]='R';
t[2][3]='';
t[2][4]='S';
t[3][0]='';
t[3][1]='S';
t[3][2]=‘M’;
t[3][3]='';
t[3][4]='';
t[4][0]='';
t[4][1]='';
t[4][2]=‘t’;
t[4][3]='S';
t[4][4]='';
//打印出电路板
System.out.println(“12345”);
System.out.println(“--+--+--+--+--+--”);
System.out.println(“1 |”+t[0][0]+“|”+t[0][1]+“|”+t[0][2]+“|”+t[0][3]+“|”+t[0][4]+“|”);
System.out.println(“--+--+--+--+--+--”);
System.out.println(“2 |”+t[1][0]+“|”+t[1][1]+“|”+t[1][2]+“|”+t[1][3]+“|”+t[1][4]+“|”);
System.out.println(“--+--+--+--+--+--”);
System.out.println(“3 |”+t[2][0]+“|”+t[2][1]+“|”+t[2][2]+“|”+t[2][3]+“|”+t[2][4]+“|”);
System.out.println(“--+--+--+--+--+--”);
System.out.println(“4 |”+t[3][0]+“|”+t[3][1]+“|”+t[3][2]+“|”+t[3][3]+“|”+t[3][4]+“|”);
System.out.println(“--+--+--+--+--+--”);
System.out.println(“5 |”+t[4][0]+“|”+t[4][1]+“|”+t[4][2]+“|”+t[4][3]+“|”+t[4][4]+“|”);
System.out.println(“--+--+--+--+--+--”);
}
}

问题在于,每次调用
Board()
方法时,您都会将值分配给表,您可能只需要初始化一次。为此,创建一个新方法,比如说
initialize()

并在
main()的开头调用它:


嗯,似乎每次调用Board()时都会重新初始化t数组,不是吗?是的,我在编写代码时没有注意到它。不过我现在明白了!谢谢你指出它@oldprogrammer非常感谢你为我解释它!我现在明白了:)
import java.util.Scanner;

public class SmartPuzzleProgram{
    static Scanner keyboard = new Scanner(System.in);
    static char[][] table = new char[5][5];

    public static void main (String[] args){
    Board(table);

    int i = 0;
    while (i < 5){
        if(  ((int)table[i][0] + (int)table[i][1] + (int)table[i][2] + (int)table[i][3] + (int)table[i][4]) != 391  ){
        Move();
        Board(table);
        }
            else
                i++;
            } 

    int j = 0;
    while (i < 5){
        if(  ((int)table[0][j] + (int)table[1][j] + (int)table[2][j] + (int)table[3][j] + (int)table[4][j]) != 391  ){
        Move();
        Board(table);
        }
            else
                j++;
            }

    System.out.println("Congratulations! You must be SMART.");
    }

        public static void Move(){

        // Ask for user's input
        System.out.print("Enter a row (1-5): ");
        int r = keyboard.nextInt();
        System.out.print("Enter a column (1-5): ");
        int c = keyboard.nextInt();
        System.out.print("Enter a letter (S,M,A,R or T): ");
        char L = keyboard.next().toUpperCase().charAt(0);

        if (L == 'S' || L == 'M' || L == 'A' || L == 'R' || L == 'T') {
        table[r-1][c-1] = L; 
        }
            else 
                // Error check
                System.out.println("Invalid letter. Use 'S', M', 'A', 'R' or 'T'");
            }

        public static void Board(char[][] t){   

        // Given variables 
        t[0][0] = 'S';  
        t[0][1] = 'M';
        t[0][2] = 'A';      
        t[0][3] = 'R';
        t[0][4] = 'T';  

        t[1][0] = ' ';
        t[1][1] = 'T';    
        t[1][2] = 'S';  
        t[1][3] = 'M';        
        t[1][4] = ' ';   

        t[2][0] = ' ';            
        t[2][1] = ' ';
        t[2][2] = 'R';  
        t[2][3] = ' ';  
        t[2][4] = 'S';

        t[3][0] = ' ';      
        t[3][1] = 'S';  
        t[3][2] = 'M';  
        t[3][3] = ' ';  
        t[3][4] = ' ';  

        t[4][0] = ' ';        
        t[4][1] = ' ';        
        t[4][2] = 'T';
        t[4][3] = 'S';  
        t[4][4] = ' ';  

        // Prints out the board       
        System.out.println("    1   2   3   4   5  ");
        System.out.println("   ---+---+---+---+--- ");
        System.out.println("1 | " + t[0][0] + " | " + t[0][1] + " | " + t[0][2] + " | " + t[0][3] + " | " + t[0][4] + " |");
        System.out.println("   ---+---+---+---+--- ");
        System.out.println("2 | " + t[1][0] + " | " + t[1][1] + " | " + t[1][2] + " | " + t[1][3] + " | " + t[1][4] + " |");
        System.out.println("   ---+---+---+---+--- ");
        System.out.println("3 | " + t[2][0] + " | " + t[2][1] + " | " + t[2][2] + " | " + t[2][3] + " | " + t[2][4] + " |");
        System.out.println("   ---+---+---+---+--- ");
        System.out.println("4 | " + t[3][0] + " | " + t[3][1] + " | " + t[3][2] + " | " + t[3][3] + " | " + t[3][4] + " |");
        System.out.println("   ---+---+---+---+--- ");
        System.out.println("5 | " + t[4][0] + " | " + t[4][1] + " | " + t[4][2] + " | " + t[4][3] + " | " + t[4][4] + " |");
        System.out.println("   ---+---+---+---+--- ");
        }
}
public static void initialize()
{
    System.out.println("here");
    table[0][0] = 'S';
    table[0][1] = 'M';
    table[0][2] = 'A';
    table[0][3] = 'R';
    table[0][4] = 'T';

    table[1][0] = ' ';
    table[1][1] = 'T';
    table[1][2] = 'S';
    table[1][3] = 'M';
    table[1][4] = ' ';

    table[2][0] = ' ';
    table[2][1] = ' ';
    table[2][2] = 'R';
    table[2][3] = ' ';
    table[2][4] = 'S';

    table[3][0] = ' ';
    table[3][1] = 'S';
    table[3][2] = 'M';
    table[3][3] = ' ';
    table[3][4] = ' ';

    table[4][0] = ' ';
    table[4][1] = ' ';
    table[4][2] = 'T';
    table[4][3] = 'S';
    table[4][4] = ' ';
}
public static void main(String[] args)
{
    initialize();
    Board(table);
    ...
}