Click here to Skip to main content
15,904,503 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
import java.util.Scanner;
public class Steam{

    public static void main(String[] args){
        int i=0,j=0;
        Scanner input = new Scanner(System.in);
        double ac[]={3,5,1.5,2};
        double a[] = new double[100];
        String cont, choose;
        double pay=0, hour=0;
        do{
            pay=0;
            System.out.println("Select the following: ");
            System.out.println("1. steam & sauna ($3)");
            System.out.println("2. Massage ($5)");
            System.out.println("3. Soft Drink ($1.5)");
            System.out.println("4. Fast Food ($2)");
            System.out.println("5. Payment");
            System.out.println("6. Exit");

            do{
                System.out.print("Choose: ");
                choose=input.nextLine();
                if(choose.equalsIgnoreCase("6")) break;
                if(choose.equalsIgnoreCase("1")){
                    a[0]=ac[0];
                }else if(choose.equalsIgnoreCase("2")){
                    a[0]=0;
                    System.out.print("Input hour(s): ");
                    hour=input.nextDouble();
                    a[1]=ac[1]*hour;
                }else if(choose.equalsIgnoreCase("3")){
                    a[2]=ac[2];
                }else if(choose.equalsIgnoreCase("4")){
                    a[3]=ac[3];
                }else if(choose.equalsIgnoreCase("5")){
                    for(j=0; j<i; j++){
                        pay+=a[j];
                        a[j]=0;
                    }
                    System.out.println("\nTotal Payment: $"+ pay);
                    break;
                }
                i++;
            }while(true);
            if(choose.equalsIgnoreCase("6")) break;
            System.out.print("Do you want to continue? (y/n): ");
            cont=input.nextLine();
        }while(cont.equalsIgnoreCase("y"));
    }
}
Posted

1 solution

If some call used to output "Choose" is put in the loop which breaks only if one chooses "6", isn't it obvious that "Choose" can appear any number of times? :-)

Maybe your question means "why does it show "Choose" twice, without any output between the two lines"? This is also obvious: because some iteration ends without any output, those under the options "1", "3" and "4". Also note that your "if" block does not have "else" branch. You could use it to write something like "valid options are '1'-'5'".

Now, let me tell you that main problem of your code is hard-coded immediate constants, such as "1", "2", etc. You repeat them, which is really bad for maintenance. Better use explicitly declared constant.

Also, let me tell you that the whole idea of using interactive input in a console-only program is a pretty bad idea. "Professional" console-only utilities almost never use such input. Instead, all input is accepted from the command line. If some piece of data is too big for a command line, some command-line parameters can be file names; and the user put all input in a file. It seems to overly complex, but 1) such utilities are not oriented to computer lamers, 2) interactive input is even worse, error-prone, irritating.

And the last, but not least, point: use the debugger; no, use the debugger. Every time you have the slightest concern on your runtime behavior, use the debugger. If you have a question on the behavior, first use the debugger, and then, if you still have a question, ask it.

—SA
 
Share this answer
 
v2
Comments
TorstenH. 29-Oct-14 6:12am    
+5
Sergey Alexandrovich Kryukov 29-Oct-14 13:45pm    
Thank you very much.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900