gpt4 book ai didi

java - 如果我尝试将数组作为参数传递到不同的公共(public)类中,为什么我的构造函数无法在 Java 中编译?

转载 作者:行者123 更新时间:2023-12-01 18:40:23 25 4
gpt4 key购买 nike

我正在尝试编译此代码,但不断收到错误 -错误:/Users/Pedro/Dropbox/school/java/Proj/exam 3/EchoText.java:23:找不到符号符号:构造函数 MultiPoint(java.lang.Double[])位置:多点类

如果我在公共(public) EchoText 的构造函数中省略数组,它将进行编译。但是,我需要它将数组作为参数传递到 MultiPoint 公共(public)类中。我的问题是为什么它不能与参数一起编译以及这个错误到底意味着什么?

public class MultiPoint
{
private double[] testCoord;

public MultiPoint(double[] coordPt)
{
testCoord = coordPt;
}

public void printPoints()
{
for ( int i = 0; i < coordPt.length; i++ )
{
System.out.println("Coordinate # " + i + " : " + coordPt[ i ]);
}
System.out.println("");
}
}

import java.util.Scanner;
import java.lang.Math;
import java.util.ArrayList;

public class EchoText
{
private static String ans = null;

public static double number, coord;
private static int counter =0;
private static ArrayList <Double> coordList = new ArrayList<Double>();
public static void main(String[] args)
{

mainDeal();
Double coordPt[] = new Double[ coordList.size() ];
coordList.toArray(coordPt);

for (Double list1 : coordPt)
{
System.out.println((counter + 1) + list1);
}
MultiPoint myMultiPoint = new MultiPoint(coordPt);
}
// HELP MENU CREATED BY PROF. BLANDO! I DO NOT TAKE CREDIT FOR THE HELP MENU TEXT!
public static void helpMenu()
{
System.out.println("****************************** LAB 7 HELP MENU ******************************");
System.out.println("\t\t\tBy Pedro Estrada");
System.out.println("* This program defines a point in a N-Dimensional space. ");
System.out.println(" - Each point can have different number of non-zero coordinate");
System.out.println(" - You may request a random number for any coordinate by typing \"RANDOM\"");
System.out.println(" - When you are finished entering the coordinate just press the <Enter> key");
System.out.println();
System.out.println("* Pairs of point are used to create a lines");
System.out.println(" - If the 2 points have mismatched dimensions, the point from the lower-dimension space is");
System.out.println(" converted to a higher dimension point with added coordinate of 0");
System.out.println(" - When a line is created, the line distance is provided");
System.out.println();
System.out.println("* When you are done specifying points and lines type \"EXIT\" to display final operation statistics");
System.out.println("* All key words are case insensitive and can be abbreviated");
System.out.println("* Random number will be scaled between -1,000.00 and +1,000.00");
System.out.println();
System.out.println("****************************** LAB 7 HELP MENU ******************************");
}
public static String userPrompt()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a coordinate: [R]andom: [H]elp: [E]xit to exit program: Enter key for next point:");
return keyboard.nextLine();
}
public static void mainDeal()
{

/** I pulled some code from this website:
* http://www.java-forums.org/new-java/19024-detect-enter-key-being-pressed.html
*/
while(true)
{
ans = userPrompt();

if((ans == null)|| (ans.length() == 0) ||(ans.trim().equals("")))
{
System.out.println("Exiting...");
break;
}
else if (ans.trim().toUpperCase().charAt(0) == 'R')
{
coord = Math.random();
counter++;
coordList.add(coord);

}
else if (ans.trim().toUpperCase().charAt(0) == 'E')
{
System.exit(0);
}
else if (ans.trim().toUpperCase().charAt(0) == 'H')
{
helpMenu();
}
else
{
coord = Double.parseDouble(ans);
counter++;
coordList.add(coord);
}
}
}

}

最佳答案

Java 会自动将 Double 转换(拆箱)为 double,但它不会对数组执行相同的操作。您正在使用 Double[] 调用构造函数,并且它需要一个 double[]

关于java - 如果我尝试将数组作为参数传递到不同的公共(public)类中,为什么我的构造函数无法在 Java 中编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20157582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com