英文:
Why are my methods not returning anything?
问题
// 以下是已翻译的代码部分:
static char minimum(char array[])
{
char min = 'z';
int n = array.length;
for (int i = 0; i < n-1; i++)
{
if (array[i] < min)
{
min = array[i];
}
}
return min;
}
static int minimum(int array[])
{
Arrays.sort(array);
return array[0];
}
static double minimum(double array[])
{
int n = array.length;
Arrays.sort(array);
return array[0];
}
static char maximum(char array[])
{
char max = 'z';
int n = array.length;
for (int i = 0; i < n; i++)
if (array[i] > max)
{
max = array[i];
}
return max;
}
static int maximum(int array[])
{
int n = array.length;
Arrays.sort(array);
return array[n - 1];
}
static double maximum(double array[])
{
int n = array.length;
Arrays.sort(array);
return array[n - 1];
}
static int minimumat(char array[])
{
char min = 'z';
int n = array.length;
int minat = 0;
for (int i = 0; i < n-1; i++)
{
if (array[i] < min)
{
min = array[i];
}
}
for (int i = 0; i < n; i++)
{
if (array[i] == min)
{
i = minat;
}
}
return minat;
}
static int minimumat(int array[])
{
int n = array.length;
int newarray[] = array;
Arrays.sort(newarray);
int min = newarray[0];
int minat = 0;
for(int i = 0; i <= n; i++)
{
if(array[i] == min)
{
minat = i;
}
}
return minat;
}
static int minimumat(double array[])
{
int n = array.length;
double newarray[] = array;
Arrays.sort(newarray);
double min = newarray[0];
int minat = 0;
for(int i = 0; i <= n; i++)
{
if(array[i] == min)
{
minat = i;
}
}
return minat;
}
static int maximumat(char array[])
{
char max = 'A';
int n = array.length;
int maxat = 0;
for (int i = 0; i < n; i++)
{
if (array[i] > max)
{
max = array[i];
}
}
for (int i = 0; i < n; i++)
{
if (array[i] == max)
{
i = maxat;
}
}
return maxat;
}
static int maximumat(int array[])
{
int n = array.length;
int newarray[] = array;
Arrays.sort(newarray);
int max = newarray[0];
int maxat = 0;
for(int i = 0; i <= n; i++)
{
if(array[i] == max)
{
maxat = i;
}
}
return maxat;
}
static int maximumat(double array[])
{
int n = array.length;
double newarray[] = array;
Arrays.sort(newarray);
double max = newarray[n - 1];
int maxat = 0;
for(int i = 0; i <= n; i++)
{
if(array[i] == max)
{
maxat = i;
}
}
return maxat;
}
static double average(int array[])
{
int sum = 0;
int n = array.length;
for (int i = 0; i < array.length; i++)
{
sum += array[i];
}
double avg = sum / n;
return avg;
}
static double average(double array[])
{
double sum = 0;
int n = array.length;
for (int i = 0; i < array.length; i++)
{
sum += array[i];
}
double avg = sum / n;
return avg;
}
请注意,这段代码中可能存在一些逻辑错误,特别是在循环索引和数组访问方面。为了使代码更加准确和稳定,请您自行检查并调整这些部分。
英文:
I've written this code to do several calculations regarding arrays of characters, integers, and doubles.
static char minimum(char array[])
{
char min = 'z';
int n = array.length;
for (int i = 0; i < n-1; i++)
{
if (array[i] < min)
{
min = array[i];
}
}
return min;
}
static int minimum(int array[])
{
Arrays.sort(array);
return array[0];
}
static double minimum(double array[])
{
int n = array.length;
Arrays.sort(array);
return array[0
];
}
static char maximum(char array[])
{
char max = 'z';
int n = array.length;
for (int i = 0; i < n-1; i++)
if (array[i] > max)
{
max = array[i];
}
return max;
}
static int maximum(int array[])
{
int n = array.length;
Arrays.sort(array);
return array[n - 1];
}
static double maximum(double array[])
{
int n = array.length;
Arrays.sort(array);
return array[n - 1];
}
static int minimumat(char array[])
{
char min = 'z';
int n = array.length;
int minat = 0;
for (int i = 0; i < n-1; i++)
{
if (array[i] < min)
{
min = array[i];
}
}
for (int i = 0; i < n; i++)
{
if (array[i] == min)
{
i = minat;
}
}
return minat;
}
static int minimumat(int array[])
{
int n = array.length;
int newarray[] = array;
Arrays.sort(newarray);
int min = newarray[0];
int minat = 0;
for(int i = 0; i <= n; i++)
{
if(array[i] == min)
{
minat = i;
}
}
return minat;
}
static int minimumat(double array[])
{
int n = array.length;
double newarray[] = array;
Arrays.sort(newarray);
double min = newarray[0];
int minat = 0;
for(int i = 0; i <= n; i++)
{
if(array[i] == min)
{
minat = i;
}
}
return minat;
}
static int maximumat(char array[])
{
char max = 'A';
int n = array.length;
int maxat = 0;
for (int i = 0; i < n; i++)
{
if (array[i] > max)
{
max = array[i];
}
}
for (int i = 0; i < n; i++)
{
if (array[i] == max)
{
i = maxat;
}
}
return maxat;
}
static int maximumat(int array[])
{
int n = array.length;
int newarray[] = array;
Arrays.sort(newarray);
int max = newarray[0];
int maxat = 0;
for(int i = 0; i <= n; i++)
{
if(array[i] == max)
{
maxat = i;
}
}
return maxat;
}
static int maximumat(double array[])
{
int n = array.length;
double newarray[] = array;
Arrays.sort(newarray);
double max = newarray[n - 1];
int maxat = 0;
for(int i = 0; i <= n; i++)
{
if(array[i] == max)
{
maxat = i;
}
}
return maxat;
}
static double average(int array[])
{
int sum = 0;
int n = array.length;
for (int i = 0; i < array.length; i++)
{
sum += array[i];
}
double avg = sum / n;
return avg;
}
static double average(double array[])
{
double sum = 0;
int n = array.length;
for (int i = 0; i < array.length; i++)
{
sum += array[i];
}
double avg = sum / n;
return avg;
}
I have also written some simple code to test these methods in another class.
public static void main(String[] args)
{
char[] charArray = {'k', 'p', 'f', 'r', 'D', 'm'};
double doubleArray[] = {5.43, 9.81, 5.37, 7.19, 2.86, 6.55, 2.37, 7.28};
int intArray[] = {40, 7, 53, 34, 61, 70, 29, 49};
ArrayTools ArrayTools = new ArrayTools();
System.out.println(ArrayTools.minimum(charArray));
System.out.println(ArrayTools.minimum(doubleArray));
System.out.println(ArrayTools.minimum(intArray));
System.out.println(ArrayTools.maximum(charArray));
System.out.println(ArrayTools.maximum(doubleArray));
System.out.println(ArrayTools.maximum(intArray));
System.out.println(ArrayTools.minimumat(charArray));
System.out.println(ArrayTools.minimumat(doubleArray));
System.out.println(ArrayTools.minimumat(intArray));
System.out.println(ArrayTools.maximumat(charArray));
System.out.println(ArrayTools.maximumat(doubleArray));
System.out.println(ArrayTools.maximumat(intArray));
System.out.println(ArrayTools.average(intArray));
System.out.println(ArrayTools.average(doubleArray));
}
}
The code has worked properly for the "minimum" and "maximum" methods (not to be confused with the "minimumat" and "maximumat" methods). But the rest haven't printed anything.
Here is the output:
D
2.37
7
z
9.81
70
答案1
得分: 1
我在你的代码中看到的两个问题是:
1 - 在static int minimumat(char array[])
方法中,你重置了变量i的值。应该是
minat = i;
类似地,在另一个方法maximumat()
中也要对maxat
进行相应修改。
2 - 你的循环从0到长度(n)进行,因此在最后一个索引可能会导致数组越界。所以应该排除最后一个索引。应该是
for(int i = 0; i < n - 1; i++) {
英文:
2 problems I see in your code is-
1 - In static int minimumat(char array[])
method you are resetting value of i. It should be
minat = i;
and similarly maxat in other method- maximumat()
2- You are looping from 0 to length (n), so you might get ArrayIndexOutOfBound on last index, so exclude the last one. It should be
for(int i = 0; i < n; i++) {
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论