我有这个问题,只要我输入我的第一个输入,程序就会崩溃,我得到
字符串索引超出范围:0
我在别处寻找并试图找出我的错误,但我发现了不同的问题,这些问题不是我所拥有的。有人可以告诉我我哪里出错了吗?
感谢您的帮助,代码如下:
import java.util.Scanner;
public class Assignment1Q2 {
public static void main(String[] args) {
System.out.println("Thank you for your call,\nPlease take some time to answer a few questions");
collectData();
}//end of main
public static void collectData() {
Scanner userInput = new Scanner(System.in);
int age;
char gender;
char show;
int over30MY = 0, over30FY = 0, under30MY = 0, under30FY = 0;
int over30MN = 0, over30FN = 0, under30MN = 0, under30FN = 0;
System.out.println("\nWhat is your age?\n");
age = userInput.nextInt();
System.out.println("Male or Female (Enter M or Y)");
gender = userInput.nextLine().charAt(0);
gender = Character.toLowerCase(gender);
System.out.println("Do you watch the show regularly? (Enter Y or N)");
show = userInput.nextLine().charAt(0);
show = Character.toLowerCase(show);
if((age > 30) && (gender == 'm') && (show == 'y')) {
over30MY++;
}
else if((age > 30) && (gender == 'f') && (show == 'y')) {
over30FY++;
}
else if((age < 30) && (gender == 'm') && (show == 'y')) {
under30MY++;
}
else if((age < 30) && (gender == 'f') && (show == 'y')) {
under30FY++;
}
else if((age > 30) && (gender == 'm') && (show == 'n')) {
over30MN++;
}
else if((age > 30) && (gender == 'f') && (show == 'n')) {
over30FN++;
}
else if((age < 30) && (gender == 'm') && (show == 'n')) {
under30MN++;
}
else if((age < 30) && (gender == 'f') && (show == 'n')) {
under30FN++;
}//end of if else
}//end of collectData
}// end of class