gpt4 book ai didi

java输入验证: how can I split the user input at the space?

转载 作者:行者123 更新时间:2023-11-30 04:48:49 24 4
gpt4 key购买 nike

我的任务:

Perform input validation by making sure that the user enters a positive integer number for feet, a nonnegative decimal number for inches, and a positive decimal number for weight. See the sample session for details. In particular, note the format for the echo-printed height and weight values and for the generated body-mass-index value.

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

示例 session :

Enter height using feet space inches (e.g., 5 6.25): hi there
Invalid feet value. Must be an integer.
Invalid inches value. Must be a decimal number.
Re-enter height using feet space inches (e.g., 5 6.25): 0 9
Invalid feet value. Must be positive.
Re-enter height using feet space inches (e.g., 5 6.25): 5.25 0
Invalid feet value. Must be an integer.
Re-enter height using feet space inches (e.g., 5 6.25): 5 9.25
Enter weight in pounds: 0
Invalid pounds value. Must be positive.
Re-enter weight in pounds: 150.5
height = 5'-9.25"
weight = 150.5 pounds
body mass index = 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
}

最佳答案

您使用 .split 进行拆分所以,对于一个空间...

mystring.split("\\s");

对于任意数量的空格...

mystring.split("\\s+");

这两个操作都会返回一个字符串数组,然后可以像任何其他数组一样访问该数组...

String mystring = "Hello World";
String[] words = mystring.split("\\s");
System.out.println(words[0]); //<--- Would print "Hello"

对于您的特定问题,您可能希望将这些 String 对象转换为 Double 或其他对象。您可以使用 Double.parseDouble() 来完成此操作。例如,

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

上面这行代码将 "1.0" String 对象转换为 double 值 1.0

关于java输入验证: how can I split the user input at the space?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10310014/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com