请记住,你将不会从让你的家庭作业由人来完成学到什么东西线上。你可能会从中学到一些东西,下次试试。我在解决方案中包含了评论。
public static void main(String[] args) {
//The line is a String, and the numbers must be parsed to integers
String line = "564 33 654 8321 15";
//We split the line at each space, so we can separate each number
String[] array = line.split("\\s+");
//Integer.MIN_VALUE will give you the smallest number an integer can have,
//and you can use this to check against.
int largestInt = Integer.MIN_VALUE;
//We iterate over each of the separated numbers (they are still Strings)
for (String numberAsString : array) {
//Integer.parseInt will parse a number to integer from a String
//You will get a NumberFormatException if the String can not be parsed
int number = Integer.parseInt(numberAsString);
//Check if the parsed number is greater than the largestInt variable
//If it is, set the largestInt variable to the number parsed
if (number > largestInt) {
largestInt = number;
}
}
//We are done with the loop, and can now print out the largest number.
System.out.println(largestInt);
}
你能告诉我们你已经尝试什么,以及为什么它不工作,不只是伪 –
我peeskillet这里同意。你已经有了伪代码(这是一个非常好的开始)。如果你还没有真正把它放到代码中,一次只能开始一个步骤,就像从字符串中提取数字开始一样。能够将程序分解成小的,易于管理的部分是非常重要的技能,这就是为什么我们很多人没有发布解决方案;它会抢夺你的重要经验。 –
我只想知道如何访问字符串中的每个数字? –