gpt4 book ai didi

java - 当回答否时,do-while 循环不会终止

转载 作者:行者123 更新时间:2023-11-30 09:30:03 24 4
gpt4 key购买 nike

package homework4;
import java.util.Scanner;
public class Prog4 {
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
CreditCardNumber[] cred1;
CreditCardNumber cred2 = getInput();
Display("The complete number from your input:", cred2);
cred1 = getInputArray();
DisplayArray(cred1);
TryAnother(cred1);
}

public static CreditCardNumber getInput() {
String ID;
String accNum;
CreditCardNumber tempCred;
System.out.printf("Please enter issuer ID:");
ID = scanner.next();
System.out.printf("Please enter account number:");
accNum = scanner.next();
tempCred = new CreditCardNumber(ID, accNum);
return tempCred;
}

public static void Display(String ch, CreditCardNumber cred2) {
System.out.println(ch);
System.out.println(cred2.toString().replaceAll(".{4}", "$0 "));
}

public static CreditCardNumber[] getInputArray() {
CreditCardNumber[] tempArray;
String tempID;
int size;
System.out.printf("Please enter size of the aray:");
size = scanner.nextInt();
if(size < 1) {
size = 1;
}
tempArray = new CreditCardNumber[size];
System.out.printf("Please enter issuer ID:");
tempID = scanner.next();
for(int i = 0; i < tempArray.length; i++) {
tempArray[i] = new CreditCardNumber();
tempArray[i].CreateCred(tempID);
}
return tempArray;
}

public static void DisplayArray(CreditCardNumber[] cred1) {
for(int i = 0; i< cred1.length; i++) {
Display("Credit Card # " + i+":" + '\n', cred1[i]);
}
System.out.println();
}

public static boolean TryAnother(CreditCardNumber[] cred1) {
String s;
System.out.printf("Get more credit card numbers? (n for no):");
s = scanner.next();
if(s.charAt(0) != 'N' || s.charAt(0) != 'n') {
do {
TryAnother(cred1);
cred1 = getInputArray();
DisplayArray(cred1);
} while(s.charAt(0) != 'N' || s.charAt(0) != 'n');
}
return false;
}
}

您好,我在 TryAnother 方法中的 do-while 循环中遇到问题,当我输入 no 时,由于某种原因我试图让代码重复当 do while 循环的条件表明它应该终止时,该过程不会终止,它还会重复告诉用户输入"is"或“否”的行。

类似于:

Please enter issuer ID:321321 
Please enter account number:654654654
The complete number from your input:
3213 2165 4654 6549
Please enter size of the aray:7
Please enter issuer ID:789789
Credit Card # 0:
7897 8985 6852 9257
Credit Card # 1:
7897 8917 0678 9958
Credit Card # 2:
7897 8900 5781 0934
Credit Card # 3:
7897 8949 2244 6098
Credit Card # 4:
7897 8941 3828 4895
Credit Card # 5:
7897 8965 9233 5006
Credit Card # 6:
7897 8981 8442 5880
Get more credit card numbers? (n for no):n
Get more credit card numbers? (n for no):n
Get more credit card numbers? (n for no):no
Get more credit card numbers? (n for no):

当我输入 no 时你可以看到。它不断重复同一句话。我想要的是让它重复 from enter the array size 这句话,我该怎么做?

最佳答案

改变这个:

    }while(s.charAt(0) != 'N' || s.charAt(0) != 'n');       

到:

    }while(s.charAt(0) != 'N' && s.charAt(0) != 'n');       

关于java - 当回答否时,do-while 循环不会终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13438715/

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