Java(toString和构造函数)
嗨,我在我的一个编程项目中遇到了困难(我不是在要求代码,我只是在程序逻辑上有问题)。任务是创建一个程序,在这个程序中,你可以输入一个整数的字符串表示形式,例如:“1363”,将它转换成一个整数,将它保存在数组或数组列表中,做一些基本的操作,如加法、减法、乘法等。。。然后使用toString方法以字符串格式重写结果。嗯,我在让toString方法正常工作时遇到了问题。例如,当我进行输入检查时,我创建了类的两个对象,因为本质上应该有两个大的整数,结果是错误的,我不知道为什么。例如,每当字符串中有字母而不是数字时,我就会停止程序,这是正确的,但只有当第一个对象有字母时才有效,如果第二个对象有字母,它甚至不会打印第一个对象的结果,而是为第二个对象写出错误消息。我不知道如何做,它对第一个对象进行错误检查,然后对第二个对象进行错误检查,而不是在第二个对象有错误时搞砸程序。这是到目前为止我的代码,我的教授给我们上了一堂主要的课来测试我们的toString方法,我会把这两种方法都发布出来,非常感谢您的帮助Java(toString和构造函数),java,Java,嗨,我在我的一个编程项目中遇到了困难(我不是在要求代码,我只是在程序逻辑上有问题)。任务是创建一个程序,在这个程序中,你可以输入一个整数的字符串表示形式,例如:“1363”,将它转换成一个整数,将它保存在数组或数组列表中,做一些基本的操作,如加法、减法、乘法等。。。然后使用toString方法以字符串格式重写结果。嗯,我在让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);
}
}
import java.util.ArrayList;
公共类BigInt{
//私有静态最终字符串=null;
private ArrayList integerArray;//只需要一个数组
//必须修复toString和构造函数
私人字符符号;
//私有对象bigNum;
//公共最终int-bigNum;
公共BigInt(字符符号)
{
//this.bigNum=bigNum;
这个符号=符号;
//this.sign='+';
this.integerArray=新的ArrayList();
}
公共BigInt(字符串输入)
{
设置信号(输入);
setInt(输入);
//addTwoBigNum(输入);
}
公共字符getSign()
{
返回标志;
}
公共无效设置符号(字符串输入)
{
如果(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='-';
}
其他的
{
System.out.println(“无效符号”);
}
}
公共void setInt(字符串输入)
{
this.integerArray=新的ArrayList();
对于(int i=input.length()-1;i>=0;i--)
{
如果(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'
||输入.字符(0)='0')
{
add(新整数(input.charAt(i)-48));
}
else if(input.charAt(i)='+'| | input.charAt(i)='-'&&i==0)
{
打破
}
else if(input.charAt(i)='+'| | input.charAt(i)='-'&&input.length()==1)
{
System.out.println(“输入必须至少包含一个数字”);
}
其他的
{
System.out.println(“这是字符串中的无效字符”);
integerArray.clear();
系统出口(0);
打破
}
}
}
@凌驾
公共字符串toString()
{
字符串输出=”;
if(this.sign='-'&&this.integerArray.size()>0)
{
输出=“-”;
}
对于(int i=this.integerArray.size()-1;i>=0;i--)
{
输出=输出+整数数组.get(i);
}
if(this.integerArray.size()==0)
{
output=“无整数”;
}
返回输出;
}
}
公共类BigIntDemo{
公共静态void main(字符串参数[]){
BigInt B1=新的BigInt(“1234”);
//BigInt B1=新的BigInt(“1234asd”);
//BigInt B1=新的BigInt(“-1234”);
//BigInt B1=新的BigInt(“-1”);
//BigInt B1=新的BigInt(“1”);
//BigInt B1=新的BigInt(“0”);
//BigInt B1=新的BigInt(“-0”);
//BigInt B1=新的BigInt(“1222222222333333333333333334444434”);
//BigInt B2=新的BigInt(“4567”);
BigInt B2=新的BigInt(“-45”);
//BigInt B3=新的BigInt();
//BigInt B4=新的BigInt();
System.out.println(“显式BigInt值为“+B1”);
System.out.println(“默认的BigInt值为“+B2”);
//System.out.println(“两个大整数之和为”+B1.add(B2));
//System.out.println(“请输入一个新的大整数。”);
//请注意,readBigInt的代码几乎与构造函数相同
//B1.readBigInt();
系统输出打印(“新的BigInt值为”+B1);
}
}
尝试删除
System.exit(0);
如果对第一个对象执行此操作,您的程序将永远无法到达之后的对象。我不明白为什么人们一直在对我的帖子进行投票……在您的主要方法中,您从未调用toString()@Rocketsm46只是一个猜测,而没有阅读整个问题:格式化您的问题。文字问题堆积如山,难以阅读。另外,代码太多,请尝试从该部分中获取与问题相关的片段。@Zhedar下次我会记住的。复制,谢谢您的反馈。我只有一个问题,即使在我将system.exit(0)放入代码之前,它仍然不会完全完成对第一个对象的检查