我的方法为什么没有返回任何内容?

huangapple go评论67阅读模式
英文:

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 = &#39;z&#39;;
int n = array.length;
for (int i = 0; i &lt; n-1; i++)
{
if (array[i] &lt; 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 = &#39;z&#39;;
int n = array.length;
for (int i = 0; i &lt; n-1; i++)
if (array[i] &gt; 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 = &#39;z&#39;;
int n = array.length;
int minat = 0;
for (int i = 0; i &lt; n-1; i++)
{
if (array[i] &lt; min)
{
min = array[i];
}
}
for (int i = 0; i &lt; 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 &lt;= 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 &lt;= n; i++)
{
if(array[i] == min)
{
minat = i;
}
}
return minat;
}
static int maximumat(char array[])
{
char max = &#39;A&#39;;
int n = array.length;
int maxat = 0;
for (int i = 0; i &lt; n; i++)
{
if (array[i] &gt; max)
{
max = array[i];
}
}
for (int i = 0; i &lt; 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 &lt;= 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 &lt;= 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 &lt; 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 &lt; 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 = {&#39;k&#39;, &#39;p&#39;, &#39;f&#39;, &#39;r&#39;, &#39;D&#39;, &#39;m&#39;};
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 &lt; n; i++) {

huangapple
  • 本文由 发表于 2020年9月12日 23:10:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63861769.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定