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循环,但是它表示在do while循环中未定义cred1但是当我尝试为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进程没有终止时它正在执行重复部分
类似的东西:
请输入发行人ID:321321
请输入帐号:654654654
您输入的完整数字:
3213 2165 4654 6549
请输入aray的大小: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时它会继续重复相同的句子我想要的是它重复输入数组大小的句子 我怎么能这样做?
答案 0 :(得分:1)
我不确定你对这个程序的输出有什么期望。由于您询问了编译错误,因此我编辑了您的代码并删除了两个编译错误。
cred1
未在TryAnother()
中定义。因此,您必须将其作为参数传递。像这样TryAnother(CreditCardNumber[] cred1)
TryAnother()
方法中,您已将return true
语句放在if块的一侧,但在do-while循环之前。这将为您提供无法访问的语句编译错误,因为在方法中执行return
语句后没有进一步执行。所以你必须在do-while循环之后放置return true
语句。这是更正后的代码。
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;
}
}
答案 1 :(得分:0)
您应该在cred1
例程之外将main
定义为全局静态方法。
或者将cred1
作为getInputArray(...)
的参数传递给TryAnother(...)
,然后将{{1}}传递给{{1}}。