코드비교

Old VersionNew Version
1import java.util.Scanner; 1import java.util.Scanner; 
2public class Main { 2public class Main { 
3public static void main(String args[]) { 3public static void main(String[] args) {
4Scanner sc = new Scanner(System.in); 4Scanner input = new Scanner(System.in,"utf-8");
5int t = sc.nextInt(); 5int t = Integer.parseInt(input.next());
6sc.nextLine(); 6int n1,n2,nr;
7while(t-- > 0) { 7char op;
8String expression = sc.nextLine(); 8for(int i=0;i<t;i++) {
9try { 9try { 
10calculate(expression); 10n1 = Integer.parseInt(input.next());
11} catch (OperatorException e) { 11}
12System.out.println(e.getMessage()); 12catch(NumberFormatException e) {
   13System.out.print("Wrong Input: ");
   14System.out.println(e.getMessage().substring(e.getMessage().indexOf('"')+1,e.getMessage().lastIndexOf('"')));
   15input.next();
   16input.next();
   17continue;
   18}
   19try {
   20op = input.next().charAt(0);
   21n2 = Integer.parseInt(input.next());
   22}
   23catch(NumberFormatException e) {
   24System.out.print("Wrong Input: ");
   25System.out.println(e.getMessage().substring(e.getMessage().indexOf('"')+1,e.getMessage().lastIndexOf('"')));
   26continue;
   27}
   28switch(op) {
   29case '+':
   30nr = n1+n2;
   31break;
   32case '-':
   33nr = n1-n2;
   34break;
   35case '*':
   36nr = n1*n2;
   37break;
   38case '/':
   39nr = n1/n2;
   40break;
   41case '%':
   42nr = n1%n2;
   43break;
   44default:
   45continue;
   46}
   47System.out.println(n1+" "+op+" "+n2+" = "+nr);
   48}
   49input.close();
13} 50} 
14} 51} 
15}   
16static void calculate(String expression) throws OperatorException {   
17String terms[] = expression.split(" ");   
18int a, op, b;   
19try {   
20a = Integer.parseInt(terms[0]);   
21} catch (NumberFormatException nfe) {   
22System.out.println("Wrong Input: " + terms[0]);   
23return;   
24}   
25switch (terms[1]) {   
26case "+":   
27op = 1;   
28break;   
29case "-":   
30op = 2;   
31break;   
32case "*":   
33op = 3;   
34break;   
35case "/":   
36op = 4;   
37break;   
38case "%":   
39op = 5;   
40break;   
41default:   
42throw new OperatorException("Wrong Input: " + terms[1]);   
43}   
44try {   
45b = Integer.parseInt(terms[2]);   
46} catch (NumberFormatException nfe) {   
47System.out.println("Wrong Input: " + terms[2]);   
48return;   
49}   
50switch(op) {   
51case 1:   
52System.out.println(a + " + " + b + " = " + (a+b));   
53break;   
54case 2:   
55System.out.println(a + " - " + b + " = " + (a-b));   
56break;   
57case 3:   
58System.out.println(a + " * " + b + " = " + (a*b));   
59break;   
60case 4:   
61System.out.println(a + " / " + b + " = " + (a/b));   
62break;   
63case 5:   
64System.out.println(a + " % " + b + " = " + (a%b));   
65}   
66}   
67}   
68class OperatorException extends Exception {   
69OperatorException(String message) {   
70super(message);   
71}   
72}   
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
sc.nextLine();
while(t-- > 0) {
String expression = sc.nextLine();
try {
calculate(expression);
} catch (OperatorException e) {
System.out.println(e.getMessage());
}
}
}
static void calculate(String expression) throws OperatorException {
String terms[] = expression.split(" ");
int a, op, b;
try {
a = Integer.parseInt(terms[0]);
} catch (NumberFormatException nfe) {
System.out.println("Wrong Input: " + terms[0]);
return;
}
switch (terms[1]) {
case "+":
op = 1;
break;
case "-":
op = 2;
break;
case "*":
op = 3;
break;
case "/":
op = 4;
break;
case "%":
op = 5;
break;
default:
throw new OperatorException("Wrong Input: " + terms[1]);
}
try {
b = Integer.parseInt(terms[2]);
} catch (NumberFormatException nfe) {
System.out.println("Wrong Input: " + terms[2]);
return;
}
switch(op) {
case 1:
System.out.println(a + " + " + b + " = " + (a+b));
break;
case 2:
System.out.println(a + " - " + b + " = " + (a-b));
break;
case 3:
System.out.println(a + " * " + b + " = " + (a*b));
break;
case 4:
System.out.println(a + " / " + b + " = " + (a/b));
break;
case 5:
System.out.println(a + " % " + b + " = " + (a%b));
}
}
}
class OperatorException extends Exception {
OperatorException(String message) {
super(message);
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in,"utf-8");
int t = Integer.parseInt(input.next());
int n1,n2,nr;
char op;
for(int i=0;i<t;i++) {
try {
n1 = Integer.parseInt(input.next());
}
catch(NumberFormatException e) {
System.out.print("Wrong Input: ");
System.out.println(e.getMessage().substring(e.getMessage().indexOf('"')+1,e.getMessage().lastIndexOf('"')));
input.next();
input.next();
continue;
}
try {
op = input.next().charAt(0);
n2 = Integer.parseInt(input.next());
}
catch(NumberFormatException e) {
System.out.print("Wrong Input: ");
System.out.println(e.getMessage().substring(e.getMessage().indexOf('"')+1,e.getMessage().lastIndexOf('"')));
continue;
}
switch(op) {
case '+':
nr = n1+n2;
break;
case '-':
nr = n1-n2;
break;
case '*':
nr = n1*n2;
break;
case '/':
nr = n1/n2;
break;
case '%':
nr = n1%n2;
break;
default:
continue;
}
System.out.println(n1+" "+op+" "+n2+" = "+nr);
}
input.close();
}
}