Java 查找任意给定长度的纯数字字符串中的最高和最低数字
问题的措辞如下: 请用户输入一系列不分隔的单位数。程序应显示字符串中的最高和最低数字。此外,程序应显示仅包含偶数的字符串和仅包含奇数的字符串 显然,我需要以某种方式转换输入字符串(我们称之为numberInput),或者解析其中的最高和最低整数,以及所有奇数和偶数整数Java 查找任意给定长度的纯数字字符串中的最高和最低数字,java,string,Java,String,问题的措辞如下: 请用户输入一系列不分隔的单位数。程序应显示字符串中的最高和最低数字。此外,程序应显示仅包含偶数的字符串和仅包含奇数的字符串 显然,我需要以某种方式转换输入字符串(我们称之为numberInput),或者解析其中的最高和最低整数,以及所有奇数和偶数整数 public static void main(String[] args) { Scanner scan = new Scanner(System.in); String numberInput; // The
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String numberInput; // The variable of the string
System.out.println("Enter any number of positive digits, not separated by anything: "); // prompt user
numberInput = scan.next(); // read in the string
int phraseLength = numberInput.length(); // get length of input string
int middleIndex = phraseLength / 2; // find the middle index of input string
String highestDigits, lowestDigits; //?? just guessing here
int max, min; //?? made variables for highest value integer and lowest value integer
}
我想你在这里寻找的是一个循环和
text.charAt(index)
。尝试在java中查找的语法,而/查找的循环,并使用它们在字符串中循环,将字符串中的每个字符转换为int,然后比较它们。您可以跟踪当前的min
/max
,并在检查字符串中的每个字符时进行必要的更新
例如
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (counter=0;counter<phraselength;counter++) {
int this_int = (int)numberInput.charAt(counter);
if (this_int < min) {
min = this_int;
}
if (this_int > max) {
max = this_int;
}
}
int max=Integer.MIN\u值;
int min=整数最大值;
用于(计数器=0;计数器最大值){
max=这个整数;
}
}
因为这是一项明确的任务,只需稍加研究即可解决,所以我投了反对票,如下所示
在Java8
代码:
String s = "1237827";
String[] sp = s.split("");
int max = Arrays.stream(sp)
.mapToInt(st -> Integer.parseInt(st)).max().getAsInt();
int min = Arrays.stream(sp)
.mapToInt(st -> Integer.parseInt(st)).min().getAsInt();
System.out.println("The maximum number is " + max);
System.out.println("The minmum number is " + min);
System.out.print("The even numbers list is:");
Arrays.stream(sp)
.mapToInt(st -> Integer.parseInt(st))
.filter(i -> i % 2 == 0)
.forEach(i -> System.out.print(i + " "));
System.out.println("");
System.out.print("The odd numbers list is:");
Arrays.stream(sp)
.mapToInt(st -> Integer.parseInt(st))
.filter(i -> i % 2 != 0)
.forEach(i -> System.out.print(i + " "));
IntSummaryStatistics stats = Arrays.stream(sp)
.mapToInt(st -> Integer.parseInt(st))
.summaryStatistics();
System.out.println(stats);
The maximum number is 8
The minmum number is 1
The even numbers list is:2 8 2
The odd numbers list is:1 3 7 7
IntSummaryStatistics{count=7, sum=30, min=1, average=4.285714, max=8}
输出:
String s = "1237827";
String[] sp = s.split("");
int max = Arrays.stream(sp)
.mapToInt(st -> Integer.parseInt(st)).max().getAsInt();
int min = Arrays.stream(sp)
.mapToInt(st -> Integer.parseInt(st)).min().getAsInt();
System.out.println("The maximum number is " + max);
System.out.println("The minmum number is " + min);
System.out.print("The even numbers list is:");
Arrays.stream(sp)
.mapToInt(st -> Integer.parseInt(st))
.filter(i -> i % 2 == 0)
.forEach(i -> System.out.print(i + " "));
System.out.println("");
System.out.print("The odd numbers list is:");
Arrays.stream(sp)
.mapToInt(st -> Integer.parseInt(st))
.filter(i -> i % 2 != 0)
.forEach(i -> System.out.print(i + " "));
IntSummaryStatistics stats = Arrays.stream(sp)
.mapToInt(st -> Integer.parseInt(st))
.summaryStatistics();
System.out.println(stats);
The maximum number is 8
The minmum number is 1
The even numbers list is:2 8 2
The odd numbers list is:1 3 7 7
IntSummaryStatistics{count=7, sum=30, min=1, average=4.285714, max=8}
你的问题是什么?我需要找到字符串numberInput中的最高和最低数字。例如,如果用户输入1237827,我需要打印8(最高数字)和1(最低数字)。我还需要创建一个包含奇数个string numberInput的字符串和一个包含偶数个string numberInput的字符串。同样,如果用户输入1237827,我需要打印一个包含“1377”的字符串和一个包含“282”的字符串。显示您的尝试。尝试将字符串转换为int
s或char
s的数组。如果有人把数字放在一张纸上,让你“用手”做,你会怎么做?