0
import java.io.*;
import java.util.*;

    public class volumeConeD

{//class
    public static void main (String [] args)
        {//main
            Scanner keyBoard = new Scanner(System.in);//input for keyBoard
        //variables
    double volume;
    double radius;
    double hieght;
    double oneThird = 0.3333;
    double pie = 3.14;
    double yes = 1.0;
    boolean r = true;

try
    {//begin of try
    while(r == true){
    System.out.print("Volume of a Cone... V=1/3(3.14)r^2(h)");
    System.out.println ();
    System.out.println ();

    radius = getRadius(radius);//call to method
    radius = keyBoard.nextDouble ();
System.out.print("Enter a Height      ");
hieght = keyBoard.nextDouble ();
            //math
        volume = oneThird * pie * radius * radius * hieght;
        System.out.printf ("Volume =       " + volume);
                                }//end of try                                           

            catch (Exception Error){
            System.out.println("You entered wrong data");
            }
            System.out.println ();
        System.out.print("Does the user wish to try again?");
        System.out.println ();
        System.out.print("Enter 1 to go again OR any other key to end.");
        yes = keyBoard.nextDouble();
        }//end of while


}//end of main
public static double getRadius(double mRadius)
{
    System.out.print("Enter Radius Squared Number      ");

return mRadius; 
    }
}//end of program

这是我第一次在这个论坛上发帖,所以请原谅如何问...这里去...我想要做的就是使用哨兵方法(while 循环)在用户控制下重复这个问题。我几乎早点就开始工作了,但我不断收到关于我如何定义“r”的错误。现在我收到关于我的 catch 尝试块的错误。请帮忙。

volumeConeD.java:35: error: 'catch' without 'try'
            catch (Exception Error){
            ^
volumeConeD.java:35: error: ')' expected
            catch (Exception Error){
                            ^
volumeConeD.java:35: error: not a statement
            catch (Exception Error){
                  ^
volumeConeD.java:35: error: ';' expected
            catch (Exception Error){
                                  ^
volumeConeD.java:19: error: 'try' without 'catch', 'finally' or resource declarations
try
^
5 
4

5 回答 5

1

您将您try {的放在循环之外while,但对应catch的是在while循环内。但必须要么在循环外,要么在循环内,在一起。

尝试将try {线条放在while循环内。

此外,看起来这些行也不起作用:

radius = getRadius(radius);//call to method
radius = keyBoard.nextDouble ();

getRadius所做的只是打印一个提示并返回传入的参数,radius. 但是radius还没有初始化。但无论如何,它似乎什么也没做。将方法重命名为promptForRadius,它不需要接受参数或返回任何内容。

public static void promptForRadius()
{
    System.out.print("Enter Radius Squared Number      ");
}

然后在调用它时:

promptForRadius();
// Then you can call this line (unchanged)
radius = keyBoard.nextDouble();
于 2013-09-06T21:55:16.723 回答
0

你的评论说它// end of try真的应该说// end of while,反之亦然。

于 2013-09-06T21:57:10.993 回答
0

这似乎是你的问题

try
{
    while (...)
    {
       int blammo;

       try
       {
           ... code
           blammo = 9;
       }
       catch ...
       {
          // catching here means that the while loop will continue looping.
       }

       System.out.println("Blammo: " + blammo); // This results in the error: variable
       // blammo might not have been initialized.   This is because the assignment
       // "blammo = 9" is inside the try block and if an exception was thrown before
       // the assignment then it (the assignment)  will never execute and blammo will
       // be uninitialized.

    } // end of while loop

} // end of outter try
catch ...
{
   // catching here means that the exception exits the while loop.
}
于 2013-09-06T22:13:48.890 回答
0

您使用了 catch,但它与 try 不匹配... oneThird 变量可以设置为 1 / 3 (更精确)。PI 也一样,数学库已经有了 PI 定义。函数 getRadius 没用,你应该把它去掉,或者用一个要求用户输入双数的函数替换它。

import java.util.Scanner;

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double volume, radius, height, oneThird = (1.0 / 3);
        int continueExecution = 1;

        try {
            while (continueExecution == 1) { // same as r == true (redundant)
                System.out.println("Volume of a Cone... V=1/3(3.14)r^2(h)\n\n"); // '\n' is the newline character
                radius = getDoubleValue(sc, "Enter radius : ");
                height = getDoubleValue(sc, "Enter height : ");
                volume = oneThird * Math.PI * Math.pow(radius, 2) * height;
                System.out.println("Volume = " + volume + "\nEnter 1 to start again, or another number to exit: ");
                continueExecution = sc.nextInt();
            }
        } catch (Exception e) { // Pokemon exception handling !
            System.err.println(e.getMessage());
        }
    }

    public static double getDoubleValue(Scanner sc, String msg) {
        System.out.print(msg);
        return sc.nextDouble();
    }
于 2013-09-06T22:15:45.297 回答
0

我已经重新格式化了代码。try/catch 块的大括号不能在 while 循环大括号之前结束。此外,您必须在使用它们之前初始化变量(例如,半径)。Eclipse 之类的 IDE 将有助于格式化和识别编译错误。顺便说一句,我没有检查代码的逻辑正确性,但更多的是编译和语法问题

import java.util.Scanner;

public class volumeConeD

{
    public static void main(String[] args)
    {
        Scanner keyBoard = new Scanner(System.in);// input for keyBoard
        // variables
        double volume;
        double radius = 0.0;
        double hieght;
        double oneThird = 0.3333;
        double pie = 3.14;
        double yes = 1.0;
        boolean r = true;

        try
        {// begin of try
            while (r == true)
            {
                System.out.print("Volume of a Cone... V=1/3(3.14)r^2(h)");
                System.out.println();
                System.out.println();

                radius = getRadius(radius);// call to method
                radius = keyBoard.nextDouble();
                System.out.print("Enter a Height      ");
                hieght = keyBoard.nextDouble();
                // math
                volume = oneThird * pie * radius * radius * hieght;
                System.out.printf("Volume =       " + volume);

                System.out.println();
                System.out.print("Does the user wish to try again?");
                System.out.println();
                System.out.print("Enter 1 to go again OR any other key to end.");
                yes = keyBoard.nextDouble();
            }// end of while

        }// end of try

        catch (Exception Error)
        {
            System.out.println("You entered wrong data");
        }
    }// end of main

    public static double getRadius(double mRadius)
    {
        System.out.print("Enter Radius Squared Number      ");

        return mRadius;
    }
}// end of program
于 2013-09-06T22:01:23.383 回答