1. 程式人生 > >Java:利用巢狀迴圈模擬ATM機取款業務

Java:利用巢狀迴圈模擬ATM機取款業務

程式碼:

package com.jredu.ch03;

import java.util.Scanner;

public class Work4 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
	
		Scanner scan = new Scanner(System.in);
		for (int i = 1; i < 4; i++) {
			System.out.print("請輸入密碼:");
			String pwd = scan.next();
			if (!pwd.equals("111111")) {
				//密碼錯誤
				if(i==3){
					System.out.println("密碼錯誤,請取卡");
				}else{
					System.out.println("密碼錯誤,還有"+(3-i)+"次機會");
				}
			}else{
				//密碼正確
				while(true){
					System.out.print("請輸入金額:");
					int money = scan.nextInt();
					if(money%100==0&&money>0&&money<=1000){
						System.out.println("您取了"+money+"元");
						System.out.println("交易完成,請取卡!");
						return;
					}else{
						System.out.println("金額有誤,請重新輸入");
					}
				}
				}
				
		}

		
	}

}