您好我的一个编程项目有困难(我不是要求代码,我只是对程序的逻辑有问题)。赋值是创建一个程序,在其中接受int的String表示形式,例如:“1363”,将其转换为int,将其保存在数组或数组列表中,执行基本操作,如加,减,乘等。然后使用字符串格式的toString方法重写结果。好吧,我有问题只是让toString方法正常工作。例如,当我输入检查时,我创建了该类的两个对象,因为基本上应该有两个大的int数字,结果出错了,我无法弄清楚为什么。例如,每当你在字符串中有字母而不是正确的数字但我只在第一个对象有字母时才有效,如果第二个对象有一个字母,它甚至不打印结果为第一个对象,但写出第二个对象的错误消息。我不知道如何以对第一个对象进行错误检查的方式进行操作,然后继续对第二个对象进行错误检查,而不是在第二个对象出错时弄乱程序。这是我的代码到目前为止,我的教授为我们提供了一个主要课程,供我们测试我们的toString方法,我将发布两个,任何帮助表示赞赏。
import java.util.ArrayList;
public class BigInt {
//private static final String String = null;
private ArrayList <Integer> integerArray; // only need one array
// have to fix the toString and constructors
private char sign;
//private Object bigNum;
//public final int bigNum;
public BigInt(char sign)
{
//this.bigNum = bigNum;
this.sign = sign;
//this.sign = '+';
this.integerArray = new ArrayList<Integer>();
}
public BigInt (String input)
{
setSign(input);
setInt(input);
//addTwoBigNum(input);
}
public char getSign ()
{
return sign;
}
public void setSign (String input)
{
if (input.charAt(0) == '+' || input.charAt(0) == '1' || input.charAt(0) == '2'
|| input.charAt(0) == '3' || input.charAt(0) == '4' || input.charAt(0) == '5'
|| input.charAt(0) == '6' || input.charAt(0) == '7' || input.charAt(0) == '8'
|| input.charAt(0) == '9' || input.charAt(0) == '0')
{
this.sign ='+';
}
else if (input.charAt(0) == '-')
{
this.sign ='-';
}
else
{
System.out.println("Invalid sign");
}
}
public void setInt (String input)
{
this.integerArray = new ArrayList<Integer>();
for (int i= input.length() -1; i>=0; i--)
{
if (input.charAt(0) == '1' || input.charAt(0) == '2' || input.charAt(0) == '3'
|| input.charAt(0) == '4' || input.charAt(0) == '4' || input.charAt(0) == '6'
|| input.charAt(0) == '7' || input.charAt(0) == '7' || input.charAt(0) == '9'
|| input.charAt(0) == '0')
{
integerArray.add(new Integer(input.charAt(i) -48));
}
else if (input.charAt(i) == '+' || input.charAt(i) == '-' && i== 0)
{
break;
}
else if (input.charAt(i) == '+' || input.charAt(i) == '-' && input.length() ==1)
{
System.out.println("Input must contain ar least one digit");
}
else
{
System.out.println ("This is an invalid character in the string");
integerArray.clear();
System.exit(0);
break;
}
}
}
@Override
public String toString()
{
String output = "";
if (this.sign =='-' && this.integerArray.size() > 0 )
{
output = "-";
}
for (int i = this.integerArray.size() -1; i>=0 ; i--)
{
output = output + integerArray.get(i);
}
if(this.integerArray.size() == 0 )
{
output = "no integer";
}
return output;
}
}
public class BigIntDemo {
public static void main (String args [] ) {
BigInt B1 = new BigInt("1234");
//BigInt B1 = new BigInt("1234asd");
//BigInt B1 = new BigInt("-1234");
//BigInt B1 = new BigInt("-1");
//BigInt B1 = new BigInt("1");
//BigInt B1 = new BigInt("0");
// BigInt B1 = new BigInt("-0");
//BigInt B1 = new BigInt("12222222222222233333333333333344444434");
//BigInt B2 = new BigInt("4567");
BigInt B2 = new BigInt("-45");
// BigInt B3 = new BigInt( );
// BigInt B4 = new BigInt( );
System.out.println("explicit BigInt value is " + B1);
System.out.println("default BigInt value is " + B2);
//System.out.println("The sum of the two big ints is " + B1.add(B2));
//System.out.println("please enter a new big integer value. ");
//note that the code for readBigInt is almost identical to constructor
//B1.readBigInt();
System.out.print("new BigInt value is " + B1);
}
}
答案 0 :(得分:0)
尝试删除
System.exit(0);
如果对第一个对象执行此操作,则程序将永远不会到达该对象。