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


获取更多信用卡号? (否):确定

谁能建议一种做do-while循环的方法?
先感谢您

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

就像是:

请输入发行人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时,请重复输入相同的句子,然后输入数组大小的句子
我怎样才能做到这一点?

最佳答案

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


cred1未在TryAnother()中定义。因此,您必须将其作为参数传递。像这样TryAnother(CreditCardNumber[] cred1)
TryAnother()方法中,已将return true语句放在if块的旁边,但在do-while循环之前。这将给您带来无法到达的语句编译错误,因为在方法中执行return语句后不再执行任何操作。因此,您必须将return true语句放在do-while循环之后。


这是更正的代码。

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

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 {
cred1 = getInputArray(); }while(s.charAt(0) != 'N' || s.charAt(0) != 'n'); } return false; // put the return statement here }

}

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

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

10-10 05:55