2016-11-20 431 views
1

我遇到任務問題。我必須將下面的doWhile循環轉換爲While循環,以便兩個循環的結果是相同的? 正如你所看到的Integer類型只是在doWhile循環中。但類型int有什麼區別。我如何解決這個任務?在While循環中變換doWhile

doWhile循環:

public Integer a_doWhile(int x){ 
    int i = 0; 
    Integer result; 
    do { 
     result = ++i*i; 
    } while (result < x); 
    return result; 
} 

我的解決辦法:

public Integer a_while(int x){ 
    int i=0; 
    int result; 
    while(result < x){ 
     result = ++i*i; 
    return result; 
    } 
} 

但我的解決辦法是錯誤的。它必須做這個「整數」,有人可以幫我嗎?謝謝:)

回答

0

int和Integer是同一事物的不同表示。 Int是原始的,而Integer是對象表示。這是這裏What is the difference between an int and an Integer in Java and C#?

討論您可以轉換您dowhile,同時這樣

public Integer a_While(int x){ 
    int i = 0; 
    Integer result = ++i*i; 
    while (result < x) { 
     result = ++i*i; 
    } 
    return result; 
+0

工作就像一個魅力。謝謝你,Leadfoot! –

+0

不要忘記標記爲答案;) – Leadfoot