Java I';我试图通过for循环调用一个方法,而第一次迭代将只读取该方法中的第一行代码
我试图使用增强的for循环在一行中多次调用一个方法,但是如果我迭代多次,第一次迭代将只读取第一行代码。以下是我正在使用的两种方法:Java I';我试图通过for循环调用一个方法,而第一次迭代将只读取该方法中的第一行代码,java,for-loop,oop,foreach,iteration,Java,For Loop,Oop,Foreach,Iteration,我试图使用增强的for循环在一行中多次调用一个方法,但是如果我迭代多次,第一次迭代将只读取第一行代码。以下是我正在使用的两种方法: public Account() { this.subDetails = new HashMap<Integer, String>(); System.out.println("What is the account type? (Individual/Family)"); String planT
public Account() {
this.subDetails = new HashMap<Integer, String>();
System.out.println("What is the account type? (Individual/Family)");
String planType = keyboard.nextLine();
System.out.println("Enter your card number: ");
this.cardNum = keyboard.nextLine();
System.out.println("Enter the expiration date: ");
this.cardExp = keyboard.nextLine();
if (planType.equals("Family")) {
System.out.println("How many users?");
int numUsers = keyboard.nextInt();
for (int z=0; z<numUsers; z++) {
this.addUser();
}
StreamingService.numFamUsers = StreamingService.numFamUsers + numUsers;
StreamingService.monthlyRevenue = StreamingService.monthlyRevenue + 14.99;
StreamingService.monthlyFamRevenue = StreamingService.monthlyFamRevenue + 14.99;
}
else if (planType.equals("Individual")) {
this.addUser();
StreamingService.numIndUsers++;
StreamingService.monthlyRevenue = StreamingService.monthlyRevenue + 9.99;
StreamingService.monthlyIndRevenue = StreamingService.monthlyIndRevenue + 9.99;
}
StreamingService.numAccounts++;
this.subDetails.put(i, planType);
i++;
}
public void addUser() {
System.out.println("What is the email of the next user?");
String e = keyboard.nextLine();
User y = new User(e, i);
StreamingService.userEmails.add(y);
StreamingService.numUsers++;
this.acctUsers.add(e);
}
公共帐户(){
this.subDetails=newhashmap();
System.out.println(“账户类型是什么?(个人/家庭)”);
字符串planType=keyboard.nextLine();
System.out.println(“输入您的卡号:”);
this.cardNum=keyboard.nextLine();
System.out.println(“输入到期日期:”);
this.cardExp=keyboard.nextLine();
if(planType.equals(“族”)){
System.out.println(“有多少用户?”);
int numUsers=keyboard.nextInt();
对于(int z=0;zScanner.nextInt
不会使用在numers之后输入的换行符。因此,之后的任何读取行都会使用换行符并停止,因为它就是这样做的
因此,输入用户数后的第一个电子邮件提示将始终返回空字符串。您可以在输出中看到这一点:在这种情况下,电子邮件提示重复
要解决此问题,请在所有keyboard.nextLine()调用之后调用keyboard.nextLine()
(只需忽略该函数的输出).什么是键盘
?我没有在你的代码中看到该变量的声明。通过你调用它的方法,我确实知道它是扫描仪
?是的,这是一个扫描仪^^^^是的,但是你如何实例化它?你的代码没有达到你期望的效果的唯一方法是扫描仪为你提供一个值urnumuers
,或(以及“下一个用户的电子邮件是什么?”的重复输出提示)将立即返回,而不是等待第一个值。