Reputation: 13
import javax.swing.JOptionPane;
public class SampleLT1 {
public static void main(String[] args) {
double dAllowance;
int days;
String strDAllowance, strDays;
do{
strDAllowance = JOptionPane.showInputDialog(null,"Please enter Daily Allowance $:(>0)",
"Allowance", JOptionPane.INFORMATION_MESSAGE);
dAllowance = Double.parseDouble(strDAllowance);
if (dAllowance<0) System.out.println("Daily Allowance shoould be >0. Please enter again.");
if (dAllowance==0) {//if enter daily Allowance as 0, exit the program
System.out.println("Thank you for using Allowance Calculating Application.");
System.exit(0);
}
}while (dAllowance<0);//if daily allowance is smaller than 0, enter again
do{
strDays = JOptionPane.showInputDialog(null,"Please enter No. of working days: (>0 and <42)",
"Allowance", JOptionPane.INFORMATION_MESSAGE);
days = Integer.parseInt(strDays);
if(days<0 || days>42) System.out.println("No. of working days should >0 and <42. Please enter again.");
if (days==0) {//enter 0 to quit the program
System.out.println("Thank you for using Allowance Calculating Application.");
System.exit(0);
}
}while (days<0 || days>42);//if days <0 or >42, enter days again
System.out.println("For an internship student who worked "+days +" days with daily allowance $"+dAllowance+", his total allowance is $"+calAllowance(dAllowance,days));
System.exit(0);
}
public static double calAllowance(double dailyAllowance, int noOfDays){
return dailyAllowance * noOfDays;//calculate and return total allowance
}
}
How do I use while loop, for loop to replace this do-while loop?
Can anyone teach me? Need to know for my upcoming test.
Upvotes: 0
Views: 3234
Reputation: 1911
I've converted the first do-while loop to a for loop and the code is below.
EDIT:
I think the version below is a little better than the previous one:
import javax.swing.JOptionPane;
public class SampleLT1 {
public static void main(String[] args) {
double dAllowance = 0;
int days;
String strDAllowance, strDays;
for(int i =-1;i<0;){
strDAllowance = JOptionPane.showInputDialog(null,"Please enter Daily Allowance $:(>0)",
"Allowance", JOptionPane.INFORMATION_MESSAGE);
dAllowance = Double.parseDouble(strDAllowance);
if (dAllowance<0){
System.out.println("Daily Allowance shoould be >0. Please enter again.");
}
if (dAllowance==0) {//if enter daily Allowance as 0, exit the program
System.out.println("Thank you for using Allowance Calculating Application.");
System.exit(0);
}
if (dAllowance>0) i++;
}//if daily allowance is smaller than 0, enter again
do{
strDays = JOptionPane.showInputDialog(null,"Please enter No. of working days: (>0 and <42)",
"Allowance", JOptionPane.INFORMATION_MESSAGE);
days = Integer.parseInt(strDays);
if(days<0 || days>42) System.out.println("No. of working days should >0 and <42. Please enter again.");
if (days==0) {//enter 0 to quit the program
System.out.println("Thank you for using Allowance Calculating Application.");
System.exit(0);
}
}while (days<0 || days>42);//if days <0 or >42, enter days again
System.out.println("For an internship student who worked "+days +" days with daily allowance $"+dAllowance+", his total allowance is $"+calAllowance(dAllowance,days));
System.exit(0);
}
public static double calAllowance(double dailyAllowance, int noOfDays){
return dailyAllowance * noOfDays;//calculate and return total allowance
}
}
Upvotes: 0
Reputation: 37845
You don't need to enter anything in a for loop. A completely empty for loop for(;;)
is the same as while(true)
. Any while loop can be converted to a for loop:
int i = 0;
while (i < 10) i++;
Is the same as:
int i = 0;
for ( ; i < 10; ) i++;
Because your example is do-while you can't get the identical logic without making a totally empty loop and doing the break yourself. A for/while also makes a conditional check before the loop enters but a do-while only does it after.
You can get something very similar but you must assign the control variable to something that meets the condition:
int days = -1;
for ( ; days < 0 || days > 42; ) {
}
But I don't really know why the instructor wants you to refactor the loop in to something redundant like this. Because you are checking all the possible values inside the loop you don't need the outer condition. You can just break. Otherwise you are making the same check twice. I would personally do the following (even over do-while):
double dAllowance;
String strDAllowance;
for ( ; ; ) {
strDAllowance = JOptionPane.showInputDialog(/* ~~~ */);
try {
dAllowance = Double.parseDouble(strDAllowance);
if (dAllowance > 0) {
break;
} else if (dAllowance == 0) {
System.exit(0);
}
} catch (NumberFormatException e) {}
/* notify and automatically continues */
}
If you really want to delve in to the deepest of "what is valid loop logic" consider the following:
while (
( dAllowance = Double.parseDouble(
JOptionPane.showInputDialog(
null,
"Please enter Daily Allowance $:(>0)",
"Allowance",
JOptionPane.INFORMATION_MESSAGE)
) ) < 0) {
System.out.println("Daily Allowance shoould be >0. Please enter again.");
}
if (dAllowance == 0) {
System.out.println(
"Thank you for using Allowance Calculating Application.");
System.exit(0);
}
Note that I only braced the while loop for readability. The braces should not be necessary. That could also of course be turned in to a for loop:
for (;
( dAllowance = Double.parseDouble(
JOptionPane.showInputDialog(
null,
"Please enter Daily Allowance $:(>0)",
"Allowance",
JOptionPane.INFORMATION_MESSAGE)
) ) < 0;
System.out.println("Daily Allowance shoould be >0. Please enter again.")
); // <- no body necessary
But that's the kind of answer you probably should not put down.
Upvotes: 2