我正在编写程序,并在尝试执行for循环时遇到错误。我想在for循环中声明一个变量,然后在该变量获得某个值时断开它,但它返回错误“无法解析为变量”。在for循环中需要帮助声明一个变量(Java)
这里是我的代码
int i = -1;
for (; i == -1; i = index)
{
Scanner scan = new Scanner(System.in);
System.out.println("Please enter your first and last name");
String name = scan.nextLine();
System.out.println("Please enter the cost of your car,"
+ "\nthe down payment, annual interest rate,"
+ "\nand the number of years the car is being"
+ "\nfinanced, in that order.");
DecimalFormat usd = new DecimalFormat("'$'0.00");
double cost = scan.nextDouble();
double rate = scan.nextDouble();
int years = scan.nextInt();
System.out.println(name + ","
+ "\nyour car costs " + usd.format(cost) + ","
+ "\nwith an interest rate of " + usd.format(rate) + ","
+ "\nand will be financed annually for " + years + " years."
+ "\nIs this correct?");
String input = scan.nextLine();
int index = (input.indexOf('y'));
}
我想运行我的程序的输出segement直到用户输入的是,则循环中断。
添加到上面的回答也处理大写字母Y,如果还什么“耶”的用户类型或类似的东西,那也应该处理 –
@KaushikSivakumar是,该机制从环打破应该改变为“blatherskyte”,“yay”和“Y”的原因。 – rgettman