2011-04-19 87 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