简体   繁体   English

尝试调试Java代码

[英]Trying to Debug Java Code

I guess I'm not getting arrays because somehow I managed yet again to break a perfectly good program. 我猜我没有得到数组,因为我以某种方式再次设法打破了一个完美的程序。

It was working earlier but then I did something and it stopped working. 它早些时候工作了,但是后来我做了些事情,但是它停止了工作。 I would like it back to the way it was. 我想回到原来的样子。

Code in main: 主要代码:

public class Lab14d

 {

  public static void main( String args[] )
{

    int r = 5;
    int e = 3;
    int k = 9;

    double[]g ={100,90,85,72.5,95.6};
    double[]c ={50.0,100.0,80.0};
    double[]a ={93.4,-90.0,90.0};
    double[]d ={1,2,3,4,5,6,7,8,9};

    Grades w = new Grades();


    w.SortArray(g);
    w.getSum(g);
    w.getAverage(r);
    System.out.println(w);

    w.SortArray(c);
    w.getSum(c);
    w.getAverage(e);
    System.out.println(w);

    w.SortArray(a);
    w.getSum(a);
    w.getAverage(e);
    System.out.println(w);

    w.SortArray(d);
    w.getSum(d);
    w.getAverage(k);
    System.out.println(w);


}
}     

code in other file 其他文件中的代码

public class Grades

  {

    int x = 0;
    public Grades()
    {
     x = 0;
     }
public static SortArray(double[] array)
{
    Arrays.sort(array);
    for (int i = 0; i < array.length; i++)
    {
        return "Grade " + i + " :: " + "   " + array[i];
    }

}
public double getSum(double[] array )
{
    double sum=0.0;
    for(int spot = 0; spot <= array.length; spot++)
    {
        sum = sum + array[spot];
    }

    return sum;
}

public double getAverage(int x)
{
    double average=0.0;
    average = getSum()/2;
    return average;
}
public String toString()
{
    return "Average = " + getAverage();
}
 }

out put i want 我想要的

grade 0 ::  100.00
grade 1 ::   90.00
grade 2 ::   85.00
grade 3 ::   72.50
grade 4 ::   95.60

average = 88.62


grade 0 ::   50.00
grade 1 ::  100.00
grade 2 ::   80.00

average = 76.67


grade 0 ::   93.40
grade 1 ::  -90.00
grade 2 ::   90.00

average = 31.13


grade 0 ::    1.00
grade 1 ::    2.00
grade 2 ::    3.00
grade 3 ::    4.00
grade 4 ::    5.00
grade 5 ::    6.00
grade 6 ::    7.00
grade 7 ::    8.00
grade 8 ::    9.00

average = 5.00

I appreciate any help given. 我感谢您提供的任何帮助。

your line of code average = getSum()/2; 您的average = getSum()/2;代码行average = getSum()/2; should be giving a compile time error ,we cannot see that method in the file.Same is the case with return "Average = " + getAverage(); 应该给出编译时错误,我们无法在文件中看到该方法。 return "Average = " + getAverage();

The main problem is you have defined your methods to accept parameters,but in these places you are calling them without those parameters. 主要问题是您已经定义了接受参数的方法,但是在这些地方,您调用的是没有这些参数的方法。

I really doubt how this code is supposed to be working, public static SortArray(double[] array) no RETURN type specified! 我真的怀疑这段代码应该如何工作, public static SortArray(double[] array)没有指定RETURN类型!

your methods just returning the result, but you are not assigning it to any variable or not printing what you get as result!! 您的方法只是返回结果,但是您没有将其分配给任何变量或未打印结果。 instead of that you are trying to print the object itself! 而不是尝试打印对象本身! the return statement will not print to the console.. it will just pass the result to where it is called/ to where we assigned it to .. my suggesion is: return语句不会打印到控制台上。它只会将结果传递到调用它的地方/我们分配给它的地方。我的建议是:

1: change the returning statemnts to print statements(in all methods if you wish to do there itself) 1:将返回的statemnts更改为print语句(如果您希望自己做,可以使用所有方法)

return "Grade " + i + " :: " + "   " + array[i];

to

System.out.println("Grade " + i + " :: " + "   " + array[i]);

I think it is better to print from there itself, because in the above statement you need to print the result from the loop itself, it is better than storing then passing and printing them anyway ... 我认为最好从那里开始打印,因为在上面的语句中您需要从循环本身打印结果,这比存储然后传递并打印它们要好。

Your code is full of problems, you are returning wrong values, have unused variables (x), no return type specified etc. Below is the corrected version. 您的代码充满问题,返回错误的值,具有未使用的变量(x),未指定返回类型等。下面是更正的版本。 I replaced double[] with Double[] (objects instead of primitives) such that I can sort them using Arrays.Sort in descending order (as you asked). 我将Double []替换为Double [](对象而不是基元),以便可以使用Arrays.Sort按降序对它们进行排序(根据您的要求)。 Arrays.Sort with only work like this with objects and not primitives. Arrays.Sort仅与对象一起使用,而不与图元一起使用。 The code produces the output you want. 该代码产生所需的输出。

Main class: 主班:

public class Lab14d {
    public static void main(String[] args) {
        Double[] g = { 100.00d, 90.00d, 85.00d, 72.5, 95.6 };
        Double[] c = { 50.0, 100.0, 80.0 };
        Double[] a = { 93.4, -90.0, 90.0 };
        Double[] d = { 1.00d, 2.00d, 3.00d, 4.00d, 5.00d, 6.00d, 7.00d, 8.00d, 9.00d };

        Grades.SortArray(g);
        System.out.println("Average = " + String.format("%.2f", Grades.GetAverage(g)));
        System.out.println();

        Grades.SortArray(c);
        System.out.println("Average = " + String.format("%.2f", Grades.GetAverage(c)));
        System.out.println();

        Grades.SortArray(a);
        System.out.println("Average = " + String.format("%.2f", Grades.GetAverage(a)));
        System.out.println();

        Grades.SortArray(d);
        System.out.println("Average = " +String.format("%.2f", Grades.GetAverage(d)));
        System.out.println();
    }
}

Grades class: 年级:

import java.util.Arrays;
import java.util.Collections;

public class Grades {

    public static void SortArray(Double[] array) {
        Arrays.sort(array, Collections.reverseOrder());
        for (int i = 0; i < array.length; i++) {
            System.out.println("Grade " + i + " :: " + array[i]);
        }
    }

    private static double GetSum(Double[] array) {
        double sum = 0.00d;
        for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }
        return sum;
    }

    public static double GetAverage(Double[] array) {
        double total = GetSum(array);
        return total / array.length;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM