아래 코드에 대한 도움이 필요합니다. 나는 사용자가 'N'을 선택할 때까지 프로그램을 반복하려고하는데, 그때 레모네이드에 대한 합계를 온스와 총 쿠키 및 각 종류의 셔츠로 판매하고 싶습니다.문자열 인덱스가 범위를 벗어 났으며 입력 데이터를 사용하는 합계
나는 루프 프로그램에 while 루프에 넣어하지만 오류 얻을 : 스레드에서
예외를 "주"java.lang.StringIndexOutOfBoundsException : 문자열 색인이 범위를 벗어 : 0 java.lang.String의에서 .charAt (알 수없는 출처) at edu.kirkland5.SnackStand.main (SnackStand.java:43)
누구나 내가이 두 가지를 성취하기 위해해야 할 일을 도울 수 있다면 크게 감사하겠습니다. 고맙습니다!
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String userName = "";
int lemonaid = 0;
double OZ12_PRICE = 1.50;
double OZ16_PRICE = 2.00;
char cookies = 0;
double COOKIES_OAT= .75;
double COOKIES_CHOCO= .75;
char tshirt = 0;
double TSHIRT_Y = 15.00;
double TSHIRT_N = 8.00;
char item;
char answer = 0;
System.out.println("Hello. May I ask your name?");
userName = input.nextLine();
while (answer != 'N')
{
System.out.println("Welcome " +userName +". Would you like to purchase something? (Y/N) ");
answer = input.nextLine().charAt(0);
answer = Character.toUpperCase(answer);
System.out.println(" ***Lemonaid Stand Program***");
System.out.println("=======================================");
System.out.println("Enter purchase item, first letter only.");
System.out.println("Lemonaid, Cookies, T-shirt.");
System.out.println("Enter Q to Quit.");
System.out.println();
System.out.println("Enter an item or Q to quit (L,C,T,Q)");
item = input.nextLine().charAt(0);
item = Character.toUpperCase(item);
while (item != 'L' && item != 'C' && item != 'T' && item != 'Q')
{
System.out.println("Invalid input.");
System.out.println("Please enter an item, or Q to quit. (L,C,T,Q): ");
item = input.nextLine().charAt(0);
item = Character.toUpperCase(item);
}
if (item == 'L')
{
System.out.println("What size would you like?");
System.out.printf("%-10s%10.2f\n", "12oz.", OZ12_PRICE);
System.out.printf("%-10s%10.2f\n", "16oz.", OZ16_PRICE);
System.out.println("Input the number only.");
lemonaid = input.nextInt();
while (lemonaid != 12 && lemonaid != 16)
{System.out.println("I'm sorry. Please only input an 16 or 12.");
lemonaid=input.nextInt();
}
if (lemonaid == 12)
{
Sales_Header();
System.out.printf("%-10s%10.2f\n", "12oz.", OZ12_PRICE);
}
else if (lemonaid == 16)
{
Sales_Header();
System.out.printf("%-10s%10.2f\n", "16oz.", OZ16_PRICE);
}
else
{
}
}//end If = L
else if (item == 'C')
{
System.out.println("What type would you like?");
System.out.printf("%-14s%10.2f\n", "Oatmeal", COOKIES_OAT);
System.out.printf("%-10s%10.2f\n", "Chocolate Chip", COOKIES_CHOCO);
System.out.println("Input the first letter only. (O/C)");
cookies = input.nextLine().charAt(0);
cookies = Character.toUpperCase(cookies);
while (cookies != 'O' && cookies != 'C')
{
System.out.println("I'm sorry. Please only input an 'O' or 'C'.");
cookies = input.nextLine().charAt(0);
cookies = Character.toUpperCase(cookies);
}
if (cookies == 'O')
{
Sales_Header();
System.out.printf("%-10s%10.2f\n", "Oatmeal", COOKIES_OAT);
}
else if (cookies == 'C')
{
Sales_Header();
System.out.printf("%-10s%10.2f\n", "Chocolate Chip", COOKIES_CHOCO);
}
else
{
}
}// end else if = C
else if (item == 'T')
{
System.out.println("Would you like that shirt autographed?");
System.out.printf("%-10s%10.2f\n", "With autograph", TSHIRT_Y);
System.out.printf("%-10s%11.2f\n", "W/O autograph", TSHIRT_N);
System.out.println("Select Yes or No (Y/N)");
tshirt = input.nextLine().charAt(0);
tshirt = Character.toUpperCase(tshirt);
while (tshirt != 'Y' && tshirt != 'N')
{
System.out.println("I'm sorry. Please only input an 'Y' or 'N'.");
tshirt = input.nextLine().charAt(0);
tshirt = Character.toUpperCase(tshirt);
}
if (tshirt == 'Y')
{
Sales_Header();
System.out.printf("%-10s%10.2f\n", "With autograph", TSHIRT_Y);
}
else if (tshirt == 'N')
{
Sales_Header();
System.out.printf("%-10s%10.2f\n", "W/O autograph", TSHIRT_N);
}
else
{
}
}//end else if T
else if (item == 'Q')
{
System.out.println("Main Menu.");
}
else
{
}
}//End while loop
}
}
이것은 큰 도움이됩니다! 시간 내 주셔서 대단히 감사합니다. – ArchAngel