首先我回到我知道的增量是不正確的。我不知道該怎麼做。當它遇到需要回調的情況時,我需要增量來使用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