There’s really no need to attempt to code super simple programs more efficiently. Don’t be afraid to write a bunch of if-elses!
import java.io.PrintWriter; import java.util.Scanner; /** * * @author Sanchit M. Bhatnagar * @see http://uhunt.felix-halim.net/id/74004 * */ public class P11507 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); while (sc.hasNextInt()) { int L = sc.nextInt(); if (L == 0) break; L--; String ans = "+x"; for (int i = 0; i < L; i++) { String input = sc.next(); if (!input.equals("No")) if (ans.equals("+x")) { ans = input.toString(); } else if (ans.equals("-x")) { if (input.charAt(0) == '-') { ans = "+" + input.charAt(1); } else { ans = "-" + input.charAt(1); } } else if (ans.equals("+y")) { if (input.equals("+y")) { ans = "-x"; } else if (input.equals("-y")) { ans = "+x"; } else { ans = "+y"; } } else if (ans.equals("-y")) { if (input.equals("+y")) { ans = "+x"; } else if (input.equals("-y")) { ans = "-x"; } else { ans = "-y"; } } else if (ans.equals("+z")) { if (input.equals("+z")) { ans = "-x"; } else if (input.equals("-z")) { ans = "+x"; } else { ans = "+z"; } } else if (ans.equals("-z")) { if (input.equals("+z")) { ans = "+x"; } else if (input.equals("-z")) { ans = "-x"; } else { ans = "-z"; } } } out.println(ans); } out.close(); sc.close(); } }