Using a For loop for repetition
I am trying to write a for-loop that lets the user enter how many times they want to repeat a given action.
Basically the program asks the user how many washers or stoves they want to buy then calculates the cost of those stoves or washers. I am trying to include a for-loop that will let the user keep repeating the action as many times as they would like.
Any feedback would be appreciated.
Re: Using a For loop for repetition
A normal for loop takes the following form
Code java:
for(declaration; condition; step)
or
for(int i = 0; i < 5; i++)
How would you make the item loop 6 times, 7, 8, 9? What part of the for loop determines how many times it will loop? Is it the declaration, condition, or step?
Answer this question and you are set. Just get an input from the user then set up the loop with the users input.
Re: Using a For loop for repetition
ok that makes sense, as long as the input doesn't exceed the condition, it will keep running correct?
Re: Using a For loop for repetition
Actually, as long as the condition is true the for loop will keep running. This can lead to some very interesting for-loops.
Code Java:
for (int i = 5; i >= 0; --i) // a for loop which counts down
{}
int count = 0;
for (int i = 0; count != 128; ++i) // a for loop which tests for a different number exactly equaling a number
{}
for(int i = 0;; ++i) // Java will automatically assume a true condition. This is an infinite for-loop
Re: Using a For loop for repetition
Oh ok. I am trying to nest the for loop inside a switch with two cases.
Code Java:
switch(choice)
{
case 1:
{
System.out.printf("Enter number of washers purchased: ");
washer=input.nextInt();
//for loop
totalWashers = a1.getPrice() * washer;
msg3 = String.format("You purchased %s washers for $%.2f\n",
washer, totalWashers);
JOptionPane.showMessageDialog(null, msg3);
break;
}
case 2:
{
System.out.printf("Enter number of stoves purchased: ");
stove = input.nextInt();
totalStoves = a2.getPrice() * stove;
How would i make the for loop keep running as long as the number of washers or stoves is not zero?
Re: Using a For loop for repetition
I would personally do something like this:
Code Java:
import java.util.*;
public class WashingMachine {
/**
* JavaProgrammingForums.com
*/
public static void main(String[] args) {
System.out.println("Washing Machines(1) Stove(2)");
Scanner input = new Scanner(System.in);
int choice;
while(input.hasNext()){
choice = input.nextInt();
if(choice == 0){
break;
}
if(choice == 1){
System.out.println("Washing Machines");
}
if(choice == 2){
System.out.println("Stoves");
}
}
}
}
The 'while loop' will continue to loop until 0 is entered.
Re: Using a For loop for repetition
Normally, I would discourage the use of break/continue statements used for something that could easily be fixed by changing the conditions. The reason is these statements (especially breaking to a label) encourage "spaghetti code", or code that's difficult to follow the logic of. While break/continue statements aren't anywhere near as bad as goto statements, they're still something you may want to avoid if possible. There are a few key exceptions to this rule (obviously having break statements in switches is almost always a must).
In this case, this problem could be fixed by using a "1 and a half" while loop, or a do-while loop.
Code Java:
// the do-while loop solution
do
{
System.out.println("Washing Machines(1) Stove(2)");
choice = input.nextInt();
if(choice == 1){
System.out.println("Washing Machines");
}
if(choice == 2){
System.out.println("Stoves");
}
}
while(input.hasNext() && choice != 0);
Re: Using a For loop for repetition