当前位置: 首页 > 知识库问答 >
问题:

我有一个程序,我想用java验证用户输入,我想让用户输入一个双倍值,只输入一个双倍值,有什么建议吗?

苏高远
2023-03-14
package SteppingStone;

import java.util.Scanner;


public class SteppingStone2_IngredientCalculator {
    
    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        
        String nameOfIngredient = "";
        double numberCups = 0.0;
        int numberCaloriesPerCup = 0;
        double totalCalories = 0.0;
        
        System.out.println("Please Enter Ingredient Name: ");
        nameOfIngredient = scnr.nextLine(); //In case ingredient is more than one word long. 
        
        System.out.println("Please enter the number of cups of " + nameOfIngredient + " required: ");
        numberCups = scnr.nextDouble();

    /* ^^ here I want to make sure that the value entered 
          is numeric like 2 instead of two
          and I want to apply this to the later value 
          numberCaloriesPerCup.
    */

        
        System.out.println("Please enter the number of calories per cup of " + nameOfIngredient + " : ");
        numberCaloriesPerCup = scnr.nextInt();
        
        totalCalories = numberCups * numberCaloriesPerCup;
        
        System.out.println(nameOfIngredient + " uses " + numberCups + " cups and this amount contains " + totalCalories + " total calories.");
        
    }

}

共有1个答案

雍焱
2023-03-14

您可能希望将其接受为字符串,并检查它是否是数字的或不使用String方法。如果格式正确,您可以向前移动,或者在显示错误时重新提示用户输入正确的值。

public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        
        String nameOfIngredient = "";
        double numberCups = 0.0;
        int numberCaloriesPerCup = 0;
        double totalCalories = 0.0;
        
        System.out.println("Please Enter Ingredient Name: ");
        nameOfIngredient = scnr.nextLine(); //In case ingredient is more than one word long. 
        
        System.out.println("Please enter the number of cups of " + nameOfIngredient + " required: ");
        String numCups = scnr.next();
        while(!numCups.chars().allMatch( Character::isDigit ))
        {
             System.out.println("Incorrect format for number of cups. Please enter numeric values");
             numCups = scnr.next();
        }
        numberCups = Double.parseDouble(numCups);
            
        System.out.println("Please enter the number of calories per cup of " + nameOfIngredient + " : ");
        numberCaloriesPerCup = scnr.nextInt();
        
        totalCalories = numberCups * numberCaloriesPerCup;
        
        System.out.println(nameOfIngredient + " uses " + numberCups + " cups and this amount contains " + totalCalories + " total calories.");
        
    }

或者,您也可以使用try-catch语句来执行此操作。我相信这是解析双值的更好方法

 public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        
        String nameOfIngredient = "";
        double numberCups = 0.0;
        int numberCaloriesPerCup = 0;
        double totalCalories = 0.0;
        
        System.out.println("Please Enter Ingredient Name: ");
        nameOfIngredient = scnr.nextLine(); //In case ingredient is more than one word long. 
        
        System.out.println("Please enter the number of cups of " + nameOfIngredient + " required: ");
        String numCups = scnr.next();
        while(numberCups==0.0)
        {
            try {
                numberCups = Double.parseDouble(numCups);
            } catch (NumberFormatException e) {
                System.out.println("Incorrect format for number of cups. Please enter numeric values");
                numCups = scnr.next();
            }
        }
        System.out.println("Please enter the number of calories per cup of " + nameOfIngredient + " : ");
        numberCaloriesPerCup = scnr.nextInt();
        
        totalCalories = numberCups * numberCaloriesPerCup;
        
        System.out.println(nameOfIngredient + " uses " + numberCups + " cups and this amount contains " + totalCalories + " total calories.");
        
    }
 类似资料: