我正在編寫程序,並在嘗試執行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