2016-11-20 15 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; 
    } 
} 

しかし、私の解決策が間違っています。これは "整数"とsthをしなければならない、誰かが私を喜ばせることができますか?ありがとう:)

答えて

0

intと整数は同じものの表現が異なります。 Integerはオブジェクト表現ですが、Intはプリミティブです。これは、あなたがこの

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

のような魅力のように働いている間にdowhileあなたを変換することができ、ここでWhat is the difference between an int and an Integer in Java and C#?

議論されています。リード足をありがとうございました! –

+0

答えとしてマークすることを忘れないでください;) – Leadfoot

関連する問題