0

トライ&キャッチで困っています。要件は、do while ループ内にある必要があることです。数値以外の値をキャッチする必要があります (簡単にするために、double 以外の値をキャッチしようとしています)。数字を入力するとプログラムは正常に動作しますが、文字を入力すると、目的の「数字がありません」というメッセージが表示されません。コードは次のとおりです。

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

    public class Triangle {

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

            // inputting a new scanner
            Scanner input = new Scanner(System.in);
            double a=0; double b=0; double c=0;
            do {   
                try{
                    // prompt the user to enter values
                    System.out.println("Enter values for triangle sides a, b and c"
                    + "\n(only numbers are accepted): ");
                    a=input.nextDouble(); // inputing and declaring value as double
                    b=input.nextDouble(); // inputing and declaring value as double
                    c=input.nextDouble(); // inputing and declaring value as double
                }
                catch (NumberFormatException nfe) {
                    System.out.println("Not a number");
                    }

                if (a == (double)a && b == (double)b && c == (double)c)
                    System.out.println("\nThe values you have entered are:\na = " + a +
                    "\nb = " + b + "\nc = " + c);

                boolean sumGreater = isTriangle(a, b, c); // invoking method isTriangle

                // if statement to check if entered values form triangle or not
                if (!sumGreater)

                    // Display message if statement is false
                    System.out.println("The entered values do not form a triangle");

                else
                    // Display output if message is true.
                    // Methods triangleType,
                    // perimeter and area, are invoked inside the output.
                    System.out.printf("\nThe type of triangle is: %s" + "\nPerimeter = %.2f" + "\nArea = %.2f \n", triangleType(a,b,c), perimeter(a,b,c), area(a,b,c));

            } while (a==(double)a && b==(double)b && c==(double)c);
        }

        // Defining method isTriangle as boolean
        public static boolean isTriangle(double a, double b, double c) {
            boolean sumGreater; // declaring expression as a boolean

            // if statement to check if the entered values form a triangle,
            // using the triangle inequality theorem, where sum of any two sides
            // must be greater the the remaining third side
            if((a+b)>c && (a+c)>b && (b+c)>a)
                sumGreater = true;
            else
                sumGreater = false;
            return sumGreater; // returning the value to main method
        }

        // Defining method perimeter as double
        public static double perimeter(double a, double b, double c) {
            double perimeter = a+b+c; // declaring the sum of the values as double
            return perimeter; // returning the value of perimeter to main method
        }

        // Defining method area as double, using Heron's formula to calculate area
        public static double area(double a, double b, double c) {
            double s=(a+b+c)/2;
            double h=s*(s-a)*(s-b)*(s-c);
            double area = Math.sqrt(h);
            return area; // returnig the value of area to main method
        }

        // Defining method triangleType as String, to determine type of triangle
        public static String triangleType(double a, double b, double c) {
            String triangleType = " ";

            // if statement to determine type of triangle
            if (a==b&&a==c&&b==c)
                triangleType = "Equilateral";
            else if(a==b||b==c||a==c)
                triangleType = "Isosceles";
            else
                triangleType = "Scalene";
            return triangleType; // returning value of triangleType to main method
        }
    }
4

5 に答える 5

0

提案してくれたすべての人に感謝します。最終的に、入力に従って望ましい結果を得るコードを書くことができました:

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

public class Triangle {

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

    // inputting a new scanner
    Scanner input = new Scanner(System.in);

    double a=0; double b=0; double c=0; // declaring and initializing doubles

    // starting do-while loop
    do {

        // using try-catch method to catch mistakenly entered values
        try{
            // prompt the user to enter values
            System.out.println("Enter values for triangle sides a, b and c"
            + "\n(only numbers are accepted): ");
            a=input.nextDouble(); // inputing and declaring value as double
            b=input.nextDouble(); // inputing and declaring value as double
            c=input.nextDouble(); // inputing and declaring value as double
        }
        catch (InputMismatchException e) {

            // display message if entered value isn't a number
            System.out.println("Not a number");
            }
    } while (a!=(double)a && b!=(double)b && c!=(double)c); // end of do-while loop

        // displaying entered values, while avoiding to display the initialized 
        if (a!=0 && b!=0 && c!=0)
            System.out.println("\nThe values you have entered are:\na = " + a +
                    "\nb = " + b + "\nc = " + c);

        boolean sumGreater = isTriangle(a, b, c); // invoking method isTriangle

        // if statement to check if entered values form triangle or not,
        // while avoiding the initialized values to be displayed
        if (!sumGreater && a!=0 && b!=0 && c!=0)

            // Display message if statement is false
            System.out.println("The entered values do not form a triangle");

        else if(a!=0 && b!=0 && c!=0)
            // Display output if message is true.
            // Methods triangleType,
            // perimeter and area, are invoked inside the output.
            System.out.printf("\nThe type of triangle is: %s" + 
                    "\nPerimeter = %.2f" + "\nArea = %.2f \n", 
                    triangleType(a,b,c), perimeter(a,b,c), area(a,b,c));
}

// Defining method isTriangle as boolean
public static boolean isTriangle(double a, double b, double c) {
    boolean sumGreater; // declaring expression as a boolean

    // if statement to check if the entered values form a triangle,
    // using the triangle inequality theorem, where sum of any two sides
    // must be greater the the remaining third side
    if((a+b)>c && (a+c)>b && (b+c)>a)
        sumGreater = true;
    else
        sumGreater = false;
    return sumGreater; // returning the value to main method
}

// Defining method perimeter as double
public static double perimeter(double a, double b, double c) {
    double perimeter = a+b+c; // declaring the sum of the values as double
    return perimeter; // returning the value of perimeter to main method
}

// Defining method area as double, using Heron's formula to calculate area
public static double area(double a, double b, double c) {
    double s=(a+b+c)/2;
    double h=s*(s-a)*(s-b)*(s-c);
    double area = Math.sqrt(h);
    return area; // returnig the value of area to main method
}

// Defining method triangleType as String, to determine type of triangle
public static String triangleType(double a, double b, double c) {
    String triangleType = " ";

    // if statement to determine type of triangle depending on met conditions
    if (a==b&&a==c&&b==c)
        triangleType = "Equilateral";
    else if(a==b||b==c||a==c)
        triangleType = "Isosceles";
    else
        triangleType = "Scalene";
    return triangleType; // returning value of triangleType to main method
}

}

于 2013-10-28T11:38:57.057 に答える
0

行の後に構文エラーがあります

} while (a==(double)a && b==(double)b && c==(double)c);

}この行の後に aを付けます。あなたのプログラムは私のために実行されているようです。

于 2013-10-27T18:26:42.543 に答える
0

以下のコードを使用してください。要件に適しています。あります } が抜けているうちに追加してください。

            try{
                // prompt the user to enter values
                System.out.println("Enter values for triangle sides a, b and c"
                + "\n(only numbers are accepted): \t");
                a=Double.parseDouble(input.next());
                b=Double.parseDouble(input.next());
                c=Double.parseDouble(input.next());
            }
            catch (NumberFormatException nfe) {
                System.out.println(" Not a number");
                continue;
                }
于 2013-10-27T19:16:57.367 に答える
0

間違ったタイプの例外をキャッチしており、表示されたエラー メッセージがそれを示しています。このメソッドは、トークン (この場合は入力からのテキスト) が浮動小数点数でない場合にScanner.nextDouble()スローします。InputMismatchExceptionはScanner ではなく、メソッドNumberFormatExceptionによってスローされます。Double.parseDouble()

一般的なアドバイスとして、使用するメソッドの Javadoc ドキュメントを読む必要があります。メソッドを呼び出したときに発生する可能性のある結果と例外の種類が明確に説明されています。この場合のように、例外のキャッチは常にコンパイラによって強制されるわけではありjava.util.InputMismatchExceptionませRuntimeExceptionん。つまり、チェックされた例外ではないため、キャッチする必要はありません。

于 2013-10-27T21:18:58.507 に答える