gpt4 book ai didi

java - 为什么我的循环在计算最后一个数字之前就跳出了?

转载 作者:行者123 更新时间:2023-12-02 10:27:47 26 4
gpt4 key购买 nike

该程序的任务是获取 1 到 100 之间的整数并计算它们出现的次数。

例如用户输入的数字是 1 2 2 3 3 25 25 67 98 99 99。输出将是:

1 occurs 1 time
2 occurs 2 times
3 occurs 2 times
25 occurs 2 times
67 occurs 1 time
98 occurs 1 time

问题

虽然我的程序可以成功计算大多数数字,但它无法计算两个 99,如果我用 98 替换倒数第二个 99,那么输出将是 98 出现 2 次,但不会计算在内99.

我已经尝试过调试这个东西,我不应该说它不计算数组中的最后一个或多个数字,因为它会计算,但它会在移动到 else-if 语句之前跳出。

它似乎跳出来,因为第一个 if 语句是 true,因此不需要转到 else 并且它已完成循环,但弄清楚如何使其仍然打印最后一个数字让我感到困惑。

这是代码。问题出在 LinearSearch() 方法中:

import java.util.Scanner;

public class Lab8 {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int usernumbers[] = new int[20];
int size = usernumbers.length;

System.out.println("Enter integers between 1 and 100, " + "enter a 0 to end ");

// user populates the array usernumbers
int i = 0;
do {
usernumbers[i] = input.nextInt();
} while (usernumbers[i++] != 0);

// invokes the method to sort the array
arraySort(usernumbers);

System.out.println(java.util.Arrays.toString(usernumbers));

// this invokes the method to search the array for the numbers
linearSearch(usernumbers);

input.close();
}

public static void arraySort(int[] usernumbers) {
// bubblesort for usernumbers[]
int a = usernumbers.length;
for (int i = 0; i < a - 1; i++) {
for (int j = 0; j < a - i - 1; j++) {

if (usernumbers[j] > usernumbers[j + 1]) {
int temp = usernumbers[j];
usernumbers[j] = usernumbers[j + 1];
usernumbers[j + 1] = temp;
}
}
}
}

public static void linearSearch(int[] usernumbers) {
int compare = usernumbers[0];
int count = 0;

for (int i = 0; i < usernumbers.length; i++) {
if (compare == usernumbers[i]) {
count++;
} else {
if (count > 1 && compare != 0) {
System.out.println(compare + " occurs " + count + " times");
}

if (count == 1 && compare != 0) {
System.out.println(compare + " occurs " + count + " time");
}

count = 1;
compare = usernumbers[i];
}
}
}
}

最佳答案

问题是您错过了最后一个数字的迭代。

public class Job8 {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int usernumbers[] = new int[20];
int size = usernumbers.length;

System.out.println("Enter integers between 1 and 100, " + "enter a 0 to end ");

// user populates the array usernumbers
int i = 0;
do {
usernumbers[i] = input.nextInt();
} while (usernumbers[i++] != 0);

// invokes the method to sort the array
arraySort(usernumbers);

System.out.println(java.util.Arrays.toString(usernumbers));

// this invokes the method to search the array for the numbers
linearSearch(usernumbers);

input.close();
}

public static void arraySort(int[] usernumbers) {
// bubblesort for usernumbers[]
int a = usernumbers.length;
for (int i = 0; i < a - 1; i++) {
for (int j = 0; j < a - i - 1; j++) {

if (usernumbers[j] > usernumbers[j + 1]) {
int temp = usernumbers[j];
usernumbers[j] = usernumbers[j + 1];
usernumbers[j + 1] = temp;
}
}
}
}

public static void linearSearch(int[] usernumbers) {
int count = 0;
int currentNumber = 0;
for (int i = 0; i < usernumbers.length; i++) {
int nextIndex = i + 1;
currentNumber = usernumbers[i];

if (currentNumber == 0) {
continue;
}

if (nextIndex < usernumbers.length) {
int nextNumber = usernumbers[nextIndex];
if (currentNumber == nextNumber) {
count++;
continue;
} else {
if (count == 0) {
System.out.println(currentNumber + " occurs once");
} else {
System.out.println(currentNumber + " occurs " + count + " times");
count = 0;
}
}
}
}

System.out.println(+currentNumber + " occurs " + (count + 1) + " times");
}
}

关于java - 为什么我的循环在计算最后一个数字之前就跳出了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53798170/

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