Java do-while 做出是或否的回答?爪哇
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21960477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
do-while to make a yes or no answer? java
提问by user3309169
My current objective is to make this line of code continue to run over again as long as the "yes" is the true, or if it is "no" then stop, or if it is invalid prompt the user to make a correct selection. I'm completely lost on what to do.
我目前的目标是让这行代码继续运行一遍,只要“是”为真,或者如果是“否”则停止,或者如果无效则提示用户做出正确的选择。我完全不知道该怎么做。
import java.util.Scanner;
public class Temperature
{
public static void main(String[] args){
Scanner input = new Scanner (System.in);
System.out.println("Enter a number, a space and F to convert from degF to degC and C to convert to degC to degF");
double deg = input.nextDouble();
String letter = input.next().toUpperCase();
char dg = letter.charAt(0);
//String prompt;
tempLoop:
do{
if( dg == 'C'){
double cTemp= (deg-32)*5.0/9.0;
System.out.printf("%f degC converted to degF is %.2f%n",deg, cTemp );;
}
else if(dg == 'F'){
double fTemp = (deg*9/5) + 32;
System.out.printf("%f degF converted to degC is %.2f%n",deg, fTemp );
continue;
}
else{
System.out.println("That character does not correspond to a valid unit of measure ");
break;
}
}while(!(dg == 'C' || dg == 'F'));
System.out.println("Would you like to continue?(yes/no)");
}
}
回答by Kick
You are taking the USER input only one time so i m not sure why you are using do-while
loop.Also if you are trying to iterate on the basis on YES\NO then add the inputs in the do
loop and add one for input about YES/NO and add this check in while.
您只接受一次 USER 输入,所以我不确定为什么要使用do-while
循环。此外,如果您尝试在 YES\NO 的基础上进行迭代,则在do
循环中添加输入并添加一个输入关于 YES/NO 和添加此签入同时。
Use below code :
使用以下代码:
public class Temperature
{
public static void main(String[] args){
Scanner input = new Scanner (System.in);
double deg;
char dg;
String con;
do{
System.out.println("Enter a number, a space and F to convert from degF to degC and C to convert to degC to degF and YES to continue");
deg= input.nextDouble();
String letter = input.next().toUpperCase();
dg = letter.charAt(0);
con = input.next();
if( dg == 'C'){
double cTemp= (deg-32)*5.0/9.0;
System.out.printf("%f degC converted to degF is %.2f%n",deg, cTemp );;
}
else if(dg == 'F'){
double fTemp = (deg*9/5) + 32;
System.out.printf("%f degF converted to degC is %.2f%n",deg, fTemp );
continue;
}
else{
System.out.println("That character does not correspond to a valid unit of measure ");
break;
}
}while(con.equalsIgnoreCase("YES"));
} }
回答by Gigggas
You need to change the loop to include the user-prompting code and change it so that it only happens again after the user types "yes". Like so:
您需要更改循环以包含用户提示代码并更改它,以便它仅在用户键入“是”后再次发生。像这样:
import java.util.Scanner;
public class Temperature
{
public static void main(String[] args){
Scanner input = new Scanner (System.in);
double deg;
char dg;
String prompt;
tempLoop:
do{
System.out.println("Enter a number, a space and F to convert from degF to degC and C to convert to degC to degF");
deg = input.nextDouble();
String letter = input.next().toUpperCase();
dg = letter.charAt(0);
if( dg == 'C'){
double cTemp= (deg-32)*5.0/9.0;
System.out.printf("%f degC converted to degF is %.2f%n",deg, cTemp );;
}
else if(dg == 'F'){
double fTemp = (deg*9/5) + 32;
System.out.printf("%f degF converted to degC is %.2f%n",deg, fTemp );
continue;
}
else{
System.out.println("That character does not correspond to a valid unit of measure ");
break;
}
System.out.println("Would you like to continue?(yes/no)");
prompt = input.next();
}while(prompt.equalsIgnoreCase("yes"));
}
}
回答by CamLeng
You want to ask for the user's input BEFORE you start the loop again. This way, at the end of the loop, it asks the user if he/she wants to continue. If the answer is "yes", then the code will continue to loop. If the answer is "no", the program will exit the loop and move on.
您想在再次开始循环之前询问用户的输入。这样,在循环结束时,它会询问用户是否要继续。如果答案为“是”,则代码将继续循环。如果答案为“否”,则程序将退出循环并继续。
import java.util.Scanner;
public class Temperature
{
public static void main(String[] args){
Scanner input = new Scanner (System.in);
System.out.println("Enter a number, a space and F to convert from degF to degC and C to convert to degC to degF");
double deg = input.nextDouble();
String letter = input.next().toUpperCase();
char dg = letter.charAt(0);
//String prompt;
tempLoop:
do{
if( dg == 'C'){
double cTemp= (deg-32)*5.0/9.0;
System.out.printf("%f degC converted to degF is %.2f%n",deg, cTemp );;
}
else if(dg == 'F'){
double fTemp = (deg*9/5) + 32;
System.out.printf("%f degF converted to degC is %.2f%n",deg, fTemp );
continue;
}
else{
System.out.println("That character does not correspond to a valid unit of measure ");
break;
}
System.out.print("Would you like to continue?(yes/no) ");
String cont = input.nextLine();
}while((!(dg == 'C' || dg == 'F')) && cont.equalsIgnoreCase("yes"));
}
}
回答by nidomiro
I updated your code a bit. You are now able to Type 15F, 15C, ... (without whitespace) to convert. After this you will be asked if you want to continue.
我稍微更新了你的代码。您现在可以输入 15F、15C、...(不带空格)进行转换。在此之后,您将被询问是否要继续。
public static void main(String[] args) {
double deg = 0;
Scanner input = new Scanner(System.in);
do {
System.out
.println("Enter a number and F to convert from degF to degC or C to convert to degC to degF");
String text = input.next().toUpperCase();
try {
deg = Double.parseDouble(text.substring(0, text.length() - 1));
} catch (NumberFormatException e) {
e.printStackTrace();
}
if (text.endsWith("C")) {
double cTemp = (deg - 32) * 5.0 / 9.0;
System.out.printf("%f degC converted to degF is %.2f%n", deg, cTemp);
;
} else if (text.endsWith("F")) {
double fTemp = (deg * 9 / 5) + 32;
System.out.printf("%f degF converted to degC is %.2f%n", deg, fTemp);
continue;
} else {
System.out
.println("That character does not correspond to a valid unit of measure ");
}
System.out.println("Type YES to continue");
} while (input.next().equalsIgnoreCase("YES"));
input.close();
}
回答by Saleem Kalro
import java.util.Scanner;
public class Temperature
{
private static String reply;
public static void main(String[] args){
Scanner input = new Scanner (System.in);
System.out.println("Enter a number, a space and F to convert from degF to degC and C to convert to degC to degF");
double deg = input.nextDouble();
String letter = input.next().toUpperCase();
char dg = letter.charAt(0);
//String prompt;
tempLoop:
do{
do{
if( dg == 'C'){
double cTemp= (deg-32)*5.0/9.0;
System.out.printf("%f degC converted to degF is %.2f%n",deg, cTemp );;
}
else if(dg == 'F'){
double fTemp = (deg*9/5) + 32;
System.out.printf("%f degF converted to degC is %.2f%n",deg, fTemp );
continue;
}
else{
System.out.println("That character does not correspond to a valid unit of measure ");
break;
}
}while(!(dg == 'C' || dg == 'F'));
System.out.println("Would you like to continue?(yes/no)");
reply=input.next().toUpperCase();
}while(reply.equals("YES"));
}
}