Baekjun 2920 Musical scale Java question

Asked 2 years ago, Updated 2 years ago, 42 views

Hello, I am a student who started studying Java. There is no error in the code, but the result is not output. Where did it go wrong? public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); ArrayList arr = new ArrayList<>();

    while (st.hasMoreTokens()) {
        arr.add(Integer.parseInt((st.nextToken())));
    }
    while (st.hasMoreTokens()) {
        int i = 0;
        if (arr.indexOf(i) == arr.indexOf(i) - 1) {
            System.out.println("ascending");
            i++;
        }
        else if (arr.indexOf(i) == arr.indexOf(i) + 1) {
            System.out.println("descending");
            i++;
        }
        else {
            System.out.println("mixed");
        }
    }
}

}

java

2022-09-20 18:58

1 Answers

I mean, if the code isn't wrong, there's nothing wrong I think it just passes through the while door Try logging the first and second while statements

while (st.hasMoreTokens()) {
//1. Where to take a picture 1
    arr.add(Integer.parseInt((st.nextToken())));
//2. Where to take a picture2
}
while (st.hasMoreTokens()) {
//3. Where to take a picture 3
    int i = 0;
    if (arr.indexOf(i) == arr.indexOf(i) - 1) {
        System.out.println("ascending");
        i++;
    }
    else if (arr.indexOf(i) == arr.indexOf(i) + 1) {
        System.out.println("descending");
        i++;
    }
    else {
        System.out.println("mixed");
    }
}

}

//Place 1: If it does not come out, the conditions of the while statement correspond x //Place to log 2: If there is a place 1 to log, but there is no place 2 to log, the condition of the while statement is applicable, but it is not included in the ArrayList //Place to log 3: If there is a place 1 and a place 2 to log, but there is no 3, the condition of the statement twice corresponds x


2022-09-20 18:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.