我的代码中有什么问题吗? [英] Is there something wrong in my code?

查看:91
本文介绍了我的代码中有什么问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须单独添加每个列,我已经完成了,但我的代码中出现了问题,而不是添加列只显示一次,它会多次显示答案并且它会增加多少列添加。我如何更正?



我将不胜感激任何提示

谢谢



我尝试过的事情:



i have to add each column separately only, i have done it, but something is wrong in my code, instead of adding the columns to display it only once, it displays the answer multiple times and it adds up how much columns was added. How do i correct this?

I would appreciate any tips
Thank you

What I have tried:

public class Vetting{
    
   Scanner input = new Scanner(System.in);
   String TeacherName;
   int RatesArray[][] = new int [5][5];
   
    int Assessment1,Assessment2,Assessment3,Assessment4,Assessment5;
   public int [][] RateArray()
   {
       System.out.println("Enter Assessment marks");
       for(int row =0; row<RatesArray.length;row++)
       {
           for(int col = 0; col<RatesArray.length;col++)
           {
               RatesArray[row][col] = input.nextInt();
           }
           
           
       }
       return RatesArray;
   }
   //Displaying output of the table array 
   public void OutputArray()
       {
           RateArray();
           System.out.println("The rates are: ");
           System.out.print("\t\t\t Assessment 1, Assessment 2,  Assesment 3,  Assessment 4 ,  Assessment 5");
                System.out.println(" ");

                for(int i = 0; i < RatesArray.length; i++)
              {
                System.out.print( "Student " + (i+1) +": \t");

                for(int j = 0; j < RatesArray.length; j++)
              {
                System.out.print("\t\t"+ RatesArray[i][j]);
              }
                System.out.println("\n");

       }
           System.out.println(getTotal() + ",");
           System.out.println("The last number in the total array is: " + getMin());
           System.out.println("Good job on the high score of: " + getMax());
           System.out.println("You could improve on the score of: " + getMin());
           
       }
   
    //Adding the sum of columns only
    public int getTotal()
    {  
        int column= RatesArray.length;
    
        int columnsum [] = new int [column];
        
        for (int i = 0; i < RatesArray.length; i++)
        {
                for (int j = 0; j < RatesArray.length; j++)
            {
                columnsum[i] += RatesArray[j][i];
            }
        }
        System.out.println(Arrays.toString(columnsum));
        return column;
    }
    //Get largest value from getTotal
    public int getMax()
    {  
        int Largest = 0;

         for (int i = 1; i < getTotal(); i++)
            {
                if (Largest < getTotal())
                {
                    Largest = getTotal();
                }
            }
        
        return Largest;
    }
    //Get lowest value from getTotal
    public int getMin()
    {
        int Lowest = 0;

         for (int i = 1; i > getTotal(); i++)
            {
                if (Lowest > getTotal())
                        
                {
                    Lowest = getTotal();
                }
            }
        
        return Lowest;
    }

推荐答案

我没有编译和执行你的程序,但有一些显眼的界限:



getTotal()中你正在反转数组索引:

I have not compiled and executed your program but there are some conspicuous lines:

In getTotal() you are reversing the array index:
columnsum[i] += RatesArray[j][i];

不应该是 [i] [j]



getMax()中,您要与 getTotal()返回的错误(固定)值进行比较也会多次打印相同的输出:

Shouldn't that be [i][j]?

In getMax() you are comparing with the wrong (fixed) value returned by getTotal() which is also printing the same output multiple times:

if (Largest < getTotal())
{
    Largest = getTotal();
}

你必须在所有数组条目上再次迭代以找到最大值并打印而不是调用 getTotal()



类似于 getMin()

You have to iterate again over all array entries to find the maximum and print that rather than calling getTotal().

Similar for getMin().


在GetTotal中,您的数组索引相反:



In GetTotal, your array indices are reversed:

columnsum[i] += RatesArray[j][i];



应该是:


Should be:

columnsum[i] += RatesArray[i][j];





另一个问题,现在隐藏了,因为数组恰好是5x5,在内部循环中:





Another problem, which is now hidden because the array happens to be 5x5, is in the inner loops:

for (int i = 0; i < RatesArray.length; i++)
{
        for (int j = 0; j < RatesArray.length; j++)
    {
        columnsum[i] += RatesArray[j][i];
    }
}



应该是:


Should be:

for (int i = 0; i < RatesArray.length; i++)
{
        for (int j = 0; j < RatesArray[i].length; j++)
    {
        columnsum[i] += RatesArray[i][j];
    }
}


这篇关于我的代码中有什么问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆