不好意思 前几天有事没来得及 我改为goto后 还是在最后跳转的时候出现问题 illegal start of expression goto read_new1 illegal start of expression goto read_new2 goto在java中确实是保留字,但有没有别的跳转方式???
从新逻辑: do{ balance+=payment; interest=balance*interestRate/100; balance+=interest; year++; System.out.println("After "+year+" later,your balance is "+balance); input=JOptionPane.showInputDialog ("Do you want to retirement?(Y/N)"); if(input.equals("N") continue; else if(input.equals("Y")) { System.out.println(year+" you retire and your balance is "+balance); System.exit(0); }else{ do{ input=JOptionPane.showInputDialog ("pls enter again:Do you want to retirement?(Y/N)"); if(input.equals("N")) break; //出错 提示undefined lable: read_new1 }while(TRUE); } }while(input.equals("N"));
java不支持goto语句的. continue只能循环内部使用. break可以在循环外部使用.
to: layer781010 请问最后while(TURE)是什么意思? 修改了程序如下,能运行,但当出现连续2次或以上错误输入时,有问题 要象这样,是不是最后倒数第八行else continue 这样写有问题,continue跳转到哪里 ////////////////////////////////程序 import java.text.*; import javax.swing.*;public class Retirement{ public static void main(String[] args){ String input=JOptionPane.showInputDialog ("how much money will you contribute every years:"); double payment=Double.parseDouble(input); input=JOptionPane.showInputDialog ("interest rate in %:"); double interestRate=Double.parseDouble(input); NumberFormat formatter=NumberFormat.getCurrencyInstance(); double balance=0,interest=0; int year=0;
do { balance+=payment; interest=balance*interestRate/100; balance+=interest; year++; System.out.println("After "+year+" later,your balance is "+formatter.format(balance)); input=JOptionPane.showInputDialog ("Do you want to retirement?(Y/N)"); input=input.toUpperCase(); if(input.equals("N")) continue; else if(input.equals("Y")) { System.out.println(year+"year later you retire and your balance is "+formatter.format(balance)); System.exit(0); } else { do{ input=JOptionPane.showInputDialog ("pls enter again:Do you want to retirement?(Y/N)"); input=input.toUpperCase(); if(input.equals("N")) break; else if(input.equals("Y")) { System.out.println(year+"year later you retire and your balance is "+formatter.format(balance)); System.exit(0); } else continue; } while(input.equals("Y")); } } while(input.equals("N")); } }
我改为goto后 还是在最后跳转的时候出现问题
illegal start of expression goto read_new1
illegal start of expression goto read_new2
goto在java中确实是保留字,但有没有别的跳转方式???
do{
balance+=payment;
interest=balance*interestRate/100;
balance+=interest;
year++;
System.out.println("After "+year+" later,your balance is "+balance);
input=JOptionPane.showInputDialog
("Do you want to retirement?(Y/N)");
if(input.equals("N")
continue;
else if(input.equals("Y")) {
System.out.println(year+" you retire and your balance is "+balance);
System.exit(0);
}else{
do{
input=JOptionPane.showInputDialog
("pls enter again:Do you want to retirement?(Y/N)");
if(input.equals("N"))
break; //出错 提示undefined lable: read_new1
}while(TRUE);
}
}while(input.equals("N"));
continue只能循环内部使用.
break可以在循环外部使用.
修改了程序如下,能运行,但当出现连续2次或以上错误输入时,有问题
要象这样,是不是最后倒数第八行else continue 这样写有问题,continue跳转到哪里
////////////////////////////////程序
import java.text.*;
import javax.swing.*;public class Retirement{
public static void main(String[] args){
String input=JOptionPane.showInputDialog
("how much money will you contribute every years:");
double payment=Double.parseDouble(input); input=JOptionPane.showInputDialog
("interest rate in %:");
double interestRate=Double.parseDouble(input); NumberFormat formatter=NumberFormat.getCurrencyInstance(); double balance=0,interest=0;
int year=0;
do
{
balance+=payment;
interest=balance*interestRate/100;
balance+=interest;
year++;
System.out.println("After "+year+" later,your balance is "+formatter.format(balance));
input=JOptionPane.showInputDialog
("Do you want to retirement?(Y/N)");
input=input.toUpperCase();
if(input.equals("N"))
continue;
else if(input.equals("Y"))
{
System.out.println(year+"year later you retire and your balance is "+formatter.format(balance));
System.exit(0);
}
else
{
do{
input=JOptionPane.showInputDialog
("pls enter again:Do you want to retirement?(Y/N)");
input=input.toUpperCase();
if(input.equals("N"))
break;
else if(input.equals("Y"))
{
System.out.println(year+"year later you retire and your balance is "+formatter.format(balance));
System.exit(0);
}
else continue;
}
while(input.equals("Y"));
}
}
while(input.equals("N"));
}
}
除非输入“N”