• 카테고리

    질문 & 답변
  • 세부 분야

    알고리즘 · 자료구조

  • 해결 여부

    미해결

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

24.01.19 16:45 작성 조회수 107

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를 잘 정의해 주었는데 왜 이러는 걸까요?

답변 1

답변을 작성해보세요.

0

작은 개발자님의 프로필

작은 개발자

2024.01.19

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

자바칩님의 프로필

자바칩

질문자

2024.01.19

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

작은 개발자님의 프로필

작은 개발자

2024.01.19

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]);

}

}

}

이상없이 잘 실행됩니다.