【发布时间】:2021-01-29 06:11:59
【问题描述】:
对于这个编程任务,我们应该在这个数组中为数独游戏的一行找到重复项的索引。我有这个方法,static boolean equals(int[] a, int[] a2):
boolean isValue = true;
int[] dataRow = { 9, 8, 7, 6, 5, 4, 3, 2, 8 };
int[] chk = { 9, 8, 7, 6, 5, 4, 3, 1, 8 };
isValue = Arrays.equals(dataRow, chk);
int i, j;
for (i = 0; i < dataRow.length; i++)
{
for (j = 0; j < dataRow.length; j++)
{
if (dataRow[i] == dataRow[j])
{
System.out.println("Duplicate - " + dataRow[i] + " found at index " + i + " and " + j);
//--8 found at index 8 and 1
这个程序在这里简单地打印出这个:Duplicate - 9 found at index 0 and 0,这意味着没有找到重复项。我评论说在索引 8 和 1 中找到了 8。我只是不确定如何打印出找到重复项的位置。我尝试修改 if 语句,但没有奏效:if (dataRow[i] != chk[j])、if (dataRow[i] == chk[i])。我还尝试将嵌套的 for 循环放入 while 循环:while (!isValue),但这也不起作用。我想我的教授也想确保数组中的所有值都在 1-9 之间,我的想法是这样的:while (!isValue && (dataRow >= 1 && dataRow <= 9)),但我不确定这是否可行。感谢你们能给我的任何帮助。
【问题讨论】:
标签: java arrays for-loop if-statement while-loop