所以我正在制作一个从用户输入的值中获取数组的程序,但我在编写查找数组模式的代码时遇到了问题。我试着写自己的代码,然后试着使用别人的代码版本,但没有成功,因为老实说,我没有完全理解。
import java.util.Scanner;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int length;
Statistics stats = new Statistics();
System.out.println("Welcome to our statistics program!");
System.out.print("Enter the amount of numbers you want to store: ");
length=Integer.parseInt(keyboard.next());
int[] nums = new int[length];
for(int i=0; i<length; i++) {
System.out.println("Enter a number: ");
nums[i]=keyboard.nextInt();
}
System.out.println("Array elements are: ");
for (int i=0; i<length; i++) {
System.out.println(nums[i]);
}
public int Mode (int[] nums) {
double maxValue = -1.0d;
int maxCount = 0;
for(int i = 0; i < data.length; i++) {
double currentValue = data[i];
int currentCount = 1;
for(int j = i + 1; j < data.length; ++j) {
if(Math.abs(data[j] - currentValue) < epsilon) {
++currentCount;
}
}
}
if (currentCount > maxCount) {
maxCount = currentCount;
maxValue = currentValue;
} else if (currentCount == maxCount) {
maxValue = Double.NaN;
}
}
System.out.println("The minimum number is " + stats.Mode(nums));
您可以考虑使用HashMap来维护循环中数组中值的频率:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
public static int getIntegerInput(String prompt, Scanner scanner) {
System.out.print(prompt);
int validInteger = -1;
while (scanner.hasNext()) {
if (scanner.hasNextInt()) {
validInteger = scanner.nextInt();
break;
} else {
System.out.println("Error: Invalid input, try again...");
System.out.print(prompt);
scanner.next();
}
}
return validInteger;
}
public static int getPositiveIntegerInput(String prompt, Scanner scanner) {
int num = getIntegerInput(prompt, scanner);
while (num <= 0) {
System.out.println("Error: Integer must be positive.");
num = getIntegerInput(prompt, scanner);
}
return num;
}
public static int getMode(int[] nums) {
if (nums.length == 0) {
throw new IllegalArgumentException("nums cannot be empty");
}
Map<Integer, Integer> valueFrequencies = new HashMap<>();
valueFrequencies.put(nums[0], 1);
int maxFreq = 1;
int candidateMode = nums[0];
for (int i = 1; i < nums.length; i++) {
int value = nums[i];
valueFrequencies.merge(value, 1, Integer::sum);
int candidateFreq = valueFrequencies.get(value);
if (candidateFreq > maxFreq) {
candidateMode = value;
maxFreq = candidateFreq;
}
}
return candidateMode;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int numsLength = getPositiveIntegerInput("Enter how many numbers you want to store: ", scanner);
int[] nums = new int[numsLength];
for (int i = 0; i < numsLength; i++) {
nums[i] = getIntegerInput(String.format("Enter number %d: ", i + 1), scanner);
}
int mode = getMode(nums);
System.out.printf("Mode: %d%n", mode);
}
}
示例用法:
Enter how many numbers you want to store: 0
Error: Integer must be positive.
Enter how many numbers you want to store: 6
Enter number 1: 3
Enter number 2: 2
Enter number 3: 5
Enter number 4: 5
Enter number 5: 3
Enter number 6: 3
Mode: 3