Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java输入验证:如何在空间中分割用户输入?_Java_Input - Fatal编程技术网

java输入验证:如何在空间中分割用户输入?

java输入验证:如何在空间中分割用户输入?,java,input,Java,Input,我的任务: 通过确保用户为英尺输入正整数,为英寸输入非负小数,为重量输入正小数来执行输入验证。有关详细信息,请参见示例会话。特别是,请注意echo打印的身高和体重值以及生成的体重指数值的格式 我的程序可以运行,但我不知道如何使它看起来像下面的示例程序。我必须让用户输入的高度,以英尺,然后空间,然后英寸。我不知道如何在空格处分割用户输入 示例会话: 使用英尺空间英寸(例如5.6.25)输入高度:hi here 无效的英尺值。必须是整数。 无效的英寸值。必须是十进制数。 使用英尺空间英寸(例如5.6

我的任务:

通过确保用户为英尺输入正整数,为英寸输入非负小数,为重量输入正小数来执行输入验证。有关详细信息,请参见示例会话。特别是,请注意echo打印的身高和体重值以及生成的体重指数值的格式

我的程序可以运行,但我不知道如何使它看起来像下面的示例程序。我必须让用户输入的高度,以英尺,然后空间,然后英寸。我不知道如何在空格处分割用户输入

示例会话:

使用英尺空间英寸(例如5.6.25)输入高度:hi here
无效的英尺值。必须是整数。
无效的英寸值。必须是十进制数。
使用英尺空间英寸(例如5.6.25)重新输入高度:0.9
无效的英尺值。必须是积极的。
使用英尺空间英寸重新输入高度(例如,5.6.25):5.25 0
无效的英尺值。必须是整数。
使用英尺空间英寸重新输入高度(例如,5 6.25):5 9.25
以磅为单位输入重量:0
无效的磅值。必须是积极的。
重新输入重量(单位:磅):150.5
高度=5'-9.25“
重量=150.5磅
体重指数=22.1
到目前为止,我的代码是:

package ch14;
import java.util.Scanner;

public class BMI {


public static void main(String[] args) {

    String inputString;
    double bmi;
    double pounds;
    double inches;
    String feet;

    Scanner stdIn = new Scanner(System.in);

    System.out.print("Please enter height using feet space inches (e.g., 5 6.25): ");
    inputString = stdIn.nextLine();


    try
    {
        Double.parseDouble(inputString);
    }
    catch(NumberFormatException nfe)
    {
        System.out.println ("Invalid inch value. Number must be a decimal.");
        System.out.print ("Re-enter height in inches: ");
        inputString = stdIn.nextLine ();
    }

    inches=Double.parseDouble(inputString);

    if(inches<=0)
    {
        System.out.println("Invalid inch value. Must be a positive number.");
        System.out.print("Re-enter height in inches:");
        inputString = stdIn.nextLine();
        inches=Double.parseDouble(inputString);
    }

    System.out.print("enter weight(in pounds) ");
    inputString = stdIn.nextLine();

    try
    {
        Double.parseDouble(inputString);
    }
    catch(NumberFormatException nfe)
    {
        System.out.println("Invalid pound value. Number must be a decimal.");
        System.out.print("Re-enter the weight in pounds: ");
        inputString = stdIn.nextLine();
    }

    pounds=Double.parseDouble(inputString);

    if(pounds <= 0)
    {
        System.out.println("Invalid pound value. Must be a positive number.");
        System.out.print("Re-enter the weight in pounds:");
        inputString = stdIn.nextLine();
        pounds=Double.parseDouble(inputString);
    }

    System.out.println("Height = " + inches + "\"");
    System.out.println("Weight = " + pounds + " pounds");
    System.out.printf("Body Mass Index = %.1f\n",(pounds * 703.)/(inches * inches));

    }//end main
}
包装ch14;
导入java.util.Scanner;
公共类BMI{
公共静态void main(字符串[]args){
字符串输入字符串;
双倍体重指数;
两磅;
双英寸;
线脚;
扫描仪标准输入=新扫描仪(System.in);
系统输出打印(“请使用英尺空间英寸输入高度(例如,5.6.25):”;
inputString=stdIn.nextLine();
尝试
{
parseDouble(inputString);
}
捕获(NumberFormatException nfe)
{
System.out.println(“无效的英寸值。数字必须是十进制。”);
System.out.print(“以英寸为单位重新输入高度:”;
inputString=stdIn.nextLine();
}
英寸=Double.parseDouble(inputString);
如果(英寸您使用so分割,对于单个空间

mystring.split("\\s");
对于任意数量的空格

mystring.split("\\s+");
这两个操作都返回一个字符串数组,然后可以像任何其他数组一样访问该数组,例如

String mystring = "Hello World";
String[] words = mystring.split("\\s");
System.out.println(words[0]); //<--- Would print "Hello"
上面这一行代码将
“1.0”
字符串对象转换为
1.0
的双精度值

Double number = Double.parseDouble("1.0");