0

前の質問への回答、その質問に対する私の最初の答えは解決されましたが、ループに関しては別の問題があり、後でforループを使用するだけで解決されました。

ただし、私の問題は、例外が処理された後、ユーザーが常にプログラムを再起動する必要がないようにすることです。むしろ、同じ最初の質問をユーザーにループさせたいのです。returnステートメントの後にprintステートメントを配置し、try catchの後にロジックコードを完全にコピーしようとしましたが、それによってユーザーが例外に対して無制限にループすることはないことに気付きました。また、補足として、私の前の質問には良い答えがありましたが、誰も私のより頻繁な問題に答えることができませんでした。そのため、誰も答えにチェックマークを付けませんでした。

import java.io.*;
import java.text.DecimalFormat;

public class Test
{

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

        double x;
        x = circlemethods(0.0, 0.0, 0.0, 1.0);

    }

    public static double circlemethods(double volume, double surfacearea,
            double area, double radius) throws IOException
    {

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

        String numInput;
        String reqInput;
        String amountStr;
        double numInt = 0;
        double num = 0;
        double answer = 0;
        double amount = 0;
        double answer2 = 0;
        double answer3 = 0;
        double answer4 = 0;

        for (double i = 0; i < 999; i++)
            ;
        try
        {

            // for (double i = 0; i < 999; i++);

            // while (numInt != 999) {

            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();

            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");
            reqInput = myInput.readLine();
            numInt = Double.parseDouble(reqInput);

            System.out.println("Now enter the radius of the required shape(Half of diameter)");
            numInput = myInput.readLine();
            num = Double.parseDouble(numInput);

            DecimalFormat nextAmount = new DecimalFormat("0.00");
            amountStr = nextAmount.format(amount);

            if (numInt == 1)
            {
                System.out.println("You chose to calculate circumference, given the radius :" + num);
                answer = (3.14) * (2) * (num);
                System.out.print("The circumference of that sphere is :");
                System.out.println(answer + "cm³");
                return answer;
            }
            else if (numInt == 2)
            {
                System.out.println("You chose to calculate area, given the radius :" + num);
                answer2 = (3.14) * 2;
                System.out.print("The area of the circle is :");
                System.out.println(answer2 + "cm²");
                return answer2;
            }
            else if (numInt == 3)
            {
                System.out.println("You chose to calculate volume, given the radius :" + num);
                answer3 = 4 / 3 * (3.14) * (num) * (3) * (3) * (3);
                System.out.print("The volume of that sphere is : cm³");
                System.out.println(answer3 + "cm³");
                return answer3;
            }
            else
            // if (numInt == 4)
            {
                System.out.println("You chose to calculate surface area, given the radius :" + num);
                answer4 = 4 * (3.14) * (num) * (2) * (2);
                System.out.print("The Surface area of that sphere is :");
                System.out.println(answer4 + "cm²");
                return answer4;

            }

        } catch (Exception e)
        {
            System.out.println("Please do not enter any string values, next time input enter a number ");
            return 0;
            // how to loop this untill the user inputs a number????

        }

    }

}
4

2 に答える 2

0

有効な値を取得するまでループする必要があります。これを行う1つの方法は、ブール値がtrueに設定されるまでループすることです。これに似たものを試してください:

boolean inputIsValid = false;
while (!inputIsValid) { ...

次に、有効な入力があると判断したら、次の行を追加します。

inputIsValid = true;

inputIsValidがtrueになるまで、ループが続きます。

無限のwhileループを作成することもできます。次に、有効な入力を受け取ったらbreak、ループから外れます。

while(true) {
    //when valid input is received
    break;
}
于 2013-03-18T23:56:38.467 に答える
0

まず、while(true)999回ループする代わりにループを使用します。(そのループの直後にセミコロンがあることを考えると、そのループは実際には何もしません。)

次に、ブロックからを削除return 0;しますcatch

だからそれは

while(true) {
    try {
        .... //your code
    }
catch {...}
} //while loop end bracket

このように、ループは、returnステートメントの1つに到達した場合にのみ終了します。

于 2013-03-19T00:00:10.427 に答える