mercredi 30 septembre 2015

My final else sentence isn't working in program(Java) [duplicate]

This question already has an answer here:

So I've been trying to do this school exercise, the code compiles and all, but it won't print the final else sentence if the characters won't pair (aka aren't same brackets such as "( and )" or "[ and ]".

Any advice?

public  class PairOrNot {
    public static void main(String[] args) {
        char first;
        char last;
        System.out.println("Hello blahblah");
        System.out.println("1st input");
        first = In.readChar();
        System.out.println("2nd input");
        last = In.readChar();
        if (first == '(')
            if (last == ')')
                System.out.println(
                        "Blahblah " + first + " and " + last + " blahblah.");
        if (first == '[')
            if (last == ']')
                System.out.println(
                        "Blahblah " + first + " and " + last + " blahblah.");
        if (alku == '{')
            if (loppu == '}')
                System.out.println(
                        "Blahblah " + first + " and " + last + " blahblah.");
        if (first == '<')
            if (last == '>')
                System.out.println(
                        "Blahblah " + first + " and " + last + " blahblah.");
            else {
                System.out.println("Characters " + first + " and " + last
                        + " aren't a pair.");
            }
    }
}

Aucun commentaire:

Enregistrer un commentaire