在Java中使用While循环

时间:2013-09-06 21:52:19

标签: java

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

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

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 

5 个答案:

答案 0 :(得分: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();

答案 1 :(得分:0)

您的评论所说的// end of try应该说// end of while,反之亦然。

答案 2 :(得分:0)

我重新格式化了代码。 try / catch块的大括号在循环括号之前不能结束。您还必须在使用变量之前初始化变量(例如,半径)。 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

答案 3 :(得分: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.
}

答案 4 :(得分:0)

你使用了一个catch,但是它与try ...不匹配 oneThird变量可以设置为1/3(更高精度)。 对于PI,Math库已经具有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();
    }