Rumah  >  Artikel  >  Cetak penyata jumlah yang berjaya pada penyata if

Cetak penyata jumlah yang berjaya pada penyata if

PHPz
PHPzke hadapan
2024-02-06 10:39:07981semak imbas
Kandungan soalan

Saya membuat program atm di java. Secara keseluruhan, ia berfungsi. Walau bagaimanapun, saya cuba untuk mencetaknya "Anda telah melebihi had" apabila pengguna memasukkan jumlah simpanan dan semakan yang berjaya. Saya menggunakan pernyataan if untuk melakukan ini. Walau bagaimanapun, saya tidak pasti sama ada ia boleh menjana cetakan "Anda telah melebihi had". Saya baru belajar java jadi harap bersabar.

package atm;

import java.util.Scanner;

public class atm {

    public static void main(String[] args) {
        
        Scanner cin = new Scanner(System.in);
        
        int checking = 1000, savings = 5000, withdrawal = 0, deposit = 0;
        
        System.out.println("======================");
        
        System.out.println("Welcome to Sound Bank!");
        
        System.out.println("======================");
        
        while (true) {
        
        System.out.println("Please select which options: \n (C)Checking Balance \n (S)Savings");
        
        String choice1 = cin.nextLine(); 
        
        System.out.println("Please select of the following: ");
        
        System.out.println("(D)Deposit \n (W)Withdrawal \n (B)Check Balance \n (E)Exit");
        
        String choice2 = cin.nextLine(); 
        
        
            switch(choice2.toUpperCase()) {
            
            case "D":
                
                if (choice1.equalsIgnoreCase("C")) {
                    System.out.println("Enter your deposit amount:");
                    double num = cin.nextDouble();
                    double total = checking + num;
                    System.out.println("You entered" + num);
                    System.out.println("You have" + total);
                        if(num > checking) {
                            System.out.println("You are over the limit.");
                        }
                } else if (choice1.equalsIgnoreCase("S")) {
                    System.out.println("Enter your deposit amount");
                    double num1 = cin.nextDouble();
                    double total1 = savings + num1;
                    System.out.println("You entered" + num1);
                    System.out.println("You have" + total1);
                        if(num1 > savings) {
                            System.out.println("You are over the limit.");
                        }
                } 
                break;
            case "W":
                if (choice1.equalsIgnoreCase("C")) {
                    System.out.println("Enter your withdrawal amount:");
                    double w = cin.nextDouble();
                    double total2 = checking - w;
                    System.out.println("You entered" + w);
                    System.out.println("You have" + total2);
                        if(w > checking) {
                            System.out.println("You are over the limit.");
                        }
                } else if (choice1.equalsIgnoreCase("S")) {
                    System.out.println("Enter your withdrawal amount:");
                    double w2 = cin.nextDouble();
                    double total3 = cin.nextDouble();
                    System.out.println("You entered " + w2);
                    System.out.println("You have" + total3);
                        if(w2 > savings) {
                            System.out.println("You are over the limit.");
                        }
                } 
                break;
            case "B": 
                if (choice1.equalsIgnoreCase("C")) {
                    double t1 = (checking + deposit) - withdrawal;
                    System.out.println("Your total amount is:" + t1);
                } else if (choice1.equalsIgnoreCase("S")) {
                    double t2 = (savings + deposit) - withdrawal;
                    System.out.println("Your total amount is:" + t2);
                }
                break;
            case "E": 
                System.exit(0);
                break;
            }
        
    
        }

    }

}

Jawapan betul


Soalannya tidak begitu jelas. Tetapi saya menganggap masalah anda telah selesai

double w2 = cin.nextDouble();

double Total3 = cin.nextDouble();

digantikan dengan

double w2 = cin.nextDouble();

double Total3 = 节省 - w2;

Ia sepatutnya berfungsi seperti yang diharapkan.

Atas ialah kandungan terperinci Cetak penyata jumlah yang berjaya pada penyata if. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Artikel ini dikembalikan pada:stackoverflow.com. Jika ada pelanggaran, sila hubungi admin@php.cn Padam