2011-04-19 85 views
1

首先我回到我知道的增量是不正確的。我不知道該怎麼做。當它遇到需要回調的情況時,我需要增量來使用temp。我無法更改增量以將參數傳遞給它,因爲分級程序測試腳本不會允許它。第二個問題是它不會增加任何輸入。舉例來說,如果你只是呼籲23號增量它只返回23.平地機的測試腳本看起來是這樣的:數字不會遞增

public class TestBigNaturalSimple { 

public static void main(String[] args) { 
    BigNatural b1 = new BigNatural(); // default constructor 
    BigNatural b2 = new BigNatural(23); // one-argument int constructor 
    BigNatural b3 = new BigNatural("346"); // one-argument String constructor 
    BigNatural b4 = new BigNatural(b2); // one-argument BigNatural 
             // constructor 

    b1.increment(); 
    b3.decrement(); 

    System.out.println(b1.toString()); // should print out 1 
    System.out.println(b4.toString()); // should print out 23 
} 
} 

我的代碼是:

public class BigNatural { 

private String num; 

public BigNatural(String input) { 
    num = input; 
} 


public BigNatural(BigNatural input) { 
    num = input.toString(); 
} 

public BigNatural(Integer input) { 
    num = input.toString(); 
} 

public BigNatural() { 
    Integer i = 0; 
    num = i.toString(); 
} 

public void increment() { 
    Integer first = 0; 
    Character ch = num.charAt(num.length()-1); 
    Integer last = Character.digit(ch, 10); 

    if (num.length() > 1) 
    { 
     if (last < 9) { 
      last++; 
     } 
     else 
      { 
      if (num.length() >= 2) 
      { 
       last = 0; 
       String temp = new String(num.substring(0, num.length()-2)); 
       increment(); 
      } 
      else 
      { 
       last++; 
      } 
     } 
    } 
    else 
    { 
     if (last < 9) 
     { 
      last++; 
     } 
     else 
     { 
      last = 0; 
      first = 1; 
     } 
    } 

    String t = last.toString(); 


    if (first > 0) 
    { 
    String x = first.toString(); 
    num.concat(x); 
    } 

    num.concat(t); 
} 

public void decrement() { 
    Character ch = num.charAt(num.length()-1); 
    Integer last = Character.digit(ch, 10); 

    if(num.length() > 1) 
    { 
     if(last == 0) 
     { 
      String temp = new String(num.substring(0, num.length()-2)); 
      decrement(); 
     } 
     else 
     { 
     last--; 
     } 
    } 
    else 
    { 
     if(last > 0) 
     { 
      last--; 
     } 
     else 
     { 
      last = 0; 
     } 
    } 

    String t = last.toString(); 
    num.concat(t); 
} 


public String toString() { 
    return num; 
} 
} 

回答

3

這已是最複雜的方式來增加我見過的數字。 ;)我假設你必須這樣做。

從我看到你不要改變num任何地方。如果你使用了一個調試器,我希望這一點很明顯。 ;)

如果您希望num更改,請嘗試使用num = num.concat(t)

注:字符串是不可變的,所以你不能改變它,你只能取代它。

編輯:這是爲您自己的興趣提供的版本。你的教授會知道你沒有寫這個,所以不要複製它。 ;)

public void increment() { 
    num = increment(num); 
} 
private static String increment(String s) { 
    if (s.length() <= 0) return "1"; 
    char ch = s.charAt(s.length() - 1); 
    String top = s.substring(0, s.length() - 1); 
    return ch < '9' ? top + ++ch : increment(top) + '0'; 
} 
+0

您知道這是一個漫長的夜晚,當你想念那個。我們現在正在某處。我遇到的唯一的另一個大問題是如何在基本情況> 9時調用temp的增量。 – Bigby 2011-04-19 09:19:48

+0

看起來您正在嘗試使用recusrion。爲此,您需要將所有參數作爲參數傳遞並返回結果。這是你想要達到的目標嗎? – 2011-04-19 09:21:46

+0

是的,原來我有temp.increment(),以便它會調用增量的臨時工,但我很快發現這是行不通的。 – Bigby 2011-04-19 09:23:52

1

字符串在Java中是不可變的。因此,代碼

num.concat(t); 

在您的增量方法將不會做你的期望。

1

首先,應用該規則不要重複自己

  • 遞增是增加1
  • 遞減是增加-1

因此,你只需要簡單地在功能上寫上一個數字作爲輸入並將其添加到BigNatural中:

public void increment() { 
    add(1); 
} 

public void decrement() { 
    add(-1); 
} 

private void add(int i) { 
    // Your homework here ... 
    // You will have only one function to debug and correct, not 2 
} 

第二:正如其他答案指出的,num.concat(t);不符合你的期望,你需要num = num.concat(t);。當您使用您不知道的功能時,請始終參閱Java文檔。如果你沒有一個編輯器允許你調試你的程序,我強烈建議你得到一個:例如Eclipse,但其他編輯器可能會更好的學習工具。額外的好處是,這些工具將爲您設置代碼格式,警告您很多錯誤,...

+1

+0:鑑於增量執行的複雜方式,我不認爲這是問題,我不認爲實施add會有所幫助。 ;) – 2011-04-19 09:19:10