在开始之前,此代码会被大量注释和分隔。
我编写了这段代码来测试一种方法,该方法将接收两个数字(根类型(例如,平方,立方体等)和基数(例如,49、64、81等)。但是当程序运行时,输出看起来像这样。
4的平方根= unitOne.Complex@5e481248
49的平方根= unitOne.Complex@66d3c617
27的立方根= unitOne.Complex@63947c6b
这是什么意思,我该如何更改?
我什至不知道我该如何用谷歌搜索这样的东西,我问过我认识的每个人,没有人可以提供帮助,所以这是我的最后选择。
注意:在阅读此代码时,我指的是复数,即负一值的平方根。
package unitOne;
import java.io.*;
public class SolverForSqrtOfNegOne {
public static void main(String[]args)throws IOException{//main function
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));//inputs
String again;
do {
System.out.println("enter the type of root (1,2,3,4 as in first root square root cube root fourth root etc)");//prompt
float rootType=Float.parseFloat(input.readLine());//inputs from user
while(rootType==0){//error check
System.out.println("wow youre so cool, you can find infinity! now enter something thats not the zeroth root");
rootType=Float.parseFloat(input.readLine());
}
System.out.println("whats under the root? (numbers only, no fractions, powers, scientific notation or anything else like that)");//prompt
double underRoot=Double.parseDouble(input.readLine());//inputs from user
System.out.println("answer is: ");
String x=String.valueOf(anyRoot(rootType,underRoot));
System.out.println(x);
System.out.println("again? (y/n)");
again=input.readLine();
while(!again.equals("y") && !again.equals("n")){
System.out.println("please, please. enter either y or n for yes or no. thankyou");
}
}while(again.equals("y"));
}
//above this is point is just collecting and returning data.
//below this point is the actual method that returns the calculated value.
public static Complex anyRoot(float rootType, double underRoot){
if(underRoot<0 && rootType%2 == 0 && rootType>0) {//for i
underRoot=underRoot*(-1);//factoring out -1
underRoot=Math.pow(underRoot,(1.0/rootType));//rooting everything thats not negitive one
return new Complex (underRoot , "*i");//outputs root in terms of i
}
else if(underRoot<0 && rootType%2 == 1 && rootType>0) {//if odd number root
underRoot=underRoot*(-1);//factoring out -1
underRoot=-Math.pow(underRoot,(1.0/rootType));//rooting everything thats not negitive one
return new Complex (underRoot,"") ;//outputs root in terms of i
}
else if(rootType>0){//if the value given is positive
underRoot=Math.pow(underRoot,(1.0/rootType));//rooting everything thats not negitive one
return new Complex (underRoot,"") ;//outputs root in terms of i
}
else if (rootType<0) {//for negitive roots
rootType=rootType*-1;
if(underRoot<0 && rootType%2 == 0 && rootType>0) {
underRoot=underRoot*(-1);//factoring out -1
underRoot=Math.pow(underRoot,(1.0/rootType));//rooting everything thats not negitive one
underRoot=1.0/underRoot;//recepricial
return new Complex (underRoot,"*i");//outputs root in terms of i
}
else if(underRoot<0 && rootType%2 == 1 && rootType>0) {
underRoot=underRoot*(-1);//factoring out -1
underRoot=-Math.pow(underRoot,(1.0/rootType));//rooting everything thats not negitive one
return new Complex (1/underRoot,"");//outputs root in terms of i
}
else{
underRoot=Math.pow(underRoot,(1.0/rootType));//rooting everything thats not negitive one
return new Complex (1/underRoot,"");//outputs root in terms of i
}
}
else {
return new Complex(0,"ERROR");
}
}
}
class Complex {
private double res1;
private String res2;
Complex(double res1, String res2) {
this.res1 = res1;
this.res2 = res2;
}
}
我收到了有关此程序的一些输入信息,该信息位于此处的上一个问题: Is there a way to return a Double AND a String from a single method in java?