gpt4 book ai didi

java - 在另一个 for 循环中使用数组索引值

转载 作者:行者123 更新时间:2023-12-01 23:36:24 26 4
gpt4 key购买 nike

我正在尝试计算数组中存在唯一数字的次数,使用的索引数量取决于输入的元素数量。除了 1 之外,它主要是可操作的。第一个值没有被考虑在内。循环正在检查 arrays.length -1 ,因此即使我输入,数字 3 也显示为 1 的计数3 两次。我知道解决此问题的最佳方法是运行一个不使用 arrays.length -1 的循环,但是这样我就无法将一个条目与其旁边的条目进行比较,例如 if(a[i] == a[i + 1] && a[i] != 0) 查看某个值是否多次出现。我认为最好的办法是将计数值及其相应的已计数数组值存储在我的 count 方法中,然后在该方法之外执行 for 循环,这可能吗?我看不到一种方法,因为我对 java 相当陌生。我可以有一些指导吗:)

import java.util.Scanner;


public class Prac_ExeOne
{
static int count = 1;
static int numberUsed = 0; // static its used to show its a class wide variable and there is only one copy.
static int[] Array = new int [50]; // the maximum elements inside the array that can be used is 10;
int size;

public int fillArray(int[] a)
{
System.out.println("Enter up to " + a.length + " nonnegative numbers.");
System.out.println("Mark the end of the list with a negative number.");
Scanner keyboard = new Scanner(System.in);

int next;
int index = 0;
next = keyboard.nextInt();
while ((next >= 0) && (index < a.length ))
{
numberUsed++;
a[index] = next;
index++;
// Print out each value of next
System.out.println(next);
next = keyboard.nextInt();
//System.out.println("Number of indexes used" + numberUsed);
}
keyboard.close(); // close the keyboard so it can't continue to be used.
return index;
}


public int[] sort(int[] arrays)
{

for(int i = 0;i < arrays.length -1 ;i++ )
{

int store = 0;
// Move Larger Values to the right.
if (arrays[i + 1 ] < arrays[i])
{
store = arrays[i];
arrays[i] = arrays[i + 1];
arrays[i + 1] = store;
}
// Sort swapped smaller values to the left.
for(int j = i; j > 1; j--)
{
if (arrays[j] < arrays[j - 1])
{
store = arrays[j];
arrays[j] = arrays[j - 1];
arrays[j - 1] = store;
}
}
}
return arrays;

}
public void count(int[] a)
{

//for each element in array go through if conditons.
System.out.println("N " + "Count");
for(int i = 0;i < a.length -1;i++)
{
if(a[i] == a[i + 1] && a[i] != 0)
{
count++;

}
if(a[i] != a[i+1])
{
count = 1;
}
if (a[i] != 0)
{
System.out.println(a[i] + " " + count);
}
}
}

public static void main(String[] args)
{
Prac_ExeOne score = new Prac_ExeOne();
score.fillArray(Array);
score.sort(Array);
score.count(Array);


}
}

输入:

Enter up to 50 nonnegative numbers.
Mark the end of the list with a negative number.
3
3
2
2
-2

输出:

N Count
3 1
2 2
2 1

期望的结果:

简而言之,我希望程序正确计算值,然后在 N 下方左侧显示该值,并在 Count 右侧下方显示其在数组中的次数

最佳答案

对于排序函数

for (int j = i; j > 1; j--)

应该是

for (int j = i; j > 0; j--)

我把System.out.println(Arrays.toString(Array));放在调用排序之后,并在开头看到一个3,这让我事实上您正在跳过第一个元素。

请注意,还有 way more efficient sorting algorithms .

对于 count 函数,您在错误的时间重置了 count 并且打印过于频繁。我修改如下:

public void count(int[] a)
{
//for each element in array go through if conditions.
System.out.println("N " + "Count");
for (int i = 0; i < a.length - 1; i++)
{
if (a[i] != 0)
{
if (a[i] == a[i + 1])
{
count++;
}
// if the next element is different, we already counted all of the
// current element, so print it, then reset the count
else
{
System.out.println(a[i] + " " + count);
count = 1;
}
}
}
// we haven't processed the last element yet, so do that
if (a[a.length-1] != 0)
System.out.println(a[a.length-1] + " " + count);
}

关于java - 在另一个 for 循环中使用数组索引值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18616229/

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