2014-09-22 112 views
0

這些是分配的說明:包裝和掃描儀類Java中

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

,現在我不知道該怎麼辦7號

+0

我傾向於將此打開。克里斯汀沒有拋出一個「給我代碼」的問題。相反,她正在努力完成每項要求。未來還有很多事情要結束。 – jww 2014-09-22 04:27:44

回答

0

首先 - 你已經得到了掃描儀的參考。這是一個開始,但我建議您在聲明任何其他數字類型之前。

二 - 所有的數值包裝,如IntegerLong,和Double支持parse###方法,包括他們正在解析基本類型的名稱。他們以String作爲他們的正式論點。例如,對於Integer包裝器,其解析方法是Integer.parseInt()

第三,從Scanner實例中讀取一條線就像scan.nextLine()一樣簡單。如果該行包含要解析的數字,並且您想解析Double,請考慮實際上是什麼Double.parse###。我把這個作爲練習留給讀者。

+0

請參閱我的更新。 – 2014-09-22 02:06:11

+0

所以你最近的編輯有更好的...但是你真的應該擺脫value1和value2,因爲這兩個變量都不存在。 – Makoto 2014-09-22 02:06:44

+0

它們確實存在,它們已經被教授在其他一些代碼中聲明瞭。 – 2014-09-22 02:11:51

0

根本就

double parsedDouble = Double.parseDouble(scan.nextLine()); 

但是,這是一個愚蠢的問題b因爲它會更好做

double parsedDouble = scan.nextDouble(); 
+1

什麼讓這更好? – Makoto 2014-09-22 01:46:16

+0

我會認爲它更好,因爲它更簡潔。但無論如何 – 2014-09-22 01:47:01

+1

它確實更簡潔,並更好地使用'掃描儀'。如果這種方法存在,爲什麼要用手來干擾呢? (編輯:當我說這是一個愚蠢的問題,我談論這項任務,而不是OP) – Dici 2014-09-22 01:48:51