从字符串中取不同的值,并将其转换为双精度值 [英] Take different values from a String and convert them to Double Values
本文介绍了从字符串中取不同的值,并将其转换为双精度值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的code,我要求用户通过一个空格分割的输入三个不同的值。
然后,这三个不同的价值观,我想将它们分配给三种不同的双击变量。
我已经通过分配字符串的第一个字符到我的double变量试过,但我一直没能成功。
有什么建议?
感谢您!
下面是我想要做的:
INT决策=消息();
串newDimensions;
双newHeight,newWidth,满足newLength;如果(判断== 1){
newDimensions = JOptionPane.showInputDialog(请输入要添加所需量的+
\\ n要每个维度。 +
\\ n注意:一是值是高度,第二个用于宽度,第三个用于长度+
\\ n并,输入信息必须具有各值之间的空白空间。 +
\\ n例如:4 8 9);newHeight = Double.parseDouble(newDimensions.charAt(0));
解决方案
获取输入,用空格分开,并分析每个双人间。这code没有消毒的输入。
字符串输入=12.4 19.8776 23.3445
的String [] =拆分input.split();
对于(一个String:分)
{
的System.out.println(Double.parseDouble(S));
}
In my code, I'm asking the user to input three different values divided by a blank space. Then, those three different values I would like to assign them to three different Double variables.
I have tried by assigning the first character of such string to my double variables but I haven't been able to succeed.
Any suggestions?
Thank you!
Here is what I'm trying to do:
int decision = message();
String newDimensions;
double newHeight, newWidth, newLength;
if(decision == 1){
newDimensions = JOptionPane.showInputDialog("Please enter the desired amount to be added" +
"\nto each dimension." +
"\nNOTE: First value is for Height, second for Width, third for Length" +
"\nAlso, input information has to have a blank space between each value." +
"\nEXAMPLE: 4 8 9");
newHeight = Double.parseDouble(newDimensions.charAt(0));
解决方案
Get the input, split it by a whitespace and parse each Double. This code does not sanitize the input.
String input = "12.4 19.8776 23.3445";
String[] split = input.split(" ");
for(String s : split)
{
System.out.println(Double.parseDouble(s));
}
这篇关于从字符串中取不同的值,并将其转换为双精度值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文