2013-11-22 42 views
1

我是新來的java。
我必須寫,通過添加一個數字來根據這些條件下,現有的目錄號的左修改現有的目錄(3-5位數字)數到一個新的目錄號的程序:如何通過方法返回一個新號碼

  1. 新數字將是最左邊數字到最右邊數字之間的最大數字。
  2. 如果最左邊的數字等於最右邊的,新的數量將9.

輸入應爲10個數字,然後添加新的號碼。 問題是,現在,「newKatalogNumber」方法獲取舊的目錄號,並返回左邊的數字,我喜歡該方法返回新的目錄代碼。例如,如果方法獲得1234,她將返回41234,並且它將打印在主體的末尾。我還沒有找到辦法做到這一點。 是enybody有線索如何做到這一點? 我將不勝感激。

這是我的代碼:

進口java.util.Scanner的; //這個程序得到3到5位之間的舊目錄號,並通過添加一個新的數字到數字的左側將其更改爲新的目錄編號 //

公共類數 { 公共靜態INT newKatalogNumber (int num)
{ while(num> = 10) { num/= 10; } return num; }

public static void main(String[] args) 
{ 
    Scanner input = new Scanner(System.in); 

    for (int oldNum=0; oldNum<10; oldNum++) 
    { 
     System.out.print("Insert old catalog Number: ");//The user insert the old catalog number 
     int catalogNum = input.nextInt(); 
     int mostRight = catalogNum % 10; 
     int mostLeft = newKatalogNumber(catalogNum); 

     //Adding the new digit according to condition below: 

    if (mostRight>mostLeft) 
    { 
     System.out.println("Old catalog number is:"+catalogNum); 
     System.out.println("New catalog number is"+mostRight+catalogNum); 
    } 
    else if (mostLeft>mostRight) 
    { 
     System.out.println("Old catalog number is:"+catalogNum); 
     System.out.println("New catalog number is"+mostLeft+catalogNum); 
    } 
    else 
    { 
     System.out.println("Old catalog number is:"+catalogNum); 
     System.out.println("New catalog number is"+9+catalogNum); 
    } 
    } 
}    

}

+0

如果您的號碼是3,你想在左邊添加5,你能做到這一點,加入50(50 + 3 = 53)。如果你的電話號碼是33,你可以加500(500 + 33 = 533)。如果您的電話號碼是333,您可以添加5000(5000 + 333 = 5333)。試圖找出如何推廣這一點。 – hankd

+0

我覺得我講了很多人的,當我說我不明白的要求。你能用2或3個手動數字轉換來說明嗎? –

回答

2

如果您有許多像1234,你想在一開始加,讓我們說,一個5那麼你應該做的:

1234 + 5 * 10000 

如果你想在最後加上5你應該這樣做:

1234 * 10 + 5 

注意在第一種情況下,10000中的零數等於原始數字中的位數。

+0

就剩下如何得到的位數的更難的問題。 – dansalmo

+0

1)1234 + 5 * 10000 ---> 12345 – Nizan

+0

不是在我的世界。 – dansalmo

0

試試這個

 public static int NewKatatlogNumber (int num) 
    { 

     int noOfDigitsInTheNumber = num + "".length(); 
     int[] digitsArray = new int[noOfDigitsInTheNumber]; 

     int index = digitsArray.length - 1; 

     //Extract digit by digit and populate the digitarray from right to left. 
     //If your num is 123 your array will [1,2,3] 
     while (num > 0) 
     { 
      int remainder = num % 10; 

      num = num/10; 

      digitsArray[index--] = remainder; 

     } 

     //Find the number to add from the digit array.Apply your logic here 
     int numberToAddToTheLeft = getNumberToAdd(digitsArray); 


     int newNum = numberToAddToTheLeft; 
     //Construct the final token by prepending the digit you identified 
     for (int i = 0; i < digitsArray.length; i++) 
     { 
      int j = digitsArray[i]; 

      newNum = newNum * 10 + i; 

     } 

     return newNum; 
    } 
-1

轉換數爲字符串,然後添加「N」到它的前面。下面是使用DrJava的交互標籤的例子:

> Integer.parseInt("5" + Integer.toString(500)) 
5500 

> Integer.parseInt(Integer.toString(5) + Integer.toString(500)) 
5500 

但還有一個更簡潔(但less efficient)的方式,因爲加入""一個int,將其轉換爲字符串:

> Integer.parseInt(5 + "" + 500) 
5500 

如果您需要處理,你可以做負數:

if (n>0) 
    System.out.println(Integer.parseInt(p + "" + n)); 
else 
    System.out.println(Integer.parseInt("-" + p + "" + -n)); 
+0

謝謝,會嘗試你的建議。 – Nizan

相關問題