如何很好地调整Java中的循环?

问题描述 投票:0回答:3

我目前正在编写一个小程序,向用户询问 PinCode,如果 Pin 码正确则返回“:)”,如果 Pin 码错误则返回“:(”。 我的代码由一个 java 文件和一个文本文件组成。

这是代码:

import java.io.*;
import java.util.*;

public class Main {

static public boolean readPinsData(File dataFile, ArrayList<Integer> data) {
   boolean err = false;
   try {
      Scanner scanner = new Scanner(dataFile);
      String line;
      while (scanner.hasNext()) {
         line = scanner.nextLine();
         try {
            data.add(Integer.parseInt(line));
         } catch (NumberFormatException e) {
            e.printStackTrace();
            err = true;
         }
      }
      scanner.close();
   } catch (FileNotFoundException e) {
      e.printStackTrace();
      err = true;
   }

   return err;
}


public static void main(String[] args) {

    System.out.println("-----------------------");
    System.out.println("MY APP");
    System.out.println("-----------------------");
    Console console = System.console();
    int pinSize = 0;
    int nbTry = 0;

    do{
      do{
    char passwordArray[] = console.readPassword("Enter pin: ");
    pinSize = passwordArray.length;

    if(pinSize != 4){
            System.out.println("Pin must be 4 digits");
        } else {
            System.out.println("Checking...");
        }


   ArrayList<Integer> pins = new ArrayList<Integer>();
   readPinsData(new File("bdd.txt"), pins);
   //System.out.println(pins);
   //System.out.println(passwordArray);


   String[] thePins = new String[pins.size()];
   for (int i = 0; i < thePins.length; i++) {
    thePins[i] = pins.get(i).toString();
}

   String passEntered = String.valueOf(passwordArray);





   for(int i = 0 ; i < thePins.length ; i++){
      if(passEntered.equals(thePins[i]) && pinSize == 4){
          System.out.println(":)");
        } else if(!passEntered.equals(thePins[i]) && pinSize == 4){
            nbTry++;
        }

    }   

  }while(nbTry < 3);
   }while(pinSize != 4);

}
}

这是 bdd.txt,其中存储了所有好的 Pin 图:

1111
2222
3333
4444
5555
6666
7777
8888
9999

实际上我的问题是将尝试次数限制为3次。我需要解释一下:

--> 用户必须输入密码

--> 要么他输入一个好的 4 位数密码,然后打印“:)”(应用程序就完成了)

--> 要么他输入了错误的 4 位密码并打印“:(”,并且 nbTry 必须是 ++。 在这种情况下,他只剩下 2 次尝试了

--> 他还可以输入 1 位数字的 PIN 码或 2 位数字的 PIN 码或 3 位数字的 PIN 码...在这种情况下,nbTry 不受影响,他只需重新输入 4 位数字的 PIN 码即可。

我不知道如何处理 nbTry 左侧部分..有什么想法吗?

java loops for-loop while-loop
3个回答
2
投票

您希望他只能输入 4 位 PIN 码还是希望他能够输入任意长度的 PIN 码?

编辑:

读你的

main
我看到你有两个“do...while”。如果你改变它们的顺序,它应该可以工作。我无法在自动取款机上测试它,因为我在移动设备上,但可以这样尝试:

do {
  do {
      ....
  } while (pinSize != 4);
} while (nbTry < 3);

编辑2:

boolean loginCorrect = false;
for (int i = 0; i < thePins.length; i++) {
   if (passEntered.equals(thePins[i]) && pinSize == 4) {
      System.out.println(":)");
      loginCorrect = true;
      break;
   } else if (!passEntered.equals(thePins[i]) && pinSize == 4) {
      System.out.println(":(");
   }

}
if(!loginCorrect && pinSize == 4){
   nbTry++;
}

希望您能得到它,因为在手机上打字很困难。

完整的主要代码: 公共静态无效主(字符串[] args){

    System.out.println("-----------------------");
    System.out.println("MY APP");
    System.out.println("-----------------------");
    Console console = System.console();
    int pinSize = 0;
    int nbTry = 0;
    boolean authenticated = false;

    do {
        do {
            char passwordArray[] = console.readPassword("Enter pin: ");
            pinSize = passwordArray.length();

            if (pinSize != 4) {
                System.out.println("Pin must be 4 digits");
            } else {
                System.out.println("Checking...");
            }

            ArrayList<Integer> pins = new ArrayList<Integer>();
            readPinsData(new File("bdd.txt"), pins);
            // System.out.println(pins);
            // System.out.println(passwordArray);

            String[] thePins = new String[pins.size()];
            for (int i = 0; i < thePins.length; i++) {
                thePins[i] = pins.get(i).toString();
            }

            String passEntered = String.valueOf(passwordArray);

            for (int i = 0; i < thePins.length; i++) {
                if (passEntered.equals(thePins[i]) && pinSize == 4) {
                    System.out.println(":)");
                    authenticated = true;
                    break;
                }
            }

        } while (pinSize != 4);
        if (!authenticated && pinSize == 4) {
            System.out.println(":(");
            nbTry++;
        }
    } while (nbTry < 3 && !authenticated);
}

2
投票

如果我理解你的问题,你可以这样做(完成后你应该关闭()你的扫描仪)-

static public boolean readPinsData(File dataFile, ArrayList<Integer> data) {
    boolean err = false;
    Scanner scanner = null; // so we can close the Scanner.
    try {
        scanner = new Scanner(dataFile);
        String line;
        while (scanner.hasNext()) {
            line = scanner.nextLine();
            try {
                data.add(Integer.parseInt(line));
            } catch (NumberFormatException e) {
                e.printStackTrace();
                err = true;
            }
            // Limit it to 3 attempts. Set err on 3rd.
            if (data.size() >= 3) {
                err = true;
                break;
            }
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
        err = true;
    } finally {
        if (scanner != null) { // Close the Scanner.
            scanner.close();
        }
    }

    return err;
}

1
投票

有帮助的是对控制流/逻辑进行自上而下的细化。 由于这有点家庭作业的味道,只是一个想法:

Set<String> pins = readPINs();
boolean authenticated = false;
for (int attempt = 0; attempt < 3; ++attempt) {
    String pin = askForPIN();
    if (!isSyntacticalValidPIN(pin)) {
       giveError();
       break;
    } else if (pins.contains{pin)) {
       authenticated = true;
       break;
    }
}
if (authenticated) {
    offerMenu();
}
© www.soinside.com 2019 - 2024. All rights reserved.