Difference between r1.11 and the current
@@ -108,24 +108,20 @@
String input;
System.out.println("x ? y or exit");
while( true )
{
System.out.print("Calculate : ");
input = scan.nextLine();
if(input.equals("exit")) break;
Calculation(input);
}
static void Calculation(String input){
int x = 0, y = 0;
String[] tmp = input.split(" ");
try {
x = Integer.parseInt(tmp[0]);
y = Integer.parseInt(tmp[2]);
System.out.println("x ? y or exit");
{
System.out.print("Calculate : ");
input = scan.nextLine();
if(input.equals("exit")) break;
Calculation(input);
}
}
scan.close();}
static void Calculation(String input){
int x = 0, y = 0;
x = Integer.parseInt(tmp[0]);
y = Integer.parseInt(tmp[2]);
@@ -154,7 +150,6 @@
}
}
}
}}}
=== 김정민 ===
{{{
}
}
=== 김정민 ===
{{{
5.1. 유재범 ¶
package hanjagonghomework; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Calculater { private static BufferedReader in; static{ in = new BufferedReader(new InputStreamReader(System.in)); } public static void main(String[] args) throws IOException{ int su1 = 0, su2 = 0, tot = 0; char yon = 0, flag = 0; while(true){ do{ flag = 0; try{ System.out.print("SU1 = "); su1 = Integer.parseInt(in.readLine()); }catch(NumberFormatException nfe){ System.out.println("숫자만 입력 가능!"); flag = 1; } }while(flag != 0); do{ do{ flag = 0; String str = ""; try{ System.out.println("YON(+, -, *, /) = "); str = in.readLine(); yon = str.charAt(0); }catch(StringIndexOutOfBoundsException siooe){ System.out.println("뭐든 입력해야 합니다."); flag = 1; } if(str.length() != 1){ System.out.println("연산자는 1자리여야 합니다."); flag = 1; } }while(flag != 0 || yon != '+' && yon != '-' && yon!= '*' && yon != '/'); do{ flag = 0; try{ System.out.print("SU2 = "); su2 = Integer.parseInt(in.readLine()); }catch(NumberFormatException nfe){ System.out.println("숫자만 입력 가능!"); flag = 1; } }while(flag != 0); flag = 0; try{ switch(yon){ case '+' : tot = su1 + su2; break; case '-' : tot = su1 - su2; break; case '*' : tot = su1 * su2; break; case '/' : tot = su1 / su2; break; } }catch(ArithmeticException ae){ System.out.println("0으로 나눌 수는 없습니다."); flag = 1; } }while(flag != 0); System.out.println(su1 + "" + yon + "" + su2 + "=" + tot); } } }
- 자바가 문자를 받아서 그걸로 Switch 돌리는건 생각도 못했는데 되니까 신기하네요
- 예시에 나와 있는걸 어떻게 고칠까 하다가 생각나는게 없어서 생각하는걸 그만둔다.
5.3. 김용준 ¶
package Calculator_140728; import java.util.Scanner; public class base { public static void main(String [] args){ Scanner scan = new Scanner(System.in); String input; System.out.println("x ? y or exit"); while( true ) { System.out.print("Calculate : "); input = scan.nextLine(); if(input.equals("exit")) break; Calculation(input); } scan.close(); } static void Calculation(String input){ int x = 0, y = 0; String[] tmp = input.split(" "); try { x = Integer.parseInt(tmp[0]); y = Integer.parseInt(tmp[2]); } catch (NumberFormatException e) { System.out.println("Insert only a number!"); return; } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Insert only this form!(x ? y)"); return; } switch(tmp[1]) { case "+": System.out.println(x+y); break; case "-": System.out.println(x-y); break; case "*": System.out.println(x*y); break; case "/": try{ System.out.println(x/y); } catch (ArithmeticException e){ System.out.println("Don't divide by 0!"); return; } break; } } }