创建一个 toString 方法 returns java 中的数组
Creating a toString method that returns an array in java
我最近被要求编写一个 toString
方法,其中 return 是数组中的一个数字,如下所示:
after every 3 digits from the right-hand side of the number it should
add a comma , but if the digits of the number were 3 or less it
doesn't add a comma.
但我遇到了一个问题:该方法总是 return 值 0,
。如何将代码调整为 return 正确的格式?
public class BigNum {
int[] num;
final int MAX_DIGITS = 50;
public BigNum() {
this.num = new int[MAX_DIGITS];
this.num[0] = 0;
for(int i=1 ; i<this.num.length ; i++)
this.num[i] = -1;
}
public BigNum(long n) {
int number = (int)n;
this.num = new int[MAX_DIGITS];
for (int i = 0; i < this.num.length; i++) {
num[i] = number % 10;
number /= 10;
}
}
public String toString(){
String toReturn = "";
this.num = new int[MAX_DIGITS];
for(int i=0 ; i<this.num.length ; i++)
if(this.num.length>=1 && this.num.length<=3)
toReturn = num[i] + "";
for(int j=0 ; j<this.num.length ; j+=3)
toReturn = num[j] + "," ;
return toReturn;
}
您可以试试下面的代码。请记住复制 BigNum 构造函数。对以下代码进行了以下更改:
创建实例数组长度等于输入中的位数而不等于 MAX_DIGITS。
更改了 toString 方法。
public BigNum(long n) {
int number = (int)n;
int[] tempNum = new int[MAX_DIGITS];
int counter=0;
while(number>0) {
tempNum[counter] = number % 10;
number /= 10;
counter++;
}
this.num = Arrays.copyOfRange(tempNum, 0, counter);
}
public String toString(){
String toReturn = "";
if(this.num.length>=1 && this.num.length<=3) {
for(int i=this.num.length-1 ; i>=0 ; i--) {
toReturn += num[i];
}
}else {
int commaPos = this.num.length%3==0?3:this.num.length%3;
int counter=0;
while(counter<this.num.length) {
if(counter==commaPos) {
toReturn+=",";
commaPos+=3;
}
toReturn+=num[this.num.length-1-counter]
counter++;
}
}
return toReturn;
}
我使用以下代码测试了上面的内容:
public static void main(String[] args) {
BigNum bn = new BigNum(1234567);
System.out.println(bn);
}
输出:1,234,567
我最近被要求编写一个 toString
方法,其中 return 是数组中的一个数字,如下所示:
after every 3 digits from the right-hand side of the number it should add a comma , but if the digits of the number were 3 or less it doesn't add a comma.
但我遇到了一个问题:该方法总是 return 值 0,
。如何将代码调整为 return 正确的格式?
public class BigNum {
int[] num;
final int MAX_DIGITS = 50;
public BigNum() {
this.num = new int[MAX_DIGITS];
this.num[0] = 0;
for(int i=1 ; i<this.num.length ; i++)
this.num[i] = -1;
}
public BigNum(long n) {
int number = (int)n;
this.num = new int[MAX_DIGITS];
for (int i = 0; i < this.num.length; i++) {
num[i] = number % 10;
number /= 10;
}
}
public String toString(){
String toReturn = "";
this.num = new int[MAX_DIGITS];
for(int i=0 ; i<this.num.length ; i++)
if(this.num.length>=1 && this.num.length<=3)
toReturn = num[i] + "";
for(int j=0 ; j<this.num.length ; j+=3)
toReturn = num[j] + "," ;
return toReturn;
}
您可以试试下面的代码。请记住复制 BigNum 构造函数。对以下代码进行了以下更改:
创建实例数组长度等于输入中的位数而不等于 MAX_DIGITS。
更改了 toString 方法。
public BigNum(long n) { int number = (int)n; int[] tempNum = new int[MAX_DIGITS]; int counter=0; while(number>0) { tempNum[counter] = number % 10; number /= 10; counter++; } this.num = Arrays.copyOfRange(tempNum, 0, counter); } public String toString(){ String toReturn = ""; if(this.num.length>=1 && this.num.length<=3) { for(int i=this.num.length-1 ; i>=0 ; i--) { toReturn += num[i]; } }else { int commaPos = this.num.length%3==0?3:this.num.length%3; int counter=0; while(counter<this.num.length) { if(counter==commaPos) { toReturn+=","; commaPos+=3; } toReturn+=num[this.num.length-1-counter] counter++; } } return toReturn; }
我使用以下代码测试了上面的内容:
public static void main(String[] args) {
BigNum bn = new BigNum(1234567);
System.out.println(bn);
}
输出:1,234,567