Java 如何使此子例程能够处理double而不仅仅是int?

Java 如何使此子例程能够处理double而不仅仅是int?,java,Java,我必须使这个子程序能够处理double,而不仅仅是int。我是新来的编码,真的不明白这一点 下面是子程序: public static int readNumber() throws Exception { int number = 0; char characterAsciiCode = '0'; int numberValue = 0; characterAsciiCode = (char)System.in.read(); while ( char

我必须使这个子程序能够处理double,而不仅仅是int。我是新来的编码,真的不明白这一点

下面是子程序:

public static int readNumber() throws Exception 
{
    int number = 0;
    char characterAsciiCode = '0';
    int numberValue = 0;
    characterAsciiCode = (char)System.in.read();
    while ( characterAsciiCode != '\n')
    {
        //convert the character code to an actual numeric value
        numberValue = characterAsciiCode - '0';
        //integrate the numeric digit into the total number
        number = number * 10 + numberValue;
        //get the next character from the keyboard buffer
        characterAsciiCode = (char)System.in.read();
    }        
    return number;
}

非常感谢您的帮助。

因此,假设您应该在不使用nextInt()或nextDouble()的情况下自己构造数字,您可以扩展现有的逻辑,如下所示: 1) 如果在a.之前遇到新行,只需返回到目前为止找到的数字的两倍。 2) 如果你遇到这个问题。(我在这里以美国为中心,如果你住的地方使用逗号,则相应地进行调整)首先,保存数字,使用与以前相同的例程(以新行终止)将小数部分累积为整数,但跟踪小数部分的长度。 3) 返回数字部分加上小数部分除以10^decimalLength。 这是密码。它可能有一两个不必要的石膏。我当时很谨慎

public static double readNumber() throws Exception {
 int number = 0;
 char characterAsciiCode = '0';
 int numberValue = 0;

 characterAsciiCode = (char) System.in.read();

 while ((characterAsciiCode != '\n') && (characterAsciiCode != '.')) {
     //convert the character code to an actual numeric value
     numberValue = characterAsciiCode - '0';
     //integrate the numeric digit into the total number
     number = number * 10 + numberValue;
     //get the next character from the keyboard buffer
     characterAsciiCode = (char) System.in.read();
 }
 if (characterAsciiCode == '\n') {
     return (double) number;
 }
 int decimal = 0;
 int decimalLen = 0;
 characterAsciiCode = (char) System.in.read();
 while (characterAsciiCode != '\n') {
     //convert the character code to an actual numeric value
     numberValue = characterAsciiCode - '0';
     //integrate the numeric digit into the total number
     decimal = decimal * 10 + numberValue;
     decimalLen++;
     //get the next character from the keyboard buffer
     characterAsciiCode = (char) System.in.read();
 }
 return (double) number + (double) decimal / Math.pow(10, decimalLen);

 }

你不能用扫描仪吗?比如“Scanner s=new Scanner(System.in);double n=s.nextDouble();”我不明白为什么不熟悉编码的人必须这样做。这真的不是一个初学者友好的问题,如果您只需要从用户输入中读取数字(整数或双精度),这不是一个好方法。