Java 将多个整数作为单个值返回
嘿,我正在研究一种方法,它从两个传递的整数的1、10、数百和数千位中选取最小的数字,然后返回一个由每个位置的最小值组成的整数。例如,如果int a=4321,int b=1957,那么该方法将返回1321。这是到目前为止我的代码,我想我已经得到了所有东西,但是我无法找到如何正确地将新值作为整数返回Java 将多个整数作为单个值返回,java,oop,Java,Oop,嘿,我正在研究一种方法,它从两个传递的整数的1、10、数百和数千位中选取最小的数字,然后返回一个由每个位置的最小值组成的整数。例如,如果int a=4321,int b=1957,那么该方法将返回1321。这是到目前为止我的代码,我想我已经得到了所有东西,但是我无法找到如何正确地将新值作为整数返回 public static int biggestLoser(int a, int b){ int first; int second; int third; int
public static int biggestLoser(int a, int b){
int first;
int second;
int third;
int fourth;
if(a>9999 || a<1000 || b>9999 || b<1000){
if(a>b)
return b;
else
return a;
}
else{
if(a%10 < b%10)
first=a%10;
else
first=b%10;
if(a/1000<b/1000)
fourth=a/1000;
else
fourth=b/1000;
if(a/100%10<b/100%10)
second=a/100%10;
else
second=b/100%10;
if(a/10%10<b/10%10)
third=a/10%10;
else
third=b/10%10;
//int total=fourth,third,second,first;?????
//return total;
}
}
publicstaticintbiggestloser(inta,intb){
int优先;
int秒;
第三名;
第四;
如果(a>9999 | a9999 | bb)
返回b;
其他的
返回a;
}
否则{
如果(a%10如果(a/1000你可以先形成一个字符串,然后做一个整数.parseInt()
或者做一些数学,比如inttotal=(第一个*1)+(第二个*10)+(第三个*100)+(第四个*1000);
最好实现这样的东西:
int returnValue = 0;
for(int digit = 0; digit < 4; digit++) {
int aDigit = (a / Math.pow(10, digit)) % 10; // Gets the digit
int bDigit = (b / Math.pow(10, digit)) % 10;
int max = (a > b)? a : b; // Calculates de maximum digit
returnValue += max * Math.pow(10, digit); //Adds the new digit to the return value
}
return returnValue;
int returnValue=0;
用于(整数位数=0;位数<4;位数++){
int aDigit=(a/Math.pow(10,数字))%10;//获取数字
intbdigit=(b/Math.pow(10位))%10;
int max=(a>b)?a:b;//计算反最大位数
returnValue+=max*Math.pow(10位);//将新数字添加到返回值中
}
返回值;
首先,您的代码有一个小错误。您必须将代码交换为第二个
和第三个
if(a/100%10<b/100%10)
third=a/100%10;
else
third=b/100%10;
if(a/10%10<b/10%10)
second=a/10%10;
else
second=b/10%10;
就是这样。inti=1000,z=0;
int i=1000,z=0;
do{
int x = a/i;
a=a%i;
int y = b/i;
b=b%i;
if(x<y)
z+=x*i;
else z+=y*i;
i=i/10;
}while(i>0);
return z;
//this is just logic, works for 4 digit numbers
做{
int x=a/i;
a=a%i;
int y=b/i;
b=b%i;
if(x0);
返回z;
//这只是逻辑,适用于4位数字
只需将值作为int数组返回。我已经重构了您的方法来实现这一点
public static int[] biggestLoser(int a, int b){
int first;
int second;
int third;
int fourth;
int [] values;
if(a>9999 || a<1000 || b>9999 || b<1000){
if(a>b)
return b;
else
return a;
}
values = new int[4];
else{
if(a%10 < b%10)
first=a%10;
else
first=b%10;
if(a/1000<b/1000)
fourth=a/1000;
else
fourth=b/1000;
if(a/100%10<b/100%10)
second=a/100%10;
else
second=b/100%10;
if(a/10%10<b/10%10)
third=a/10%10;
else
third=b/10%10;
//int total=fourth,third,second,first;?????
values[0] = first;
values[1] = second;
values[2] = third;
values[3] = fourth;
return value;
}
考虑乘以10和加法,正如你在纸上所做的那样。不要让它变得更复杂——基本的第三级算术。MAT.POW是非常低效的,而他只需要乘以整数。谢谢,但是它有点太复杂了!
public static int[] biggestLoser(int a, int b){
int first;
int second;
int third;
int fourth;
int [] values;
if(a>9999 || a<1000 || b>9999 || b<1000){
if(a>b)
return b;
else
return a;
}
values = new int[4];
else{
if(a%10 < b%10)
first=a%10;
else
first=b%10;
if(a/1000<b/1000)
fourth=a/1000;
else
fourth=b/1000;
if(a/100%10<b/100%10)
second=a/100%10;
else
second=b/100%10;
if(a/10%10<b/10%10)
third=a/10%10;
else
third=b/10%10;
//int total=fourth,third,second,first;?????
values[0] = first;
values[1] = second;
values[2] = third;
values[3] = fourth;
return value;
}
for(int x : values){
System.out.print(x);
}