jeudi 9 février 2017

Invalid input in java

Vote count: 0

The program should looks like this:

Enter the diameter of a cylinder (in centimeters): one 2 Please enter an integer value (less than 2,147,483,648) as decimal digits: -3 Please enter a positive integer value: 4444444444 Please enter an integer value (less than 2,147,483,648) as decimal digits: 5 six Enter the height of a cylinder (in centimeters): -7 Please enter a positive integer value: eight Please enter an integer value (less than 2,147,483,648) as decimal digits: 9999999999 Please enter an integer value (less than 2,147,483,648) as decimal digits: 10 11

A can with a diameter of 5 and a height of 10 has a volume of 196.35, and a surface area of 196.35.

But I really dont know how to change my code...Really need help!!!

System.out.println("Enter the diameter of a cylinder (in centimeters):");

    while (!scnr.hasNextInt() ) {
        if( !scnr.hasNextInt())
        System.out.println("Please enter an integer value: ");
        scnr.next();
    }



    while(d<=0){
        if(d<=0)
        System.out.println("Please enter a positive integer value: ");      
        scnr.next();    
}
    d = scnr.nextFloat();

    while (d > 2147483647 )
    {
        if(d > 2147483647)
        System.out.println("Please enter an integer value (less than 2,147,483,648) as decimal digits: ");  
        scnr.next();
    }

    d = scnr.nextFloat();


    System.out.println("Enter the height of a cylinder (in centimeters):");
        //Set the value that users just input to height
        h = scnr.nextFloat();

        while (!scnr.hasNextInt() ) {
            if( !scnr.hasNextInt())
            System.out.println("Please enter an integer value: ");
            scnr.next();
        }



        while(h<=0){
            if(h<=0){
            System.out.println("Please enter a positive integer value: ");
            }
            scnr.next();    
    }

        while (h > 2147483647 )
        {
            if(h > 2147483647)
            System.out.println("Please enter an integer value (less than 2,147,483,648) as decimal digits: ");  
            scnr.next();
        }












    //Calculate the value of volume and surface area of the cylinder
    v = (float) ((h*Math.PI*d*d)/4.0);
    s = (float) ((Math.PI*d*d/2 + d*Math.PI*h));

    //Print the value of volume

    System.out.println("A can with a diameter of " + d+ " and a height of "+ h+ " has ");
    //Print with only two places to the right of the decimal
    System.out.printf("\ta volume of %.2f", v );
    System.out.println(",");

    //Print the value of surface area

    //Print with only two places to the right of the decimal
    System.out.printf("\tand a surface area of %.2f", s);
    System.out.println(".");

asked 17 secs ago

Let's block ads! (Why?)



Invalid input in java

Aucun commentaire:

Enregistrer un commentaire