Java 如何根据以下条件验证每个输入字符
我正在开发一个Boggle游戏。我想验证按键/向上/向下等的两件事Java 如何根据以下条件验证每个输入字符,java,performance,validation,optimization,boggle,Java,Performance,Validation,Optimization,Boggle,我正在开发一个Boggle游戏。我想验证按键/向上/向下等的两件事 使用键盘时,应限制用户只输入板中的字符(并将其附加到输入字符串/单词中),否则跳过它。向用户显示4 x 4的转向架板。比如说 A B B A A A B B B B A A A B A B 在这种情况下,C-Z是无效的输入,它们不应该附加到输入字符串中,我希望用户在输入完整单词后只按ENTER键一次 输入的字符必须与上一个字符相邻(垂直、水平或对角) 目前,我正在使用scanner(system.in)获取一个字符串,并使用两
A B B A
A A B B
B B A A
A B A B
在这种情况下,C-Z是无效的输入,它们不应该附加到输入字符串中,我希望用户在输入完整单词后只按ENTER键一次system.in
)获取一个字符串,并使用两个单独的函数验证这些条件,但我需要现场验证
我的代码:
package com.boggle;
import java.io.*;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.Random;
public class BoggleBoard {
String[] Cons = {"B","C","D","F","G","H","J","K","L","M",
"N","P","Q","R","S","T","V","W","X","Y","Z"};
String[] Vow = {"A","E","I","O","U"};
String[][] board = new String[4][4];
String[] adjcntStr = new String[16];
boolean[] ConsMark = new boolean[21];
String Bstring = "";
boolean isValid;
Random rand = new Random(System.currentTimeMillis());
public void ShowNear(){
for(int i=0; i<16; i++){
cout(adjcntStr[i]+"\n");
}
}
public void Play(){
cout("Enter Your Word\n");
Scanner read = new Scanner(System.in);
String xYz = read.next();
if(!ValidChar(xYz))
{
cout("Invalid Choice\n");
Play();
}
else
{
if(!ValidSqnc(xYz, 0))
{
cout("\nInvalid Sequence\n");
Play();
}
else
{
cout("\n\nWord: "+xYz);
}
}
read.close();
}
public boolean ValidChar(String t){
for(int i = 0; i<t.length();i++){
if(!Bstring.contains(""+t.charAt(i)+"")){
return false;
}
}
return true;
}
public boolean ValidSqnc(String S, int i){
if(i+1 == S.length())
{
isValid = true;
return true;
}
if(adjcntStr[Bstring.indexOf(Character.toString(S.charAt(i)))].contains(Character.toString(S.charAt(i+1))))
{
ValidSqnc(S, ++i);
}
else
{
isValid = false;
}
return isValid;
}
public void cout(String T){
System.out.print(T);
}
public String GetStr(){
return Bstring;
}
public BoggleBoard(){
setBoard();
getBoard();
near();
}
public void setBoard(){
int c=0;
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if( (int)rand.nextInt(2)==0 && c<5){
board[i][j] = Vow[c];
Bstring = Bstring + Vow[c];
++c;
}
else
{
board[i][j]= Cons[UniqueInt()];
Bstring = Bstring + board[i][j];
}
}
}
}
public int UniqueInt(){
int index;
while(true)
{
index = (int)(rand.nextInt(21));
if(ConsMark[index]==false)
{
ConsMark[index] = true;
break;
}
}
return index;
}
public void getBoard(){
for(int i=0;i<4;i++){
for(int f=0;f<4;f++){
cout(board[i][f]+" ");
}
System.out.println();
}
}
public void near(){
int row = 0;
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
adjcntStr[row] = "";
for (int x = Math.max(0, i - 1); x <= Math.min(i + 1, board.length); x++) {
for (int y = Math.max(0, j - 1); y <= Math.min(j + 1,
board[i].length); y++) {
if (x >= 0 && y >= 0 && x < board.length
&& y < board[i].length) {
if(x!=i || y!=j){
adjcntStr[row] = adjcntStr[row] + board[x][y];
}
}
}
}
row++;
}
}
}
package com.boggle;
导入java.io.*;
导入java.io.File;
导入java.io.IOException;
导入java.util.Scanner;
导入java.util.Random;
公共级挡板{
字符串[]Cons={“B”、“C”、“D”、“F”、“G”、“H”、“J”、“K”、“L”、“M”,
“N”、“P”、“Q”、“R”、“S”、“T”、“V”、“W”、“X”、“Y”、“Z”};
字符串[]Vow={“A”、“E”、“I”、“O”、“U”};
字符串[][]板=新字符串[4][4];
字符串[]adjcntStr=新字符串[16];
布尔值[]ConsMark=新布尔值[21];
字符串Bstring=“”;
布尔值是有效的;
Random rand=新的Random(System.currentTimeMillis());
公共空间展示区(){
对于(int i=0;i保存您的代码并解释您的具体困难。@PM77-1该过程非常困难,简单的复制粘贴是否有效?您需要提供足够的信息,向我们展示您迄今为止所取得的成就以及您的具体困难所在。纯代码转储将不会有帮助。有几种解决方案;哪一种最合适e将取决于您所采取的方法。控制台应用程序可能从系统中读取。在
中,Swing应用程序将使用addKeyListener()
处理单个按键。可能会重复发布您的代码,并解释您的具体困难。@PM77-1此过程非常困难,简单的复制粘贴是否有效?您需要提供足够的信息,向我们展示您迄今为止所完成的工作以及您的确切位置。纯代码转储将没有帮助。有每个解决方案;哪一个最合适取决于您采取的方法。控制台应用程序可能从系统中读取。在中,作为Swing应用程序,将使用addKeyListener()
来处理各个按键。可能重复