2014-10-27 86 views
-5
import java.util.Scanner; 
import static java.lang.System.*; 

public class main 
{ 
    public static void main(String[] args) 
    { 

       Scanner keyboard = new Scanner(System.in); 

       int choice = 0; 
       double fAnswer = 0.0; 
       String quadAns; 
       boolean choice1; 

       while(choice != 4) 
       {  
         choice1 = false; 
         out.println("\n+-------------------------------------+\n"); 
         out.println("\t1) Quadratic Calculator"); 
         out.println("\t2) Area Calculator"); 
         out.println("\t3) Volume Calculator"); 
         out.println("\t4) Exit"); 

         choice = keyboard.nextInt(); 

         if (choice == 1) 
         { 
           double [] a = quadCal(); 

           out.print("The answer is: "); 
           out.println(java.util.Arrays.toString(a)); 
           // found above code here: http://stackoverflow.com/questions/12869741/returning-arrays-in-java 
           // found above code here: http://stackoverflow.com/questions/12917166/java-printing-an-array-with-arrays-tostring-error 
           choice1 = true; 
         } 

         else if (choice == 2) 
         { 

           fAnswer = areaCal(); 

         } 

         else if(choice == 3) 
         { 
           fAnswer = volCal(); 
         } 

         else if(choice == 4) 
         { 
           System.exit(0); 
         } 

         else 
         { 
           out.println("Error"); 
           choice1 = true; 
         } 

         if (choice1 == false) { 
         out.println("The answer is: " + fAnswer);  
         } 

         else 
         { 
         } 

       } 

    } 

    public static double[] quadCal() 
    { 

       Scanner keyboard = new Scanner(System.in); 

       double[] quad; 
       quad = new double[2]; 
       double a, b, c, n = 0; 
         out.print("Enter the value of a: "); 
         a = keyboard.nextDouble(); 
         out.println(); 
         out.print("Enter the value of b: "); 
         b = keyboard.nextDouble(); 
         out.println(); 
         out.print("Enter the value of c: "); 
         c = keyboard.nextDouble(); 
         out.println();     

         quad[0] = ((b*(1-2)) + Math.sqrt((b*b) - (4*a*c)))/(2*a); 
         quad[1] = ((b*(1-2)) - Math.sqrt((b*b) - (4*a*c)))/(2*a); 

       return quad; 
    } 

    public static double areaCal() 
    { 

       Scanner keyboard = new Scanner(System.in); 

       double a, b, area; 

       out.print("Enter the value of height: "); 
       a = keyboard.nextDouble(); 
       out.println(); 
       out.print("Enter the value of length: "); 
       b = keyboard.nextDouble(); 
       out.println(); 

       area = a * b; 

       return area; 
    } 

    public static double volCal() 
    { 

       Scanner keyboard = new Scanner(System.in); 

       double a, b, c, volume; 

       out.print("Enter the value of height: "); 
       a = keyboard.nextDouble(); 
       out.println(); 
       out.print("Enter the value of length: "); 
       b = keyboard.nextDouble(); 
       out.println(); 
       out.print("Enter the value of depth: "); 
       c = keyboard.nextDouble(); 
       out.println(); 

       volume = a * b * c; 

       return volume; 
    } 
} 

我无法返回我在quadCal函数中创建的数组。当答案打印在main中时,它总是为这两个数组值返回NaN。我如何获得它返回数组中的值?无法返回我的阵列

+2

邮政编码,我们不会做链接,您需要将它实际放在您的问题中,您无法链接它 – DreadHeadedDeveloper 2014-10-27 18:07:20

+0

只需添加 - 除了不方便之外,pastebin链接很可能会变陈旧,并且使您的问题对未来的堆叠式花费者不太有用。所以,我们更喜欢这里的代码是有原因的。 – FatalError 2014-10-27 18:10:54

+1

但是请仅发布_relevant_代码,而不是全部! – isnot2bad 2014-10-27 18:12:20

回答

0

NaN是Java中的有效双数。检查你正在进入一个,B,C的输入和手动评估代码的以下两行:

quad[0] = ((b*(1-2)) + Math.sqrt((b*b) - (4*a*c)))/(2*a); 
quad[1] = ((b*(1-2)) - Math.sqrt((b*b) - (4*a*c)))/(2*a); 

确保你不采取负数的平方根或通过0.0除以0.0。有关NaN的更多信息,请参阅this link