Java:在未排序的整数数组中搜索用户输入的整数的第一个匹配项
我注意到两个问题,非常感谢您的帮助Java:在未排序的整数数组中搜索用户输入的整数的第一个匹配项,java,arrays,loops,methods,java.util.scanner,Java,Arrays,Loops,Methods,Java.util.scanner,我注意到两个问题,非常感谢您的帮助 (我相信)方法“arrayIndex”和“position”是如何相互作用的。我这样说是因为当我为两个字段输入相同的精确数字时,它们返回相同的精确值,即使它们应该处理完全不同的任务 我的“position”方法应该返回数组中第一次出现的搜索值的位置,但它似乎返回了不正确的值,检查后我仍然无法找出原因。如果用户搜索不存在的值位置,则应返回“-1” 我的全部代码: import java.util.Scanner; public class Prog9Array
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 count = arrayIndex(tmax);
int i = arrayIndex(tmax);
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: " + count);
System.out.println("The first occurence of the searched value is: " + i);
}
// 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;
}
// Returns the average value in the array
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;
}
// Returns the number of values greater than the user's indexed values
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 count = 0;
for(int i = 0; i < a.length; i++) {
if(a[i] > userSearch) {
++count;
}
}
return count;
}
public static int position(int [ ] a, int match) {
Scanner user_input = new Scanner( System.in );
System.out.println("Enter a value to search: ");
int userSearch = user_input.nextInt();
for (int i = 0; i < a.length; i++)
{
if ( a[i] == userSearch )
return i;
}
return -1;
}
}
import java.util.Scanner;
公共类Prog9ArrayMethods{
公共静态void main(字符串[]args){
//缅因州波特兰市2015年1月1日至12月31日的每日高温
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=阵列最大值(tmax);
int min=arrayMin(tmax);
双倍平均值=阵列平均值(tmax);
整数计数=阵列索引(tmax);
int i=阵列索引(tmax);
System.out.println(“最大值为:“+max”);
System.out.println(“最小值为:“+min”);
System.out.println(“平均值为:“+平均值”);
System.out.println(“高于指定值的值的数量为:“+count”);
System.out.println(“搜索值的第一次出现是:“+i”);
}
//返回数组中的最大值
公共静态int数组最大值(int[]a){
int max=a[0];
for(int i=0;imax)
max=a[i];
返回最大值;
}
//返回数组中的最小值
公共静态int-arrayMin(int[]a){
int min=a[0];
for(int i=0;iuserSearch){
++计数;
}
}
返回计数;
}
公共静态int位置(int[]a,int匹配){
扫描仪用户输入=新扫描仪(System.in);
System.out.println(“输入要搜索的值:”);
int userSearch=user_input.nextInt();
for(int i=0;i
在main方法中,当您希望得到i
作为计数时,实际上得到的是arrayIndex()
而不是position()
。
只需更改
inti=arrayIndex(tmax)代码>至int i=位置(tmax)
并去掉int match()
作为position()
的参数,以下是问题的解决方案。您正在两次调用arrayIndex方法,而不是调用position方法。我修正了你的密码。现在它工作得很好
import java.util.Scanner;
公共类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 count = arrayIndex(tmax);
int i = position(tmax);
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: " + count);
System.out.println("The first occurence of the searched value is: " + i);
}
// 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;
}
// Returns the average value in the array
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;
}
// Returns the number of values greater than the user's indexed values
public static int arrayIndex(int[] a) {
Scanner user_input = new Scanner( System.in );
System.out.println("Enter a value to search for arrayIndex: ");
int userSearch = user_input.nextInt();
int count = 0;
for(int i = 0; i < a.length; i++) {
if(a[i] > userSearch) {
++count;
}
}
return count;
}
public static int position(int [ ] a) {
Scanner user_input = new Scanner( System.in );
System.out.println("Enter a value to search: ");
int userSearch = user_input.nextInt();
for (int i = 0; i < a.length; i++)
{
if ( a[i] == userSearch )
return i;
}
return -1;
}
publicstaticvoidmain(字符串[]args){
//缅因州波特兰的每日高温