gpt4 book ai didi

java - 代码可以编译但不能完全运行。当退出 Do While 循环时,会弹出红色错误提示

转载 作者:行者123 更新时间:2023-12-01 18:42:46 25 4
gpt4 key购买 nike

import java.util.Scanner; 

public class TipCalculator{

static double Steak;

static double Ribs;

static double Salad;

static double Burger;

static double SoftDrink;

static double PintofBeer;

static double Wine;

static double Champagne;

static int i;

static double [] Choice = new double [15];

static double Subtotal;

static double Tax;

static double Tip;

static double Total;

public static void main (String args []){

Scanner scan = new Scanner (System.in);

Ribs = 25;

Steak = 35;

Salad = 5;

Burger = 15;

SoftDrink = 2;

PintofBeer = 5;

Wine = 6;

Champagne = 9;

Tax = 1.13;

do {


System.out.println("Please enter one of the following options, or enter 9 to go to your bill");
System.out.println("1. 12 oz Striploin Steak ");
System.out.println("2. 16 oz Baby Back Ribs ");
System.out.println("3. Ceaser Salad ");
System.out.println("4. House Burger ");
System.out.println("5. Soft Drink ");
System.out.println("6. Wine ");
System.out.println("7. Champagne ");

Choice [i] = scan.nextInt();
}
while (Choice [i] < 9);



if (Choice [i] == 9){

Subtotal = Choice[1] + Choice[2] + Choice[3] + Choice[4] + Choice[5] + Choice[6] + Choice[7] + Choice[8] + Choice[9] + Choice[10] + Choice[11] + Choice[12] + Choice[13] + Choice[14] + Choice[15];

System.out.println("Your subtotal is " + Subtotal);
}

System.out.println("Enter your tip percent");

Tip = scan.nextInt();

Total = ((Subtotal * Tax) * (Tip/10));

System.out.println("Your total is " + Total);
}


}

最佳答案

您正在尝试访问数组索引 15(即数组中的第 16 个元素),从技术上讲,该索引不存在,因为索引从 0 开始。因此您可以访问的最后一个索引是 14。由代码初始化的总元素为 15。

更改线路:

小计 = 选择[1] + 选择[2] + 选择[3] + 选择[4] + 选择[5] + 选择[6] + 选择[7] + 选择[8] + 选择[ 9] + 选择[10] + 选择[11] + 选择[12] + 选择[13] + 选择[14] + 选择[15];

致:

小计 = 选择[0] + 选择[1] + 选择[2] + 选择[3] + 选择[4] + 选择[5] + 选择[6] + 选择[7] + 选择[ 8] + 选择[9] + 选择[10] + 选择[11] + 选择[12] + 选择[13] + 选择[14];

这将使流程滚动。 (尽管我承认,代码并没有通过仅仅阅读你的代码来完成它应该做的事情。但这是另一回事。)

关于java - 代码可以编译但不能完全运行。当退出 Do While 循环时,会弹出红色错误提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59890439/

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