如何将用户输入的值存储为双精度(double)变量?

huangapple go评论109阅读模式
英文:

How to store a value in variable as a double from user input?

问题

我刚开始学习Java编程,但是我无法弄清楚如何将用户输入的值存储为double类型。

以下是我的代码:

package FirstProject;

import java.util.Scanner;

public class TripPlanner {
    
    public static void main() {
        Scanner console = new Scanner(System.in);
        System.out.print("在1美元中有多少" + currencySymbol + "? ");
        double conversion = console.nextDouble();
    }
}

我不明白为什么当我输入例如19.5时,它会给我一个异常错误。我理解我将conversion变量定义为double类型(double conversion),并且我期望用户输入一个double类型(console.nextDouble();)。这是很基础的问题,但是作为从Python转过来的,我无法理解我做错了什么。

英文:

I've only just started learning programming in Java and I cannot figure out how to store a value from user input as a double.

Here's my code:

    package FirstProject;
    
       import java.util.Scanner;
    
       public class TripPlanner {
           
           public static void main () {
           Scanner console = new Scanner(System.in);
           System.out.print("How many " + currencySymbol + " are there in 1 USD? ");
           double conversion = console.nextDouble();
	       }
       }

I don't understand why when I enter, for example 19.5, it gives me exception error. My understanding is that I defined the conversion variable as double (double conversion) and I expect the user to enter a double (console.nextDouble();). It's fundamental but coming from Python I can't understand what I'm doing wrong.

答案1

得分: 2

使用像您所做的这样的扫描器有点不受控制。例如,扫描器期望的格式是与区域设置相关的。如果您的系统设置为德语,那么您需要输入3,7来表示3.7的双精度值。

如果输入的字符串格式无效,那么可能会引发异常,通常是java.util.InputMismatchException。您应该捕获异常并以适合您的用例的方式做出反应。

为了更好地控制所使用的区域设置,您应该从控制台中读取一个字符串,然后例如使用java.text.NumberFormat。请参考此问题

英文:

Using the Scanner like you did is somewhat uncontrolled. For example, the format the scanner expects is locale-dependent. If your system settings are German, for example, then you need to enter 3,7 for a double value of 3.7.

If the format of the entered string is invalid, then an exception will be thrown, probably java.util.InputMismatchException. You should catch the exception and react in a way that's fit to you use case.

To get better control over the locale used, you should instead read a string from the console and then for example use a java.text.NumberFormat.
See this question.

答案2

得分: 1

@Leisetreter指导我朝着正确的方向。问题出在键盘设置上。我居住在英国,习惯使用点号来表示十进制数。然而,我的键盘设置为波兰程序员模式。在波兰,十进制数使用逗号","来表示。一旦我更改了键盘设置,这个问题就不再存在。我自己可能永远想不到这一点。感谢@Leisetreter。

英文:

@Leisetreter pointed me in the right direction. It was the keyboard settings. I live in the UK and use the conventions of writing a decimal number with a dot. However, my keyboard was set to Polish programmer's. In Poland decimal numbers are written with a ",". Once I changed the settings I did not have that problem anymore. It would have never occurred to me. Thanks @Leisetreter.

答案3

得分: 0

我在你的代码中没有看到任何错误。除了 main 方法。
你应该这样调用她:public static void main (String args[])

英文:

I don't see any mistake in your code. Except from main.
The proper way you should call her is public static void main (String args[])

huangapple
  • 本文由 发表于 2020年9月5日 16:07:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63751753.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定