자바 반복문 함수를 사용해서 구성 하고 싶습니다
글쓴이: 익명 사용자 / 작성시간: 금, 2018/09/14 - 4:22오전
밑에 자바 코드는
반복해서 물어보면서 사칙연산을 하는 코딩인데요.
이 코딩을 함수를 이용해서 구현 하려면 어떻게 해야할까요?
반복문하고 조건문이 같이 들어가니까 어렵네요.
public class nuber3 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double x = 0;
double y = 0;
String op = null;
System.out.println("Hi, I am really good at math! Put me to the test.");
while (true) {
System.out.print("Please enter numbers and then press Enter: ");
x = scan.nextDouble();
y = scan.nextDouble();
System.out.print("Please enter one of the operations +, -, * or / and press Enter:");
op = scan.next();
switch (op) {
case "+":
System.out.println(+x + " + " + y + " = " + (x + y));
System.out.println("I'm great at addition!");
break;
case "-":
System.out.println(+x + " - " + y + " = " + (x - y));
System.out.println("I'm great at subtraction!");
break;
case "*":
System.out.println(+x + " * " + y + " = " + (x * y));
System.out.println("I'm great at multiplication!");
break;
case "/":
System.out.println(+x + " / " + y + " = " + (x / y));
System.out.println("I'm great at division!");
break;
default:
System.out.println("Invalid operator.");
continue;
}
System.out.print("Play again [Y/N]?:");
op = scan.next();
if ("N".equalsIgnoreCase(op)) break;
}
System.out.println("Bye bye!\n");
scan.close();
}
} Forums:

참고해보세요.
https://ide.goorm.io
자바 프로그래밍 - 네이버 책
https://book.naver.com/search/search.nhn?sm=sta_hty.book&sug=&where=nexearch&query=%EC%9E%90%EB%B0%94+%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%B0%8D
package project; import java.io.*; import java.util.StringTokenizer; import java.util.Scanner; //Class Scanner //https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html //[JAVA] 문자열 입력과 출력 (Scanner) //http://hyeonstorage.tistory.com/136 public class Main { public static void fn() { Scanner scan = new Scanner(System.in); double x = 0; double y = 0; String z = ""; while (true) { System.out.println("Hi, I am really good at math! Put me to the test."); System.out.print("Please enter number and then press Enter: "); x = scan.nextDouble(); System.out.println(x); y = scan.nextDouble(); System.out.println(y); if (x == 999) { System.out.print("Bye bye!"); return; } else { // System.out.print("Please enter number and then press Enter: "); // y = scan.nextDouble(); System.out.print("Please enter one of the operations +, -, * or / and press Enter:"); z = scan.next(); switch (z) { case "+": System.out.println( + x + " + " + y + " = " + (x + y) ); System.out.println("I'm great at subtraction!\n"); break; case "-": System.out.println( + x + " - " + y + " = " + (x - y) ); System.out.println("I'm great at subtraction!\n"); break; case "*": System.out.println( + x + " * " + y + " = " + (x * y) ); System.out.println("I'm great at subtraction!\n"); break; case "/": System.out.println( + x + " / " + y + " = " + (x / y) ); System.out.println("I'm great at subtraction!\n"); break; default: System.out.println("잘못된 연산자입니다.\n"); } } } } public static void main(String[] args) { fn(); } }----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.
매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.
각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com
댓글 달기