을 당신이 당신의 숙제 사람들에 의해 이루어지고로부터 아무것도 배울 것이라는 점을 기억하십시오 온라인. 당신은 그것으로부터 무언가를 배우고 다음 번에 스스로를 시도 할 것입니다. 솔루션에 주석을 포함 시켰습니다.
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에 동의합니다. 당신은 이미 의사 코드를 가지고 있습니다 (이것은 아주 좋은 출발입니다). 실제로 코드에 넣지 않은 경우에는 문자열에서 숫자를 추출하는 것과 같이 한 번에 한 단계 씩 시작하십시오. 프로그램을 작고 다루기 쉬운 조각으로 나눌 수 있다는 것은 매우 중요한 기술입니다. 그래서 많은 사람들이 해결책을 게시하지 않았습니다. 중요한 경험을 훔칠 것입니다. –
문자열의 각 번호에 어떻게 액세스 할 수 있는지 알고 싶습니다. –