Java 实现do-while和exit
由于对Java相当陌生,我编写了这个程序来计算各种货币。我想询问用户是否希望重复该过程,直到用户希望退出。请在这件事上帮助我。到目前为止,我的代码是:Java 实现do-while和exit,java,converter,Java,Converter,由于对Java相当陌生,我编写了这个程序来计算各种货币。我想询问用户是否希望重复该过程,直到用户希望退出。请在这件事上帮助我。到目前为止,我的代码是: public static void main(String[] args) { Scanner input = new Scanner(System.in); DecimalFormat formatter = new DecimalFormat("#0.00"); String CType, c1 = "US"
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
DecimalFormat formatter = new DecimalFormat("#0.00");
String CType, c1 = "US", c2 = "EUR", c3 = "RM", c4 = "SAR";
double CValue , US, EUR, RM, SGD, SAR;
System.out.println("Welcome to Bonus Calculator");
System.out.println("Enter any of the following Currencies:");
System.out.print("US\n" +
"EUR\n" +
"RM\n" +
"SGD\n" +
"SAR: ");
CType = input.next();
if(CType.equalsIgnoreCase("US")){
System.out.print("Enter Value: ");
CValue = input.nextInt();
EUR = CValue * .88;
RM = CValue * 3.92;
SGD = CValue * 1.35;
SAR = CValue * 3.75;
System.out.print("US = " + formatter.format(CValue) + "EUR =" + formatter.format(EUR) + "RM =" + formatter.format(RM) + "SGD =" + formatter.format(SGD) + "SAR =" + formatter.format(SAR));
}
else if(CType.equalsIgnoreCase("EU")){
System.out.print("Enter Value: ");
CValue = input.nextInt();
US = CValue * 1.14;
RM = US * 3.92;
SGD = US * 1.35;
SAR = US * 3.75;
System.out.print("EUR = " + formatter.format(CValue) + " US = " + formatter.format(US) + " RM = " + formatter.format(RM) +" SGD = " + formatter.format(SGD) +" SAR = " + formatter.format(SAR));
}
else if (CType.equalsIgnoreCase("RM")){
System.out.print("Enter Value: ");
CValue = input.nextInt();
US = CValue * .26;
EUR = US * .88;
SGD = US * 1.35;
SAR = US * 3.75;
System.out.print("RM = " + formatter.format(CValue) + " US = " + formatter.format(US) + " EUR = " + formatter.format(EUR) + " SGD = " + formatter.format(SGD) + " SAR = " + formatter.format(SAR));
}
else if (CType.equalsIgnoreCase("SGD")){
System.out.print("Enter Value: ");
CValue = input.nextInt();
US = CValue * 0.74;
EUR = US * .88;
RM = US * 3.92;
SAR = US * 3.75;
System.out.print("SGD = " + formatter.format(CValue) + " US = " + formatter.format(US) + " EUR = " + formatter.format(EUR) + " SAR = " + formatter.format(SAR) + " RM = " + formatter.format(RM));
}
else if(CType.equalsIgnoreCase("SAR")){
System.out.print("Enter Value: ");
CValue = input.nextInt();
US = CValue * 0.39;
EUR = US * .88;
RM = US * 3.92;
SGD = US * 1.35;
System.out.print("SAR = " + formatter.format(CValue) + " US = " + formatter.format(US) + " EUR = " + formatter.format(EUR) + " SGD = " + formatter.format(SGD) + " RM = " + formatter.format(RM));
}
}
将所有代码包装在一个循环中,并使用一个布尔变量,如
keepRunning
,该变量初始化为true。在循环结束时,您询问用户(从扫描仪获取输入),然后检查输入是否表示“是”或“否”(或其他术语为true和false),并相应地设置keepRunning
(如果用户选择“否”,则将其设置为false)
例如:
boolean keepRunning = true;
while( keepRunning ) {
//ask for input, calculate, print - the bulk of your code above
System.out.println("Would you like to do another? y/n");
String userInput = ...; //get from scanner, I'll leave this as an excercise for you
keepRunning = "y".equalsIgnoreCase( userInput ); //you might want to be more lenient here, e.g. also accept "yes" etc.
}
while( true ) { //keep running until we stop it from the inside
//same as above
...
//break the loop if the user didn't select "y"
if( !"y".equalsIgnoreCase( userInput ) ) {
break;
}
}
或者使用while(true)
和break代码>当用户选择“否”时
例如:
boolean keepRunning = true;
while( keepRunning ) {
//ask for input, calculate, print - the bulk of your code above
System.out.println("Would you like to do another? y/n");
String userInput = ...; //get from scanner, I'll leave this as an excercise for you
keepRunning = "y".equalsIgnoreCase( userInput ); //you might want to be more lenient here, e.g. also accept "yes" etc.
}
while( true ) { //keep running until we stop it from the inside
//same as above
...
//break the loop if the user didn't select "y"
if( !"y".equalsIgnoreCase( userInput ) ) {
break;
}
}
将所有代码包装在一个循环中,并使用一个布尔变量,如keepRunning
,该变量初始化为true。在循环结束时,您询问用户(从扫描仪获取输入),然后检查输入是否表示“是”或“否”(或其他术语为true和false),并相应地设置keepRunning
(如果用户选择“否”,则将其设置为false)
例如:
boolean keepRunning = true;
while( keepRunning ) {
//ask for input, calculate, print - the bulk of your code above
System.out.println("Would you like to do another? y/n");
String userInput = ...; //get from scanner, I'll leave this as an excercise for you
keepRunning = "y".equalsIgnoreCase( userInput ); //you might want to be more lenient here, e.g. also accept "yes" etc.
}
while( true ) { //keep running until we stop it from the inside
//same as above
...
//break the loop if the user didn't select "y"
if( !"y".equalsIgnoreCase( userInput ) ) {
break;
}
}
或者使用while(true)
和break代码>当用户选择“否”时
例如:
boolean keepRunning = true;
while( keepRunning ) {
//ask for input, calculate, print - the bulk of your code above
System.out.println("Would you like to do another? y/n");
String userInput = ...; //get from scanner, I'll leave this as an excercise for you
keepRunning = "y".equalsIgnoreCase( userInput ); //you might want to be more lenient here, e.g. also accept "yes" etc.
}
while( true ) { //keep running until we stop it from the inside
//same as above
...
//break the loop if the user didn't select "y"
if( !"y".equalsIgnoreCase( userInput ) ) {
break;
}
}
您好,请检查以下代码
public static void main(String args[]){
Scanner input = new Scanner(System.in);
DecimalFormat formatter = new DecimalFormat("#0.00");
String CType, c1 = "US", c2 = "EUR", c3 = "RM", c4 = "SAR";
double CValue , US, EUR, RM, SGD, SAR;
char choice='Y';//modified
System.out.println("Welcome to Bonus Calculator");
do{//modified
System.out.println("Enter any of the following Currencies:");
System.out.print("US\n" +
"EUR\n" +
"RM\n" +
"SGD\n" +
"SAR: ");
CType = input.next();
if(CType.equalsIgnoreCase("US")){
System.out.print("Enter Value: ");
CValue = input.nextInt();
EUR = CValue * .88;
RM = CValue * 3.92;
SGD = CValue * 1.35;
SAR = CValue * 3.75;
System.out.print("US = " + formatter.format(CValue) + "EUR =" + formatter.format(EUR) + "RM =" + formatter.format(RM) + "SGD =" + formatter.format(SGD) + "SAR =" + formatter.format(SAR));
}
System.out.println("\nDo you which to Continue if Yes press 'Y' other wise press 'N'");//modified
choice = input.next().toCharArray()[0];//modified
}while(choice!='N');//modified
}
您好,请检查以下代码
public static void main(String args[]){
Scanner input = new Scanner(System.in);
DecimalFormat formatter = new DecimalFormat("#0.00");
String CType, c1 = "US", c2 = "EUR", c3 = "RM", c4 = "SAR";
double CValue , US, EUR, RM, SGD, SAR;
char choice='Y';//modified
System.out.println("Welcome to Bonus Calculator");
do{//modified
System.out.println("Enter any of the following Currencies:");
System.out.print("US\n" +
"EUR\n" +
"RM\n" +
"SGD\n" +
"SAR: ");
CType = input.next();
if(CType.equalsIgnoreCase("US")){
System.out.print("Enter Value: ");
CValue = input.nextInt();
EUR = CValue * .88;
RM = CValue * 3.92;
SGD = CValue * 1.35;
SAR = CValue * 3.75;
System.out.print("US = " + formatter.format(CValue) + "EUR =" + formatter.format(EUR) + "RM =" + formatter.format(RM) + "SGD =" + formatter.format(SGD) + "SAR =" + formatter.format(SAR));
}
System.out.println("\nDo you which to Continue if Yes press 'Y' other wise press 'N'");//modified
choice = input.next().toCharArray()[0];//modified
}while(choice!='N');//modified
}
您可以使用while(true)作为
当用户键入EXIT时,它将退出循环。您必须作为用户的选项退出。您可以使用while(true)作为
当用户键入EXIT时,它将退出循环。您必须作为用户的选项退出。那么问题出在哪里?您似乎已经对Java有了一些了解,循环有什么问题吗?我不知道在何处以及如何让它要求用户输入另一个值,如果没有,则退出。我从代码中知道,我应该知道这一点。但是我迷路了。那么问题出在哪里呢?您似乎已经对Java有了一些了解,循环有什么问题吗?我不知道在何处以及如何让它要求用户输入另一个值,如果没有,则退出。我从代码中知道,我应该知道这一点。但是我迷路了。非常感谢你。我能够按要求完成并运行程序。祝你有愉快的一天@试试看。编码员我很高兴它奏效了,希望你也学到了一些东西:)-顺便说一句,别忘了接受答案。非常感谢。我能够按要求完成并运行程序。祝你有愉快的一天@试试看。编码员我很高兴它奏效了,希望你也学到了一些东西:)-顺便说一句,别忘了接受答案。