我希望用户输入哪一行和哪一列以读取其中的数据
例如,如果用户输入row = 6 column = 0,它将打印出Pw1
static String fullChessBoard[][] = {
{"Rb1", "Kb1", "Bb1", "Qb1", "Ab1", "Bb2", "Kb2", "Rb2"},
{"Pb1", "Pb2", "Pb3", "Pb4", "Pb5", "Pb6", "Pb7", "Pb8"},
{" ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " "},
{"Pw1", "Pw2", "Pw3", "Pw4", "Pw5", "Pw6", "Pw7", "Pw8"},
{"Rw1", "Kw1", "Bw1", "Qw1", "Aw1", "Bw2", "Kw2", "Rw2"},
};
System.out.println(Player1W + ", please make a move");
int row = scan.nextInt();
int column = scan.nextInt();
答案 0 :(得分:0)
我假设您正在编写的该程序是一个命令行程序,用户将信息放入命令提示符,然后您的程序做出响应。如果不是,请告诉我,因为我的回答在该假设下起作用。
在Java中,有多种方法可以从命令行获取输入。以下链接有一些很好的例子: [https://www.geeksforgeeks.org/ways-to-read-input-from-console-in-java/][1]
这是我写的一个简单示例,它从用户输入的字符串中输出第一个重复的字母,它使用BufferedReader类集来从InputStreamReader包装的System.in对象中获取输入。我确定这听起来很复杂,但是代码可以更好地说明这一点:
import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Set;
import java.util.TreeSet;
public class FirstRecurringCharacter {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter your string: ");
String toAnalyze = reader.readLine();
returnFirstRecurring(toAnalyze);
}
private static Character returnFirstRecurring(String toAnalyze) {
String toSearch = toAnalyze;
Set<Character> seenCharacters = new TreeSet<>();
for (int i = 0; i < toSearch.length(); i++) {
if (seenCharacters.contains(toSearch.charAt(i))) {
System.out.println(toSearch.charAt(i));
return toSearch.charAt(i);
} else {
seenCharacters.add(toSearch.charAt(i));
}
}
System.out.println("No repeating characters.");
return null;
}
}
.readLine()方法在用户按下控制台上的Enter之后获得用户输入。自己尝试!
对于您的程序,您可能希望一次提示一次,以插入行索引,然后插入列索引,如果需要,可以将其输入转换为整数,然后在程序的其余部分中使用它们。我希望这会有所帮助!
答案 1 :(得分:0)
尝试此代码
import java.util.Scanner;
public class HelloChess{
static String fullChessBoard[][] = {
{"Rb1", "Kb1", "Bb1", "Qb1", "Ab1", "Bb2", "Kb2", "Rb2"},
{"Pb1", "Pb2", "Pb3", "Pb4", "Pb5", "Pb6", "Pb7", "Pb8"},
{" ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " "},
{"Pw1", "Pw2", "Pw3", "Pw4", "Pw5", "Pw6", "Pw7", "Pw8"},
{"Rw1", "Kw1", "Bw1", "Qw1", "Aw1", "Bw2", "Kw2", "Rw2"},
};
public static void main(String []args){
Scanner scan = new Scanner(System.in);
System.out.print("Enter Row: ");
int row = scan.nextInt();
System.out.print("Enter Column: ");
int column = scan.nextInt();
System.out.println("Result : "+fullChessBoard[row][column]);
scan.close();
}
}