首先我回到我知道的增量是不正确的。我不知道该怎么做。当它遇到需要回调的情况时,我需要增量来使用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;
}
}
您知道这是一个漫长的夜晚,当你想念那个。我们现在正在某处。我遇到的唯一的另一个大问题是如何在基本情况> 9时调用temp的增量。 – Bigby 2011-04-19 09:19:48
看起来您正在尝试使用recusrion。为此,您需要将所有参数作为参数传递并返回结果。这是你想要达到的目标吗? – 2011-04-19 09:21:46
是的,原来我有temp.increment(),以便它会调用增量的临时工,但我很快发现这是行不通的。 – Bigby 2011-04-19 09:23:52