在第二个循环中不正确的数组计算。

时间:2022-09-01 05:37:45

When I do the calculations in the second for loop it does not calculate properly. I will only calculate the last number in the array and not the other two. The if block calculation are correct. What I need to do then is to take each [row][col] value and divide that by temp3 and display it in the array. Please advice. Thank you in advance. Avi

当我在第二个for循环中计算时,它的计算不正确。我只计算数组中的最后一个数字,而不是另外两个。如果块计算是正确的。我需要做的是,将每个[行][col]值,除以temp3,并将其显示在数组中。请建议。提前谢谢你。Avi

package tester;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

public class Test1 {

public static void main(String[] args) throws FileNotFoundException 
{      
    Scanner inFile = new Scanner(new FileReader("one.txt"));

    float [][] glassdata = new float[3][4];

    loadArray(glassdata, inFile);
    displayArray(glassdata);

    System.out.println("\n***************\n");

    normalizingVector(glassdata);
}

public static void loadArray(float [][] g, Scanner inFile)
{
    for(int row = 0; row < g.length; row++)
    {
        for(int col = 0; col < g[row].length; col++)
        {
           g[row][col] = inFile.nextFloat();
        }
    }      
}

public static void displayArray(float [][] g)   
{
    for(int row = 0; row < g.length; row++)
    {
        for(int col = 1; col < g[row].length; col++)
        {
            System.out.print(g[row][col] + " ");
        }
        System.out.println();
    }
}

public static void normalizingVector(float [][] g)
{
    float temp1 = 0;
    float temp2 = 0;
    float temp3 = 0;
    float temp4 = 0;


    for (int row = 0; row < g.length; row++)
    {
        for(int col = 1; col < g[row].length; col++)
        {
            if(col < g[row].length)
            {
                temp1 = (float) Math.pow(g[row][col], 2);               
                temp2 = temp2 + temp1;
            }
            temp3 = (float) Math.sqrt(temp2);
            temp4 = (g[row][col]) / temp3;
            //System.out.print((g[row][col] / temp3) + " ");
            System.out.print(temp4 + " ");
        }
        //System.out.print(temp3);
        System.out.println();
        temp1 = 0;
        temp2 = 0;
        temp3 = 0;
        temp4 = 0;

    }
}
}

1 个解决方案

#1


0  

Your normalizingVector method has some redundancy to it and does essentially nothing. The only values being assigned are temp1, temp2, temp3 and temp4 which are local and set back to 0 at every iteration of the for-loop.

你的normalizingVector方法有一些冗余,基本上什么都没有。唯一被分配的值是temp1、temp2、temp3和temp4,它们是本地的,在for循环的每次迭代中都返回到0。

I've rewritten your code a bit: It only uses one temporary variable now which is set to 0 at the start of every iteration of the outer loop (you could also do that at the end, but in my eyes it makes your code easier to read if you do it at the start). Note that temp1 is 0 initially and increments by the square value of the accessed array element each iteration. A statement of the form t += a stands short for t = t + a. I've used that to simplify the code a bit.

我重写代码:它只使用一个临时变量设置为0的外循环的每次迭代(最后你也可以这样做,但在我眼里它使您的代码更容易阅读如果你开始时)。注意,temp1最初是0,并且每次迭代都以访问数组元素的平方值递增。t += A的表达式,t = t + A的缩写,我用它来简化代码。

public static void normalizingVector(float [][] g)
{
float temp1;

for (int row = 0; row < g.length; row++)
{
    temp1 = 0;
    //col should start from 0, unless you intended that
    for(int col = 0; col < g[row].length; col++)
        temp1 += (float) Math.pow(g[row][col], 2);
    temp1 = (float) Math.sqrt(temp1);               
    for(int col = 0; col < g[row].length; col++)
        g[row][col] = g[row][col] / Math.sqrt(temp1);
}}

You need to add a second inner for loop because, when normalizing a vector, you want every element of the vector to be divided by its length.

你需要为循环添加第二个内层,因为,当使一个向量标准化时,你需要向量的每一个元素都除以它的长度。

#1


0  

Your normalizingVector method has some redundancy to it and does essentially nothing. The only values being assigned are temp1, temp2, temp3 and temp4 which are local and set back to 0 at every iteration of the for-loop.

你的normalizingVector方法有一些冗余,基本上什么都没有。唯一被分配的值是temp1、temp2、temp3和temp4,它们是本地的,在for循环的每次迭代中都返回到0。

I've rewritten your code a bit: It only uses one temporary variable now which is set to 0 at the start of every iteration of the outer loop (you could also do that at the end, but in my eyes it makes your code easier to read if you do it at the start). Note that temp1 is 0 initially and increments by the square value of the accessed array element each iteration. A statement of the form t += a stands short for t = t + a. I've used that to simplify the code a bit.

我重写代码:它只使用一个临时变量设置为0的外循环的每次迭代(最后你也可以这样做,但在我眼里它使您的代码更容易阅读如果你开始时)。注意,temp1最初是0,并且每次迭代都以访问数组元素的平方值递增。t += A的表达式,t = t + A的缩写,我用它来简化代码。

public static void normalizingVector(float [][] g)
{
float temp1;

for (int row = 0; row < g.length; row++)
{
    temp1 = 0;
    //col should start from 0, unless you intended that
    for(int col = 0; col < g[row].length; col++)
        temp1 += (float) Math.pow(g[row][col], 2);
    temp1 = (float) Math.sqrt(temp1);               
    for(int col = 0; col < g[row].length; col++)
        g[row][col] = g[row][col] / Math.sqrt(temp1);
}}

You need to add a second inner for loop because, when normalizing a vector, you want every element of the vector to be divided by its length.

你需要为循环添加第二个内层,因为,当使一个向量标准化时,你需要向量的每一个元素都除以它的长度。