gpt4 book ai didi

java - 超出范围的字符串索引和使用输入数据的总计

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:52:28 26 4
gpt4 key购买 nike

我需要一些关于下面代码的帮助。我尝试循环程序,直到用户选择“N”,然后我想显示以盎司为单位销售的柠檬水总计以及各种 cookies 和衬衫的总计。

我放入了一个 while 循环来循环程序,但出现错误:

线程“main”中的异常 java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0 在 java.lang.String.charAt(未知来源) 在 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
}

最佳答案

要从标准输入(a/k/a STDIN a/k/a 键盘)获取单个字符,您可以采用以下两种方法之一:

char tshirt = (char) System.in.read(); //Takes one character, casts to "char"

或者您可以像这样使用 Scanner 中的方法:

char thsirt = scanner.next("."); //Takes exactly one character

这应该确保您不必处理 IndexOutofBounds 错误。

关于java - 超出范围的字符串索引和使用输入数据的总计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39691090/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com