没有得到我的代码有什么问题
not getting what's wrong with my code
public class DigitRange {
public static void main(String[] args) {
String numberstr = args[0];
int numberint = Integer.parseInt(args[0]);
int large=0;
int small=0;
System.out.println("range of " +numberint + " = "+ Range(numberstr,numberint,large,small));
}
public static int Range (String numberstr, int numberint,int large,int small){
for(int i=1;i<=numberstr.length();i++){
int digit = numberint % 10;
numberint = numberint/10;
large = Math.max(digit, large) ;
small = Math.min(digit, small);
}
int range = large - small + 1;
return range; //giving me 9
}
}
我的代码有什么问题?我在从方法 Range 返回正确值时遇到问题。当我应该返回值 6 时,我返回了值 9。我相信我在那里有一个逻辑错误。
新问题:
public class 数字范围 {
public static void main(String[] args) {
String numberstr = args[0];
int numberint = Integer.parseInt(args[0]);
int max=0;
int min=9;
System.out.println("range of " + numberstr + " = "+ Range(numberstr,numberint,max,min));
}
//finds and returns the range
public static int Range (String numberstr, int numberint,int max,int min){
if(numberint<0){
Math.abs(numberint);
for(int i=1;i<=numberstr.length()-1;i++){
int digit = numberint % 10;
numberint = numberint/10;
max = Math.max(digit, max) ;
min = Math.min(digit, min);
}
}
if(numberint>0){
for(int i=1;i<=numberstr.length();i++){
int digit = numberint % 10;
numberint = numberint/10;
max = Math.max(digit, max) ;
min = Math.min(digit, min) ;
}
int range = max - min + 1;
return range;
}
}
}
我怎样才能得到 if{} 范围之外的最大值和最小值?
您正在寻找作为参数传递给您的代码的数字中的最小和最大数字,差值 + 1 是您的范围,对吗?
大概small应该初始化为9。
public class DigitRange {
public static void main(String[] args) {
String numberstr = args[0];
int numberint = Integer.parseInt(args[0]);
int large=0;
int small=0;
System.out.println("range of " +numberint + " = "+ Range(numberstr,numberint,large,small));
}
public static int Range (String numberstr, int numberint,int large,int small){
for(int i=1;i<=numberstr.length();i++){
int digit = numberint % 10;
numberint = numberint/10;
large = Math.max(digit, large) ;
small = Math.min(digit, small);
}
int range = large - small + 1;
return range; //giving me 9
}
}
我的代码有什么问题?我在从方法 Range 返回正确值时遇到问题。当我应该返回值 6 时,我返回了值 9。我相信我在那里有一个逻辑错误。
新问题: public class 数字范围 {
public static void main(String[] args) {
String numberstr = args[0];
int numberint = Integer.parseInt(args[0]);
int max=0;
int min=9;
System.out.println("range of " + numberstr + " = "+ Range(numberstr,numberint,max,min));
}
//finds and returns the range
public static int Range (String numberstr, int numberint,int max,int min){
if(numberint<0){
Math.abs(numberint);
for(int i=1;i<=numberstr.length()-1;i++){
int digit = numberint % 10;
numberint = numberint/10;
max = Math.max(digit, max) ;
min = Math.min(digit, min);
}
}
if(numberint>0){
for(int i=1;i<=numberstr.length();i++){
int digit = numberint % 10;
numberint = numberint/10;
max = Math.max(digit, max) ;
min = Math.min(digit, min) ;
}
int range = max - min + 1;
return range;
}
}
}
我怎样才能得到 if{} 范围之外的最大值和最小值?
您正在寻找作为参数传递给您的代码的数字中的最小和最大数字,差值 + 1 是您的范围,对吗?
大概small应该初始化为9。