简体   繁体   English

Java多维数组程序问题

[英]Java Multi Dimensional Array Program Trouble

I don't understand why i'm getting these errors when i compile my code. 我不明白为什么在编译代码时会出现这些错误。

Error: F:\\G\\programA: operator * cannot be applied to int,double[] 错误:F:\\ G \\ programA:运算符*无法应用于int,double []

Error: F:\\G\\programA: operator - cannot be applied to double,double[] 错误:F:\\ G \\ programA:运算符-无法应用于double,double []

import java.io.*;

public class programA
{
  public static void main (String [] args) throws IOException
  {
    BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));

  int points = 100, dimension = 3;

  double [] length;
  length = new double [dimension];

  double [][] loc;
  loc = new double [points][dimension];

   for (int x = 0; x < points; x++){
  for (int y = 0; y < dimension; y++){
   location [x][y]= (Math.random() * (2 *length)) - length;
  }
  }

  }
}
 2 *length 

You're multiplying an array by an int. 您正在将一个数组乘以一个int。 Not going to happen. 不会发生。 The code around that is somewhat unclear, but since you're trying to set a single array element you'll want to get a single element of length , such as by length[0] or length[someIntInRange] . 周围的代码尚不清楚,但是由于您尝试设置单个数组元素,因此需要获得一个length元素,例如通过length[0]length[someIntInRange]

Also, location [x][y] should be loc[x][y] as there is no field called location. 另外, location [x][y]应为loc[x][y]因为没有称为位置的字段。

Since the code isnt clear, I am assuming you need 由于代码不清楚,所以我假设您需要

loc [x][y]= (Math.random() * (2 *length[y])) - length[y];

Math Operations cannot be done between an array and a number. 不能在数组和数字之间进行数学运算。

I don't understand why i'm getting these errors when i compile my code. 我不明白为什么在编译代码时会出现这些错误。

Error: F:\\G\\programA: operator * cannot be applied to int,double[] 错误:F:\\ G \\ programA:运算符*无法应用于int,double []

Error: F:\\G\\programA: operator - cannot be applied to double,double[] 错误:F:\\ G \\ programA:运算符-无法应用于double,double []

You are trying to multiply an int and later a double against an array of type double . 您正在尝试繁殖的int和后来double反类型的数组 double This is not possible. 这是不可能的。 If you want to multiply against all values in the array, you need to iterate over it to do so. 如果要乘以数组中的所有值,则需要对其进行迭代。

Your problem is an undefined operation here... 您的问题是这里的未定义操作...

double [] length;
length = new double [dimension];
...
(2 *length)

You need to select a value from the length array. 您需要从长度数组中选择一个值。

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

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