gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:15:19 24 4
gpt4 key购买 nike

我目前正在编写一个小程序,向用户询问 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 次。我需要解释一下:

--> 用户必须输入 PIN

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

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

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

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

最佳答案

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

编辑:

阅读你的main我发现你有两个“do...while”。如果你改变它们的顺序它应该有效。我无法在 atm 上测试它,因为我在移动设备上尝试如下:

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

编辑2:

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

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

希望您能理解它,因为在移动设备上打字很困难。

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

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);
}

关于java - 如何很好的调整Java中的循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22562029/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com