亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何處理java中的無效輸入

如何處理java中的無效輸入

桃花長相依 2022-10-26 17:07:45
我正在制作一個簡單的銀行存款和取款代碼。代碼適用于存款部分,但在提款時它會詢問提款值 2 次。取款金額取最后一個值。我想我需要進入scannerObject.nextLine();某個地方,但是在哪里以及如何使用scannerObject.nextLine();?以下是我的示例代碼。還有一個其他類文件 BankAccount.java 只有 getter 和 setter 方法。package com.amit;import java.util.Scanner;public class Main {    public static void main(String[] args) {        BankAccount account = new BankAccount();        boolean option = true;        Scanner scanner = new Scanner(System.in);        while (option){            System.out.println("Press 1 For Deposite. Press 2 For Withdrawal. Press 3 For Exit");            boolean hasvalue = scanner.hasNextInt();            if(hasvalue){                //means user has entered integer value now check if its in 1, 2 if  its other                // than this we'll take him out of program to print balance                int userValEntered = scanner.nextInt();                if (userValEntered == 1){                    //code for deposite                    System.out.println("Enter Amount To Deposite");                    Scanner amountToDeposite = new Scanner(System.in);                    account.setBalance(amountToDeposite.nextDouble());                }else if (userValEntered == 2) {                    //Code for withdrawal                    System.out.println("Enter Amount To Withdraw");                    Scanner amountToWithdraw = new Scanner(System.in);                    if (amountToWithdraw.nextDouble() >= account.getBalance()){                        System.out.println("Unable to Withdraw Given Amount, Try Other Amount");                        continue;                    }else {                        double currentBalance = account.getBalance() - amountToWithdraw.nextDouble();                        account.setBalance(currentBalance);                        System.out.println("Thanks for Doing Business With us");                    }                }else{                    //if user enters anything other than 1 or 2                    break;                }            }
查看完整描述

1 回答

?
慕桂英546537

TA貢獻1848條經驗 獲得超10個贊

我發現我使用.nextDouble了兩次的錯誤。從現在開始,我會確保在使用它之前先將它保存在變量中。這就是我更新代碼的方式。


package com.amit;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        BankAccount account = new BankAccount();

        boolean option = true;

        Scanner scanner = new Scanner(System.in);

        while (option){

            System.out.println("Press 1 For Deposite. Press 2 For Withdrawal. Press 3 For Exit");

            boolean hasvalue = scanner.hasNextInt();

            if(hasvalue){

                //means user has entered integer value now check if its in 1, 2 if  its other

                // than this we'll take him out of program to print balance

                int userValEntered = scanner.nextInt();

                if (userValEntered == 1){

                    //code for deposite

                    System.out.println("Enter Amount To Deposite");

                    Scanner amountToDeposite = new Scanner(System.in);

                    account.setBalance(amountToDeposite.nextDouble());

                }else if (userValEntered == 2) {

                    //Code for withdrawal

                    System.out.println("Enter Amount To Withdraw");

                    Scanner amountToWithdraw = new Scanner(System.in);

                    double withdrawAmt = amountToWithdraw.nextDouble();

                    if (withdrawAmt >= account.getBalance()){

                        System.out.println("Unable to Withdraw Given Amount, Try Other Amount");

                        continue;

                    }else {

                        double currentBalance = account.getBalance() - withdrawAmt;

                        account.setBalance(currentBalance);

                        System.out.println("Thanks for Doing Business With us");

                    }

                }else{

                    //if user enters anything other than 1 or 2

                    break;

                }


            }else {

                //if user enters anything other than integer

                break;

            }


        }

        //code to print balance here.

        System.out.println("Your Balance is: "+account.getBalance());

    }

}


查看完整回答
反對 回復 2022-10-26
  • 1 回答
  • 0 關注
  • 145 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號