Java 如何将do while循环转换为while循环
如何将执行while循环转换为while循环?Java 如何将do while循环转换为while循环,java,loops,while-loop,do-while,Java,Loops,While Loop,Do While,如何将执行while循环转换为while循环? int numAttempts = 0; do { System.out.println("Do you want to convert to Peso or Yen?"); pesoOrYen = readKeyboard.nextLine(); if(pesoOrYen.equalsIgnoreCase("Peso")||pesoOrYen.equalsIgnoreCase("Yen")) {
int numAttempts = 0;
do
{
System.out.println("Do you want to convert to Peso or Yen?");
pesoOrYen = readKeyboard.nextLine();
if(pesoOrYen.equalsIgnoreCase("Peso")||pesoOrYen.equalsIgnoreCase("Yen"))
{
notPesoOrYen = false;
}
else if (numAttempts < 2)
{
System.out.println("Sorry, but '"+pesoOrYen+"' is not a valid currency type. Try again:");
notPesoOrYen = true;
}
numAttempts++;
} while(notPesoOrYen==true && numAttempts < 3);
int numatempts=0;
做
{
System.out.println(“您想转换成比索还是日元?”);
pesoOrYen=readKeyboard.nextLine();
if(比索等值信号案(“比索”)| |比索等值信号案(“日元”))
{
notPesoOrYen=假;
}
否则如果(numtempts<2)
{
System.out.println(“对不起,”+pesoOrYen+“”不是有效的货币类型。请重试:”;
notPesoOrYen=正确;
}
numtempts++;
}而(notPesoOrYen==true&&numtempts<3);
我尝试执行而(notPesoOrYen==true&&numtempts<3)
然后执行该语句,但它不起作用
我的完整代码
封装电流转换器
导入java.util.Scanner;
导入java.text.NumberFormat
公共类电流转换器
{
publicstaticvoidmain(字符串[]args)
{
扫描仪读取键盘=新扫描仪(System.in);
双双肩胛骨;
布尔值notPesoOrYen=true;
线状假牙;
双用户转换资本;
布尔userInputorUnprogramagain=true;
最终双美元兑比索=13.14;
最终双美元兑日元=106.02;
做
{
System.out.println(“您有多少美元?”);
字符串userscatial=readKeyboard.nextLine();
doubleUsersCaptial=Double.parseDouble(usersCaptial);
int numatempts=0;
做
{
System.out.println(“您想转换成比索还是日元?”);
pesoOrYen=readKeyboard.nextLine();
if(比索等值信号案(“比索”)| |比索等值信号案(“日元”))
{
notPesoOrYen=假;
}
否则如果(numtempts<2)
{
System.out.println(“对不起,”+pesoOrYen+“”不是有效的货币类型。请重试:”;
notPesoOrYen=正确;
}
numtempts++;
}而(notPesoOrYen==true&&numtempts<3);
如果(numatempts==3)
{
System.out.println(“对不起,“+pesoOrYen+””不是有效的货币类型。”);
System.out.println(“您多次输入错误的货币类型\n再见”);
系统出口(0);
}
if(比索等信号情况(“比索”))
{
usersConvertedCapital=doubleUsersCaptial*美元兑比索;
}
其他的
{
usersConvertedCapital=doubleUsersCaptial*美元兑日元;
}
NumberFormat formatter=NumberFormat.getCurrencyInstance();
字符串formatUsersCaptial=formatter.format(doubleUsersCaptial);
字符串formatUsersConvertedCapital=formatter.format(usersConvertedCapital);
System.out.println(formatUsersCaptial+“美元=”
+formatUsersConvertedCapital+“”+pesoOrYen);
System.out.println(“是否要再次运行该程序?(输入“是”或“否”);
字符串runprogramReach=readKeyboard.nextLine();
if(再次运行ProgramReach.equalsIgnoreCase(“是”))
{
UserInputorUnprogramagain=true;
}
else if(再次运行ProgramReach.equalsIgnoreCase(“否”))
{
System.out.println(“再见”);
系统出口(0);
}
其他的
{
System.out.println(“您输入的不是“是”或“否”\n”
+“再见”);
系统出口(0);
}
}while(userInputorUnprogramagain==true);
}
}
而和做。。。虽然
几乎相同,做。。。while
只是在第一次评估退出条件之前执行一次迭代,而while
即使在第一次迭代中也会对其进行评估(因此最终,while
循环的主体永远无法到达,而do…while
主体将始终至少执行一次)
您的代码片段不完整,但我猜您在循环之前没有将notPesoOrYen
初始化为true
,这就是它不工作的原因。最后,不要写while(notPesoOrYen==true&&numtempts<3)
而是while(notPesoOrYen&&numtempts<3)
,不需要比较==true
。在while循环外部初始化布尔变量:
int numAttempts = 0;
boolean notPesoOrYen=true;
while (notPesoOrYen && numAttempts < 3) {
System.out.println("Do you want to convert to Peso or Yen?");
pesoOrYen = readKeyboard.nextLine();
if (pesoOrYen.equalsIgnoreCase("Peso") || pesoOrYen.equalsIgnoreCase("Yen")) {
notPesoOrYen = false;
} else if (numAttempts < 2) {
System.out.println("Sorry, but '" + pesoOrYen + "' is not a valid currency type. Try again:");
notPesoOrYen = true;
}
++numAttempts;
};
int numatempts=0;
布尔值notPesoOrYen=true;
而(不超过3){
System.out.println(“您想转换成比索还是日元?”);
pesoOrYen=readKeyboard.nextLine();
if(比索等值信号案(“比索”)| |比索等值信号案(“日元”)){
notPesoOrYen=假;
}否则如果(numtempts<2){
System.out.println(“对不起,”+pesoOrYen+“”不是有效的货币类型。请重试:”;
notPesoOrYen=正确;
}
++Numatempts;
};
您试图以另一种方式重写此循环的具体原因是什么?起于here@Herman-你知道while
循环和do/while
循环之间的区别吗?它适用于我的Java类,我的教授要求使用while循环而不是do-while。你的while条件看起来是正确的。在此之前,请确保“notPesoOrYen==true”,我尝试了此操作,并且在运行程序后不会让用户输入另一种货币类型。Wellwhile
和do while
之间的唯一区别是第一次迭代,因此,如果在开始时conitnuation条件为true(这是您的情况),它们是严格等价的。
int numAttempts = 0;
boolean notPesoOrYen=true;
while (notPesoOrYen && numAttempts < 3) {
System.out.println("Do you want to convert to Peso or Yen?");
pesoOrYen = readKeyboard.nextLine();
if (pesoOrYen.equalsIgnoreCase("Peso") || pesoOrYen.equalsIgnoreCase("Yen")) {
notPesoOrYen = false;
} else if (numAttempts < 2) {
System.out.println("Sorry, but '" + pesoOrYen + "' is not a valid currency type. Try again:");
notPesoOrYen = true;
}
++numAttempts;
};