简体   繁体   中英

Java - How would I return an exception if the user enters a invalid input?

I'm a complete beginner when it comes to coding. I'm currently taking a programming class at my local community college. We've been given an assignment to create a program that calculates the BMI (body mass index) of the user.

I've managed to create the basis structure of the program, where the user inputs there weight and height and have it calculated into the BMI however, there is one criteria I'm struggling to create.

In which is to display any invalid inputs of the following

  • Height is not less than 48 inches
  • height is not more than 80 inches
  • weight is not less than 75 Ibs
  • weight is not more than 300 Ibs

Example interactions

Please enter your height in inches: 20
sorry, you have enter a height that is not valid

Please enter your height in inches: 62
Please enter your weight in pounds: x
sorry, you did not enter a number in pounds


I've kinda have an idea, since people are suggesting to use the try, catch statement. I've attempted but I can seem to figure it out.

import java.util.*;
public class BMI {

    public static void main(String[ ] arg){

        // BMI calculator 
        Scanner keyboard = new Scanner ( System.in ); 
        final double BMI_CONVERSION = 703.0; // pounds/inches to kilograms/meters for BMI calculations 

        double bodyMassIndex; 
        double height = 0; 
        double weight = 0;
        String BMICategory = "";

        // user inputs 

        // inches
        System.out.print( "What's your height in inches? "); 
        height = keyboard.nextDouble();

        // Pounds
        System.out.print( "What's you weight in pounds? "); 
        weight = keyboard.nextDouble(); 

        // Calculate BMI 
        bodyMassIndex = weight * BMI_CONVERSION / (height * height); 
        if (bodyMassIndex < 18.5) 
        { 
            BMICategory = "considered at a underweight"; 
        } 
        else if (bodyMassIndex >= 18.5 && bodyMassIndex <= 25) 
        { 
            BMICategory = "considered at a optimal weight"; 
        } 
        else 
        { 
            BMICategory = "considered overweight"; 
        } 


        // output 
        System.out.println( "Your BMI is: " + bodyMassIndex + ", You are " +     BMICategory + ".");

    }
}

You need to reorganize your program if you want to use exceptions.Exception means you would have a try/catch block outside of function, that throws exception.

1) - Create class that would have necessary fileds for calculations, and calculate method.

2) - Create class that would be responsive for the input cycle, with separate functions for each varialbe. This method should throw Exception if the value is invalid.Create master-function that will call all necessary functions to read data and will close it into try/catch block Like this

public BMI masterFunc(){

try{


    readFirst();

    readSecond();

    //etc....
    BMI obj = new BMI();
    //set you params to new created class BMI
    //and return it from func
    return obj;

}catch(Exception e){

    return null;

 }

}

Now you could put it in cycle in main like

BMI obj = null;

while(obj == null)
    BMI obj = ReaderClass.masterFunc();

Hope i could answer you question!

看看“ public void functionName() throws InvalidInputException ”。

First of all, putting your code on and asking for help is against website rules.

Anyways, in your code you need to loop your input(while loop with value set as true would be the best bet), in this loop you check if the input is falling into your criteria. If it is then you break out of the loop.

This way you can stick to your criteria and get the BMI at the end.

PS the concepts used here are looping and breaking out of a loop, you can check out some examples online for better understanding

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM