1
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 循环吗?先感谢您

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

就像是:

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):

as you can see when I enter no it keep repeat the same sentence what i want is for it to repeat from enter the array size sentence how can i do this?

4

2 回答 2

1

I'm not sure what are you expecting as the output of this program. Since you asked about a compile error, I have edited your code and removed two compile errors.

  1. cred1 is not defined in the TryAnother(). Hence you have to pass it as a parameter. Like this TryAnother(CreditCardNumber[] cred1)
  2. In the TryAnother() method, you have put a return true statement in side a if block, but before a do-while loop. This will gives you a unreachable statement compile error, since there is no further execution after executing a return statement in a method. So you have to put the return true statement after the do-while loop.

Here is the corrected code.

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

于 2012-11-18T07:53:00.123 回答
0

You should define cred1 as a global static method, outside of the main routine.

Or pass cred1, as a parameter to getInputArray(...), which in turn pass them further, to TryAnother(...).

于 2012-11-18T07:04:46.930 回答