Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.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_Wrapper - Fatal编程技术网

Java中的包装器和扫描程序类

Java中的包装器和扫描程序类,java,wrapper,Java,Wrapper,以下是作业说明: For this Question, you will be using two of the prominent wrapper classes to convert String variables which represent numerical data into primitive types as instances of the wrapper classes. As part of this Question you will be

以下是作业说明:

    For this Question, you will be using two of the prominent wrapper classes to convert String variables
    which represent numerical data into primitive types as instances of the wrapper classes. 
    As part of this Question you will be using a Scanner object to read in user input.
    There are also 2 previously declared and instantiated String object references, value1 and value2
    that you will use as part of this exercise. 

    1) Declare an int variable with the identifier parsedInt, and double variable with the identifer parsedDouble
    2) Use a static method of the Integer class to convert the String object reference value1 to an int
        and assign that value to the parsedInt variable. 
    3) Use a static method of the Double class to convert the String object reference value2 to a double
        and assign that value to the parsedDouble variable. 
    4) Declare 2 Double variables with identifiers of your choosing.
    5) Declare an instantiate a Scanner variable (object reference) with the identifier of your choosing
        Make sure that it can read from the Java console (System.in) and that you have imported the Scanner class
    5) Using a static method of the Double class, convert String values read in from the console
        (by calling the nextLine method on your Scanner object reference) to the Double type 
        and assign them to the two Double object references declared in the previous step. 
    6) Declare 2 boolean variables with the identifiers isInfinite and isNaN
    7) Call methods on your Double object references to make the following tests and store the result in the proper variable
        Use a method to test if the first value that you read in from the console is Infinite
        Use a method to test if the second value that you read in from the console is Not a Number (NaN)
    8) Declare an int variable result 
    9) Convert the values of the two Double object references to integers
        and subtract the second number that was read in from the first that was read in
        and assign the result of that mathematical expression to the variable result
我的问题从5号开始。我不太确定我被要求做什么,我不知道如何将字符串值转换为双精度。这就是我到目前为止所做的:

    double woof1, woof2;
    boolean isInfinite, isNaN;
    int result;

    Integer parsedInt = new Integer(value1);
    Double parsedDouble = new Double(value2);

    Scanner scan = new Scanner(System.in);
更新 现在我有了这个

    double woof1, woof2;
    boolean isInfinite, isNaN;
    int result;

    Integer parsedInt = new Integer(value1);
    Double parsedDouble = new Double(value2);

    Scanner scan = new Scanner(System.in);

    woof1 = Integer.parseInt(scan.nextLine());
    woof2 = Double.parseDouble(scan.nextLine());
现在我不知道怎么做第七题。

只要做就行了

double parsedDouble = Double.parseDouble(scan.nextLine());
然而,这是一个愚蠢的问题,因为这样做会更好

double parsedDouble = scan.nextDouble();

首先,您已经获得了扫描仪的参考。这是一个开始,但我建议在声明任何其他数值类型之前使用它

第二,所有的数字包装器,如
Integer
Long
Double
都支持
parse####
方法,该方法包括它们正在解析的基元类型的名称。它们将
字符串
作为形式参数。例如,对于
Integer
包装器,其解析方法是
Integer.parseInt()


第三,从
扫描器
实例读取一行代码就像
scan.nextLine()
一样简单。如果该行包含要解析的数字,并且要解析<代码>双< /代码>,那么请考虑“<代码>双”。我将此作为练习留给读者。

是什么让它更好?我认为它更好,因为它更简洁。但是whateverIt确实更简洁,并且更好地使用了
扫描仪
。如果有这种方法,为什么还要费心用手呢?(编辑:当我说这是一个愚蠢的问题时,我谈论的是作业,而不是OP)所以如果我按照你提到的第一种方式来做,我还需要Double parsedDouble=new Double(value2)吗?请查看我的更新。所以你最近的编辑会更好。。。但是你真的应该去掉value1和value2,因为这两个变量都不存在。它们确实存在,教授已经在其他代码中声明了它们。我倾向于保留这个选项。克里斯汀没有回答“给我密码”的问题。相反,她正在完成每一个需求。未来将有更多的交易要完成。