[英]Range of an integer based on the max and min value of it
In my class, I was asked to:在我的 class 中,我被要求:
Create a method that has a parameter of a single integer and return the range of the integer parameter.创建一个参数为单个 integer 的方法,并返回 integer 参数的范围。
(A negative integer parameter can be treated as positive integer.) (负 integer 参数可以视为正 integer。)
Clarifying example:澄清示例:
2634982 will produce a range of 7 (since 9 minus 2) 2634982 将产生 7 的范围(因为 9 减去 2)
And here's the code:这是代码:
import java.util.Scanner;
public class Main {
static int Range(int one)
{
int max = 0;
int min = Integer.MAX_VALUE;
int two = 0;
int three = 0;
while (one != 0) {
two = one % 10;
three = one / 10;
System.out.print(two+" "+three);
if (two > max){
max = two;
}
if (two < min){
min = two;
}
//System.out.print("\n"+min+" "+max);
one = max-min;
}
return one;
}
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
System.out.print("Enter an integer: ");
int one = key.nextInt();
System.out.println("\n"+one+" will produce a range of "+Range(one));
}
}
I'm having trouble having the while loop run more than once and I feel like solving that would make this work (at least better than before)我在 while 循环多次运行时遇到了麻烦,我觉得解决这个问题会使这项工作(至少比以前更好)
Why are you using the variable "three"?你为什么使用变量“三”? I consider it isn't needed.我认为不需要。 I suggest you to use the following code:我建议您使用以下代码:
import java.util.Scanner;
public class Main {
static int Range(int one)
{
int max = 0;
int min = Integer.MAX_VALUE;
int two = 0;
while (one != 0) {
two = one % 10;
one = one / 10;
//System.out.print(two+" "+three);
if (two > max){
max = two;
}
if (two < min){
min = two;
}
//System.out.print("\n"+min+" "+max);
}
one = max-min;
return one;
}
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
System.out.print("Enter an integer: ");
int one = key.nextInt();
System.out.println("\n"+one+" will produce a range of "+Range(one));
}
}
An alternative solution †另一种解决方案†
int
to a String
.将int
转换为String
。String
to an array of char
.将String
转换为char
数组。char
array (in ascending order).对char
数组进行排序(按升序)。import java.util.Scanner;
public class Main {
private static int range(int one) {
String str = String.valueOf(one);
str = str.replaceFirst("\\+|\\-", "");
char[] digits = str.toCharArray();
Arrays.sort(digits);
return (digits[digits.length - 1] - '0') - (digits[0] - '0');
}
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
System.out.print("Enter an integer: ");
int one = key.nextInt();
System.out.println("\n"+one+" will produce a range of "+ range(one));
}
}
nextInt
, of class java.util.Scanner
will accept integers with a leading +
or -
.请注意,class java.util.Scanner
的方法nextInt
将接受带有前导+
或-
的整数。 Hence I remove that character after converting the int
to a String
.因此,我在将int
转换为String
后删除了该字符。char
is really a number type (like int
) so subtracting the character 0
from the char
gives its numerical value. char
实际上是一种数字类型(如int
),因此从char
中减去字符0
即可得到其数值。 In other words it is a way to convert a char
, which is a digit, to an int
.换句话说,这是一种将char
(数字)转换为int
的方法。int
, for example 7
, the range will be 0 (zero).如果您输入单个数字int
,例如7
,则范围将为 0(零)。Here is output from a sample run:这是来自示例运行的 output:
Enter an integer: 1959
1959 will produce a range of 8
† Assuming you are allowed to use class java.util.Arrays † 假设您被允许使用 class java.util.Arrays
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.