2014-10-27 87 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