java - Java 中的包装器和扫描器类

标签 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 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 。这是我到目前为止所拥有的:

    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());

现在我不知道如何做第 7 点。

最佳答案

首先 - 您已经获得了扫描仪的引用信息。这是一个开始,但我建议您在声明任何其他数字类型之前先进行它。

第二 - 所有数字包装器,例如 IntegerLongDouble 都支持 parse### 方法,其中包括它们正在解析的原始类型的名称。他们将 String 作为正式参数。例如,对于 Integer 包装器,其解析方法是 Integer.parseInt()

第三,从 Scanner 实例读取一行就像 scan.nextLine() 一样简单。如果该行包含您要解析的数字,并且您想要解析 Double,那么请考虑 Double.parse### 实际上是什么。我将其作为练习留给读者。

关于java - Java 中的包装器和扫描器类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25965675/

相关文章:

java - 在 webdriver 2.21 和 mozilla11 中处理警报

java - 使用 GGTS 创建 Grails 项目时出错

java - java中如何获取token

java - 为什么当将 hashmap 传输到 treemap 时,具有重复值的不同键会消失

java - 将时间对象持久化为实体而不是值类型?

java套接字和DoS

css - 关键包装问题

java - Servlet 响应包装器存在编码问题

jquery - 从 Ajax 响应中删除元素

python - 如何编写更新关键字参数的 python 装饰器?