Java - whileループの書き方

okwaves2024-01-25  10

現在、Java で簡単な ATM プログラムを作成しています。 ユーザーが間違ったピンを入力すると、ピンが一致するまでユーザーに再入力を求める while ループを作成したいと考えています。ピンが一致すると、メイン メニューが表示されます。

自分で試してみましたが、解決方法がわかりません。

while(userPIN != savedPIN)  
    {
     System.out.print("Please enter your correct PIN : ");
     Scanner again = new Scanner(System.in);
     int pass = again.nextInt();
     break;
    }    
    
    

プログラムでは単一のスキャナを使用できます。スキャナーを再作成し続けるのは非効率的です。

– NomadMaker

2020 年 9 月 3 日 11:34

単一のスキャナを使用するにはどうすればよいですか?

– user14214461

2020 年 9 月 3 日 11:42

1

ループの前にスキャナを作成し、それを使用するだけです。

– NomadMaker

2020 年 9 月 3 日 14:20

1

おそらく、パスではなく userPin を割り当てる必要があります。

– NomadMaker

2020 年 9 月 3 日 14:23



------------------------

「break;」を削除します。ステートメントを作成し、次のように userPIN を新しい PIN で更新します。

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        int savedPIN = 4444;
        Scanner input = new Scanner(System.in);
        System.out.println("Enter password");
        int userPIN = input.nextInt();
        double withdraw = 0.0, amount = 0.0, deposit = 0.0;

        while (userPIN != savedPIN) {
            System.out.print("Please enter your correct PIN : ");
            Scanner again = new Scanner(System.in);
            userPIN = again.nextInt();
        }

        while (userPIN == savedPIN) {

            System.out.println(" 1 - Inquire Balance \n 2 - Withdraw \n 3 - Deposit \n 0 - Quit ");


            Scanner inputNum = new Scanner(System.in);
            int number = inputNum.nextInt();
            switch (number) {
                case 1:
                    System.out.println("The current balance is $" + amount);
                    break;

                case 2:
                    System.out.println("Enter the amount to withdraw : ");
                    withdraw = input.nextDouble();
                    if (amount >= withdraw) {
                        amount = amount - withdraw;
                        System.out.println("The current balance is $" + amount);
                    } else {
                        System.out.println("Insufficient balance");
                    }
                    break;

                case 3:
                    System.out.println("Enter the amount to deposit : ");
                    deposit = input.nextDouble();
                    amount = amount + deposit;
                    System.out.println("The current balance is $" + amount);
                    break;

                case 0:
                    System.exit(4);
            }

        }


    }
}

3

入力したのですが、入力したにもかかわらず、正しい PIN を入力するよう求められ続けます。

– user14214461

2020 年 9 月 3 日、11:12

これは私があなたのコードで行ったもので、すべてが機能しているようです。編集を確認してください。

– タファズワ チンベレンワ

2020 年 9 月 3 日 11:17

1

スキャナは 1 つだけ作成します。再作成または複数作成する必要はありませんが、コードが読みにくくなり、メモリ使用量が増加します。

– クラッシュベスト

2020 年 9 月 3 日 12:05



------------------------

OK 2 エラー:

1) userPIN != SavedPIN をテストしますが、その値を変数パスに受け入れますが、何もしません。

2) 最初のループのブレークを削除すると、常にループせずに終了します。

次のようになります:

while(pass!= savedPIN)  
    {
     System.out.print("Please enter your correct PIN : ");
     Scanner again = new Scanner(System.in);
     int pass = again.nextInt();
    }

1

試してみましたが、正しい PIN を入力するよう求められ続けます。gh 正しい PIN を入力しました。

– user14214461

2020 年 9 月 3 日 11:02

総合生活情報サイト - OKWAVES
総合生活情報サイト - OKWAVES
生活総合情報サイトokwaves(オールアバウト)。その道のプロ(専門家)が、日常生活をより豊かに快適にするノウハウから業界の最新動向、読み物コラムまで、多彩なコンテンツを発信。