강의

멘토링

커뮤니티

Inflearn Community Q&A

muskcitrus9459's profile image
muskcitrus9459

asked

Do it! Algorithm Coding Test with JAVA

내림차순으로 정렬하기 강의에서..

Written on

·

257

0

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

String str = sc.next();

int A[] = new int[str.length()];

for(int i=0; i<str.length(); i++){

A[i] = Integer.parseInt(str.substring(i, i+1));

}

for(int i=0; i<str.length(); i++){

int Max = i;

for(int j = i+1; j<str.length(); j++); {

if(A[j]>A[Max]) {

Max = j;

}

}

if (A[i] < A[Max]){

int temp = A[i];

A[i] = A[Max];

A[Max] = temp;

}

}

for (int i=0; i<str.length(); i++){

System.out.println(A[i]);

}

}

}

 

안녕하세요 강의 잘 보고 있어요.

강사님이 치라는 대로 코드를 따라 쳤는데 계속 오류가 뜨네요?? (굵게 표시한 부분)

cannot find symbol 오류인데.. 분명 j와 max를 잘 정의해 주었는데 왜 이러는 걸까요?

java코딩-테스트알고리즘

Answer 1

0

for(int j = i+1; j<str.length(); j++); // 세미콜론 제거

muskcitrus9459님의 프로필 이미지
muskcitrus9459
Questioner

감사합니다. 그런데 세미콜론을 삭제해도 틀렸다고 나오는데.. 혹시 이 이유도 아신다면 답변해주시면 감사하겠습니다.

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

String str = sc.next();

int A[] = new int[str.length()];

for (int i = 0; i < str.length(); i++) {

A[i] = Integer.parseInt(str.substring(i, i + 1));

}

for (int i = 0; i < str.length(); i++) {

int Max = i;

for (int j = i + 1; j < str.length(); j++)

{

if (A[j] > A[Max]) {

Max = j;

}

}

if (A[i] < A[Max]) {

int temp = A[i];

A[i] = A[Max];

A[Max] = temp;

}

}

for (int i = 0; i < str.length(); i++) {

System.out.println(A[i]);

}

}

}

이상없이 잘 실행됩니다.

muskcitrus9459's profile image
muskcitrus9459

asked

Ask a question