Auf Thema antworten

Kann mir jm. eine Lösung nennen um zu verhindern ,dass man ,um die zweite if Bedingung zu erfüllen, keine 2mal "U" als input eingeben muss? 


Danke schonmal!  :)


[code=Java]import java.util.Scanner;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;


public class Circle {

  

    public static void main(String[] args) {

      

        System.out.println("Enter your start variable.  (A, U, r oder d)");

      

        // define all variables as String

        String AS = ("A");

        String US = ("U");

        String rS = ("r");

        String dS = ("d");

      

        Scanner var = new Scanner(System.in);

        if(var.next().equals(AS)){

            double A = 0;

            System.out.println("Please enter area of your circle");

         

            try

            {

                    //get the area from console

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

                    A = Integer.parseInt(br.readLine());

            }

            //if invalid value was entered

            catch(NumberFormatException ne)

            {

                    System.out.println("Invalid radius value" + ne);

                    System.exit(0);

            }

            catch(IOException ioe)

            {

                    System.out.println("IO Error :" + ioe);

                    System.exit(0);

            }

         


            //NOTE : use Math.PI constant to get value of pi

            //define all variables

          

            double U = Math.PI * (Math.sqrt(A / Math.PI)) * (Math.sqrt(A / Math.PI));

            System.out.println("U = " + U);

          

            double r = Math.sqrt(A / Math.PI);

            System.out.println("r = " + r);

          

            double d = (Math.sqrt(A / Math.PI) + (Math.sqrt(A / Math.PI)));

            System.out.println("d = " + d);

            }

      

            else if (var.next().equals(US)){

                double U = 0;

                System.out.println("Please enter extent of your circle");

         

                try

                {

                    //get the extent from console

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

                    U = Integer.parseInt(br.readLine());

                }

                //if invalid value was entered

                catch(NumberFormatException ne)

                {

                    System.out.println("Invalid extent value" + ne);

                    System.exit(0);

                }

                catch(IOException ioe)

                {

                    System.out.println("IO Error :" + ioe);

                    System.exit(0);

                }

         


                //NOTE : use Math.PI constant to get value of pi

                //define all variables

          

                double A = Math.PI * ((U / (2 * Math.PI) * (U / (2 * Math.PI))));

                System.out.println("A = " + A);

          

                double r = U / (2 * Math.PI);

                System.out.println("r = " + r);

          

                double d = (U / (2 * Math.PI) + (U / (2 * Math.PI)));

                System.out.println("d = " + d);

          

            }

        }

    }



  

                                                  

                    [/code]



Oben