gpt4 book ai didi

java - 回答"is"时重复方法

转载 作者:太空宇宙 更新时间:2023-11-04 07:57:12 27 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) // pass cred1 as a parameter
{
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; // put the return statement here
}
}

嗨,我正在尝试从 TryAnother 方法中的 getInputArray 方法重复,我用 do-while 循环尝试此操作,但它说 cred1 在 do while 循环内未定义,但是当我尝试为 cred1 创建本地对象时,它给了我错误代码无法访问如果 do-while 循环工作,输出应该是这样的

Enter a credit card issuer number: 321321
Enter an account number: 654654654
The complete number from your input:
3213 2165 4654 6549

Enter the number of elements in the array: 7
Enter an issuer ID# (6 digits): 789789

Credit Card # 0:
7897 8931 4062 1219

Credit Card # 1:
7897 8920 2125 3522

Credit Card # 2:
7897 8971 9793 0944

Credit Card # 3:
7897 8979 3216 3090

Credit Card # 4:
7897 8995 0461 8493

Credit Card # 5:
7897 8948 8037 5909

Credit Card # 6:
7897 8966 0251 9953

Get more credit card numbers? (n for no): ok

Enter the number of elements in the array: 3
Enter an issuer ID# (6 digits): 345345

Credit Card # 0:
3453 4576 6705 1666

Credit Card # 1:
3453 4548 1163 5684

Credit Card # 2:
3453 4563 8807 0419

获取更多信用卡号码? (n 代表否):当然

任何人都可以建议一种执行 do-while 循环的方法吗?预先感谢您

我现在已经编辑了代码,由于某种原因它正在执行重复部分,当我输入“否”时,进程不会终止

类似于:

请输入发行人 ID:321321请输入账号:654654654您输入的完整数字:3213 2165 4654 6549
请输入阵列大小:7请输入发行人ID:789789信用卡#0:

7897 8985 6852 9257
信用卡#1:

7897 8917 0678 9958
信用卡#2:

7897 8900 5781 0934
信用卡#3:

7897 8949 2244 6098
信用卡#4:

7897 8941 3828 4895
信用卡#5:

7897 8965 9233 5006
信用卡#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 时,它会重复相同的句子,我想要的是它从输入数组大小的句子开始重复我该怎么做?

最佳答案

我不确定您期望该程序的输出是什么。既然您询问了编译错误,我已经编辑了您的代码并删除了两个编译错误。

  1. cred1 未在 TryAnother() 中定义。因此,您必须将其作为参数传递。像这样 TryAnother(CreditCardNumber[] cred1)
  2. TryAnother() 方法中,您已将 return true 语句放入 if block 中,但位于 do-while 循环之前。这会给您带来无法访问的语句编译错误,因为在方法中执行 return 语句后不会进一步执行。因此,您必须将 return true 语句放在 do-while 循环之后。

这是更正后的代码。

import java.util.Scanner;
public class Prog4 {<p></p>

<p>static Scanner scanner = new Scanner(System.in);</p>

<p>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); // pass variable cred1 as a parameter
}</p>

<p>public static CreditCardNumber getInput() {
String ID;
String accNum;
CreditCardNumber tempCred;<br/>
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;
}</p>

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

<p>public static CreditCardNumber[] getInputArray()
{
CreditCardNumber[] tempArray;
String tempID;
int size;<br/>
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;
}</p>

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

<p>public static boolean TryAnother(CreditCardNumber[] cred1) // pass cred1 as a parameter
{
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
{<br/>
cred1 = getInputArray();
}while(s.charAt(0) != 'N' || s.charAt(0) != 'n');
}
return false; // put the return statement here
}</p>

<p>}</p>

<p>// A dummy class
class CreditCardNumber{
String ID;
String accNum;
public CreditCardNumber(){
}
public CreditCardNumber(String ID,String accNum){
this.ID = ID;
this.accNum = accNum;
}
public void CreateCred(String tempID){
ID = tempID;
}
}
</p>

关于java - 回答"is"时重复方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13438153/

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