請記住,你將不會從讓你的家庭作業由人來完成學到什麼東西線上。你可能會從中學到一些東西,下次試試。我在解決方案中包含了評論。
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這裏同意。你已經有了僞代碼(這是一個非常好的開始)。如果你還沒有真正把它放到代碼中,一次只能開始一個步驟,就像從字符串中提取數字開始一樣。能夠將程序分解成小的,易於管理的部分是非常重要的技能,這就是爲什麼我們很多人沒有發佈解決方案;它會搶奪你的重要經驗。 –
我只想知道如何訪問字符串中的每個數字? –