2017-02-06 29 views
0

編寫一個extractOddDigits()函數,它從正數n中提取奇數位,並且 將奇數位依次組合到一個新數字中。新號碼返回到 呼叫方法。如果輸入數字n不包含任何奇數,則返回-1。 例如,如果 n = 1234567,則返回1357;如果n = 28,則返回-1。提取奇數數字的Java方法

測試用例:

(1) n : 12345; 
(2) n : 54123; 
(3) n : 246; 
(4) n : -12 (give an error message) 

預期成果:

(1) oddDigits = 135; 
(2) oddDigits = 513; 
(3) oddDigits = -1; 
(4) oddDigits = Error input!! 

這是我做的方法,但輸出

(1) oddDigits = 135; 
(2) oddDigits = 513; 
(3) oddDigits = -1; 
(4) oddDigits = -1 

最後輸出應該是錯誤輸入!

public static long extractOddDigits(long n){ 


String output = ""; 
if(n < 0) // check if negative 
{ 
    output = "Error Input!!"; 

} 
if(n % 2 == 0){ //check even 

    output = "-1"; 
} 

while(n > 0) { 
    int left = (int) (n % 10); 
    if(left % 2 != 0)  
    output = left + output; 
    n /= 10; 
} 
System.out.println("oddDigit = " + output); 
} 

如何檢查n是否爲負數,然後進入錯誤輸入?

+0

這是功課?作爲一個提示,雖然你可以單獨使用整數,但使用'Integer.toString()'將數字轉換爲字符串,收集奇數並使用'Integer.parseInt()'獲取結果可能更容易。 –

+0

那麼,這是便宜的,如果這是消極返回一個錯誤,這將是的'&'這裏 – AxelH

回答

0

的問題是:

n=-12,要指定outputError Input!!

,然後因爲還n%2==0,要覆蓋output爲 「-1

另外,你的邏輯 - 如果n可以被2整除,不會有奇數,不正確

+3

解釋你的代碼,因爲這只是4letters不同的一個很好的使用;) – AxelH

+0

downvote有'N'幸福的沒問題甚至。 –

+0

這個答案完全沒用,我想知道是誰投了票。 –

1

你的問題是,第一個tw o if可能會分配一個值來輸出,但不是以一種排他的方式,您可以找到一個數字,它是負的,也是偶數,它包含奇數。所以你必須確保你的代碼隔離每個選項。

此外,您的需求告訴我們的方法必須返回數量,不只是打印出來,讓你的方法應該返回long而不是void,但我會離開,作爲你的功課的一部分;)

public static void extractOddDigits(long n){ 

    String output = ""; 
    if(n < 0) { 
    output = "Error Input!!"; 

    } else { 

    while(n > 0) { 
     int left = (int) (n % 10); 
     if (left % 2 != 0) { 
     output = left + output; 
     } 
     n /= 10; 
    } 
    } 
    if (output.equals("") { 
    output="-1"; 
    } 
    System.out.println("oddDigit = " + output); 
} 
+0

的「即使覈對」是沒有要求。(沒有downvote,雖然)。 – Fildor

+1

'N'是偶數並不意味着它不包含奇數字。 –

+0

的測試用例不完備,但OP說'提取從奇數位一個正數n'確實如此,這個'n%2 == 0'沒有必要,也是錯的。(我也沒有-1)):編輯:+1;) – AxelH

0

此代碼檢查所有數字是否包含任何偶數。

public static void extractOddDigits(long n){   
     String output = "";  
     if(n < 0) // check if negative 
     { 
      output = "Error Input!!"; 
     } 
     else {  
      //Iterate through the digits 
      while(n>0){ 
       long lastDigit = n%10; 
       if(lastDigit % 2 != 0) { //for odd digit 
        output = lastDigit+output; 
       } 
       n = n/10; 
      } 
     } 

     //If no odd digits 
     if(output.equals("")) { 
      output = "-1"; 
     } 
     System.out.println(output);  
    } 
-1

如果您不希望方法來運行進一步如果n < 0,然後,

if(n < 0) // check if negative 
{ 
    output = "Error Input!!"; 
    System.out.println(output); 
    return; 

} 
+0

'返回;'從函數返回,不打印錯誤''輸入!你確定你有什麼建議或想要編輯之前有人downvotes你。 –

+2

對不起!我打算在那裏寫一份印刷聲明。讓我編輯。 – Amita

0

首先只是因爲數是偶數,這並不意味着它不包含任何奇數。

所以:

if(n % 2 == 0){ //check even 

    output = "-1"; 
} 

是錯誤的。你也不需要鍵入(n % 10); int。

您可以保留一個布爾變量foundOdd,它可以跟蹤到目前爲止我們是否找到任何奇數,最後如果保持爲假,我們可以簡單地打印-1。

所以正確的代碼是:

int n = 246; 
String output=""; 
boolean foundOdd=false; 
if(n < 0) // check if negative 
    output = "Error Input!!";  
else 
{ 
    while(n > 0) 
    { 
     int left = (n % 10); 
     if(left % 2 != 0)  
     {output = left + output; foundOdd = true;} 
     n /= 10; 
    } 
} 
if(foundOdd) 
    System.out.println("oddDigit = " + output); 
else System.out.println(-1);