⚡ sacnner+반복문
package oop; import java.util.Scanner; //Scanner + 반복문 public class oop17 { public static void main(String[] args) { //oop17_box op17 = new oop17_box(); //op17.abc(); /* 응용문제. 외부클래스 (product)이며 메소드는 (product_cal) 코드를 추가하며, 다음과 같이 결과가 나올 수 있도록 코드를 제작하시오. " 사용할 포인트 금액을 입력하세요 : " 단,포인트 입력 후 상품 금액에서 사용한 포인트를 뺀 결제 금액을 출력합니다. - 상품 금액 : 50000 */ product prd = new product(); prd.product_cal(); } } class oop17_box{ public void abc() { Scanner sc = new Scanner(System.in); //가상입력 라이브러리 System.out.println("출력할 구구단 숫자 하나를 입력하세요 : "); int no = sc.nextInt(); //숫자만 받음 int f; for(f=1; f <=9; f++) { System.out.printf("%d * %d = %d%n",no,f,no*f); } sc.close(); } } class product{ public void product_cal() { Scanner sc = new Scanner(System.in); System.out.println("사용할 포인트 금액을 입력하세요 :"); int price = sc.nextInt(); int prd_price = 50000; System.out.println(prd_price-price); sc.close(); } }
⚡ 반복적으로 사용자에게 질문을 던지는..
class oop18_box{ public void abc() { Scanner sc = new Scanner(System.in); int w=1; int money; //사용자가 직접 입력하는 숫자 int total = 0; //누적시킬 변수값 while(w<=4) { System.out.println("상품금액을 입력하세요 : "); money = sc.nextInt(); //사용자가 입력한 숫자를 변수에 이관 total += money; w++; } System.out.println(total); sc.close(); } }
⚡ 반복적으로 사용자에게 질문을 던지는..2 (은행 인출)
package oop; import java.util.Scanner; public class oop19 { public static void main(String[] args) { /* oop18 응용문제. 금융관련 코드를 작성합니다. 총 입금 금액은 100000원 출금을 총 3번 합니다. 출금메세지 : "출금하실 금액을 입력하세요" 해당 출금 후 총 잔액을 표시하는 코드를 작성하시오. 단,총 입금액보다 클경우 다음과같이 메세지를 출력합니다. "잔액보다 큰 금액이므로 출금되지 않습니다." */ bank bk = new bank(); bk.money(); //bk.atm(); } } class bank{ public void money() { Scanner sc = new Scanner(System.in); int myMoney = 100000; int out; int m=1; do { System.out.println("출금하실 금액을 입력하세요 : "); out = sc.nextInt(); if(out < 0 || myMoney < out) { System.out.println("잔액보다 큰 금액이므로 출금되지 않습니다."); }else { myMoney -= out; } System.out.println("남은금액 : "+myMoney); m++; }while(m<=3); System.out.println("총 잔액은" + myMoney); sc.close(); } /*--선생님 풀이--*/ public void atm() { int money = 100000; //최종 입금된 금액 Scanner sc = new Scanner(System.in); int f; int out; //사용자가 입력한 값; for(f=1; f<=3; f++) { System.out.println("출금하실 금액을 입력하세요 : "); out = sc.nextInt(); if(money < out) { System.out.println("잔액보다 큰 금액이므로 출금되지 않습니다."); }else { money -= out; //사용자가 입력한 값을 지속적으로 감소시킴 } System.out.println("현재 잔액 : " + money); } System.out.println("총 잔액은" + money); sc.close(); } }
'CLASS > JAVA' 카테고리의 다른 글
#4-3 / Method (0) | 2024.05.03 |
---|---|
#4-2 / Scanner + 더블반복문,선택문 (0) | 2024.05.03 |
#3-3 / library(Scanner) (0) | 2024.05.02 |
#3-2 / 반복문 (0) | 2024.05.02 |
#3-1 / 외부class,if문 (0) | 2024.05.02 |