2013-03-25 86 views
0

我有一個主方法和4個其他函數類型的方法,其中包括計算,但是,我將如何調用每個方法進入主方法並繼續打印出計算結果。此外,我目前收到很多語法錯誤。如何調用主定製方法中的計算方法

我試過在需要時放置括號和大括號,但是,這只是導致了更多的錯誤。另外,我嘗試初始化其他地方的字符串和整數,但似乎仍然失敗。任何幫助將非常感激!

一些語法錯誤包括:';'預計在第60行

insert';'完成localVariableDelcartion上線60

重複,每行

import java.io.*; 
//create the class 


public class CirclemethodsFixedagain 

{ 
    //main method 
    public static void main(String[] args) throws IOException 
    { 


     BufferedReader myInput = new BufferedReader(new InputStreamReader(System.in)); 

     String numInput; 
     String reqInput; 
     String amountStr; 
     double numInt = 0; 
     double num = 0; 


     System.out.println("This program will ask for a given user radius, then proceed to calculate the user input"); 
     System.out.println("The program will use four methods to achieve this, all calling back to the main method"); 
     System.out.println("Press any key to continue"); 
     numInput = myInput.readLine(); 

     // more user questions 
     System.out.println("First, what would you like to calculate?"); 
     System.out.println("Enter '1' for Circumference, '2' for area, '3' for volume, or '4' for surface area"); 
     System.out.println("*NOTE* Pressing a key outside of this range or a regular character will re-prompt the original message"); 
     reqInput = myInput.readLine(); 
     numInt = Double.parseDouble(reqInput); 

     // more user questions 
     System.out.println("Now enter the radius of the required shape(Half of diameter)"); 
     System.out.println("*NOTE* Pressing a regular character will re-prompt the original message"); 
     numInput = myInput.readLine(); 
     num = Double.parseDouble(numInput); 



    } 
    //user created method, with each 
    public static int circlemethods(double circumference) throws IOException { 

     { 

      if (numInt == 1) 
      { 
       System.out.println("You chose to calculate circumference, given the radius :" + num); 
       circumference = (Math.PI) * (2) * (num); 
       System.out.print("The circumference of that sphere is :"); 
       return circumference; 


      } 

      public static double circlemethods2 (double area) throws IOException 
      { 
       if (numInt == 2) 
       { 
        System.out.println("You chose to calculate area, given the radius :" + num); 
        area = (Math.PI * num * num); 
        System.out.print("The area of the circle is :"); 

        return area; 
       } 
      }  
      public static double circlemethods3 (double volume) throws IOException 
      { 
       if (numInput == 3) 
       { 
        System.out.println("You chose to calculate volume, given the radius :" + num); 
        volume = (4 * Math.PI * num * num * num)/3 ; 
        System.out.print("The volume of that sphere is : cm³"); 

        return volume; 
       } 
      } 
      public static double circlemethods4 (double surfaceArea) throws IOException 
       if (numInput == 4) 
       { 
        System.out.println("You chose to calculate surface area, given the radius :" + num); 
        surfaceArea = 4 * Math.PI * num * num; 
        System.out.print("The Surface area of that sphere is :"); 

        return surfaceArea; 
       } 
     } 


    } 
} 

回答

0

你的牙套這些錯誤 - {和}字符 - 不匹配。我已經修復了問題中代碼的縮進情況,以便您可以更好地查看問題的起點 - 方法circlemethods。另外,circlemethods4缺少大括號。

在整個程序中保持一致的縮進級別使得這些類型的錯誤更加明顯。

0

編譯錯誤造成的:

  1. 你不能把裏面的其他方法的方法,將circlemethods 2,3,4 circlemethod1之外。

  2. 你的circlemethods看不到numInt局部變量。它在主要方法中聲明,並且僅在該方法中可見。

我相信你不需要if語句在每個circlemethods的開始。你最好這樣的事情:

if (numInt == 1) 
{ 
    circlemethod1(radius); 
} else if (numInt == 2) { 
    circlemethod2(radius); 
} 

等在你的主要方法。

您也可以更改每個circlemethod的參數名稱,因爲我知道它總是半徑。參數的當前名稱是方法名稱的一個很好的候選者。

0

以下是這將解決問題的投入:

  1. 你不能聲明的方法中的方法,這不是Java的語法。只需檢查支撐是否正確。使用任何IDE來做同樣的事情。
  2. 將numInt,num作爲靜態(Class)變量。正如你在靜態方法中使用那些。
  3. 使用專有名稱和駱駝命名法來命名任何方法。
    | e.g calculateCircleArea(),calculateCircleVolume()等。

希望這能解決你的問題。