gpt4 book ai didi

Java:搜索未排序的整数数组并返回有多少个数字更大

转载 作者:行者123 更新时间:2023-12-01 18:00:53 25 4
gpt4 key购买 nike

我一直在尝试创建一种方法来搜索未排序的整数数组,但遇到了一些我无法解决的问题。我想允许程序的用户输入一个整数值,并让程序返回大于搜索到的整数值的数字数量。我似乎遇到的问题是处理主方法内的变量,使用扫描仪进行搜索,以及如何修复这次运行的循环。

我非常感谢您的帮助!

我的代码:

import java.util.Scanner;

public class Prog9ArrayMethods {

public static void main(String[] args) {
// Daily high temperatures for Portland Maine Jan 1 - Dec 31 2015
int[] tmax = {32, 38, 34, 35, 41, 17, 25, 17, 29, 24, 26, 33, 31, 24,
29, 38, 20, 49, 49, 36, 31, 38, 35, 32, 37, 20, 17, 26,
30, 32, 22, 26, 12, 20, 35, 34, 19, 28, 22, 15, 30, 23,
20, 17, 16, 19, 21, 21, 32, 33, 19, 34, 35, 31, 19, 34,
21, 27, 27, 30, 36, 32, 46, 39, 23, 38, 40, 44, 47, 56,
41, 39, 38, 36, 45, 44, 28, 32, 34, 36, 35, 34, 39, 42,
49, 49, 41, 41, 40, 48, 45, 46, 66, 49, 48, 41, 47, 42,
35, 43, 54, 68, 66, 70, 65, 55, 67, 55, 57, 48, 63, 60,
53, 54, 55, 56, 58, 63, 57, 60, 55, 54, 62, 76, 75, 72,
84, 58, 59, 83, 68, 82, 64, 68, 70, 63, 74, 61, 65, 67,
69, 67, 65, 83, 84, 91, 79, 80, 77, 84, 73, 51, 50, 61,
60, 58, 73, 67, 65, 68, 81, 86, 80, 85, 78, 61, 61, 75,
72, 80, 69, 72, 72, 67, 82, 78, 67, 70, 59, 69, 75, 68,
78, 80, 71, 82, 82, 76, 84, 72, 84, 87, 90, 78, 76, 82,
76, 74, 70, 81, 84, 70, 82, 78, 76, 67, 67, 77, 83, 88,
86, 86, 86, 81, 81, 80, 82, 80, 76, 80, 77, 77, 67, 80,
77, 80, 85, 85, 89, 86, 83, 75, 73, 78, 70, 79, 75, 80,
79, 77, 75, 81, 86, 80, 84, 86, 72, 78, 82, 92, 89, 86,
78, 73, 74, 62, 73, 83, 85, 82, 83, 75, 72, 69, 65, 74,
74, 63, 63, 67, 74, 75, 69, 62, 55, 58, 58, 61, 69, 67,
63, 59, 56, 68, 70, 62, 68, 57, 61, 57, 46, 48, 66, 58,
65, 54, 47, 62, 54, 52, 59, 73, 58, 51, 58, 64, 64, 64,
68, 69, 65, 53, 58, 53, 47, 53, 60, 46, 53, 54, 47, 47,
53, 59, 46, 42, 42, 42, 41, 51, 61, 57, 41, 32, 38, 44,
45, 47, 51, 51, 57, 39, 45, 53, 48, 57, 47, 48, 56, 42,
50, 46, 40, 38, 47, 49, 47, 51, 62, 51, 43, 34, 23, 28,
44};

int max = arrayMax(tmax);
int min = arrayMin(tmax);
double average = arrayAverage(tmax);
int search = arrayIndex(search, tmax); // Not exactly sure how to fix this
System.out.println("Maximum value is: " + max);
System.out.println("Minimum value is: " + min);
System.out.println("Average value is: " + average);
System.out.println("The number of values above the specified value is: " + search);
}

// Returns the maximum value in the array
public static int arrayMax(int[] a) {
int max = a[0];

for (int i = 0; i < a.length; i++)
if (a[i] > max)
max = a[i];

return max;
}
// Returns the minimum value in the array
public static int arrayMin (int[] a) {
int min = a[0];

for (int i = 0; i < a.length; i++)
if (a[i] < min)
min = a[i];
return min;
}
public static double arrayAverage(int[] a) {
int sum = 0; // Why does it double the decimal value with "int sum = a[0]"?
double average;

for(int i=0; i < a.length; i++){
sum = sum + a[i];
}
average = (double)sum/a.length;
return average;
}
public static int arrayIndex(int userSearch, int[] a) {
Scanner user_input = new Scanner( System.in );
System.out.println("Enter a value to search: ");
userSearch = user_input.next(); // What else could I use?

for(int i = 0; i < a.length; i++) {
if(a[i] > userSearch) {
return a[i]; // Return a value that represents how many numbers are above the search varaible
}
}
return -1;
}
}

最佳答案

我必须重写你的 arrayIndex 方法。在该方法内获取用户的输入,或者您可以将输入传递给 arrayIndex 方法。

只计算大于输入的数字,然后返回结果。

public static int arrayIndex(int[] a) {

Scanner user_input = new Scanner( System.in );
System.out.println("Enter a value to search: ");
int userSearch = user_input.nextInt();

int numberOfCount = 0;

for(int i = 0; i < a.length; i++) {
if(a[i] > userSearch) {
++numberOfCount;
}
}
return numberOfCount;
}

关于Java:搜索未排序的整数数组并返回有多少个数字更大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41071787/

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