gpt4 book ai didi

java - 计算已完成的数独板的行数和列数

转载 作者:行者123 更新时间:2023-12-01 15:33:13 25 4
gpt4 key购买 nike

所以我试图计算完成的数独板的输入文件有多少行和列。我想出了这个循环

public static Pair<Integer, Integer> rowColumnCount(Scanner input1){
int nrows=0;
int ncolumns=0;


while(input1.hasNextLine()){

while(input1.hasNextInt()){
input1.nextInt();
ncolumns++;
}

input1.nextLine();
nrows++;
}

System.out.print("Rows: " +nrows + "\n");
System.out.print("Columns: " +ncolumns + "\n");

return new Pair<Integer, Integer>(nrows, ncolumns);

}//end rowCoulmnCount

我从以前的方法中得到了制作此方法的想法,在该方法中我读取并创建了数独板输入文件的数组。我认为这两种方法是相似的,但它们不是.. nrws 和 ncolumns 的输出是 1 & 81。所以有人可以帮助我找出正确的方法来计算列数以确保有 9 。或者会吗最好开始比较行和列的值,看看是否有任何重复项(1-9),使用错误检查来查看是否有正确的行数和列数?

最佳答案

尝试:

public static Pair<Integer, Integer> rowColumnCount(Scanner input1){
int nrows=0;
int ncolumns=0;


while(input1.hasNextLine()){
Scanner subinput1 = new Scanner(input1.nextLine());
while(subinput1.hasNextInt()){
subinput1.nextInt();
ncolumns++;
}

nrows++;
}

System.out.print("Rows: " +nrows + "\n");
System.out.print("Columns: " +ncolumns + "\n");

return new Pair<Integer, Integer>(nrows, ncolumns);

}//end rowCoulmnCount

关于java - 计算已完成的数独板的行数和列数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9308937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com