Stuck with Switch

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hhhhhhhh
    New Member
    • Oct 2007
    • 12

    Stuck with Switch

    I am a new member.
    I got stuck using Switch in java. Could someone help me?

    This is my code:
    Thanks




    [CODE=java]import java.util.Scann er;

    public class MailOrder {

    public static void main(String[] args) {

    Scanner input = new Scanner( System.in );//declare Scanner object

    int productID=0;//productID
    double totalValue = 0; //total value of the order
    double Product1 = 2.98;
    double Product2 = 4.50;
    double Product3 = 6.87;

    System.out.prin tln("Enter product number (1-3): " );
    productID = input.nextInt() ; //read the product ID

    // determine the number sold of the item
    System.out.prin tln( "Enter quantity ordered: " );
    int quantity = input.nextInt() ;

    //please complete the switch statement
    switch (productID)
    {
    case 1:
    quantity += quantity;
    break;

    case 2:
    quantity += quantity;
    break;

    case 3:
    quantity += quantity;
    break;

    } // end switch

    if (totalValue !=0)
    System.out.prin tln ("Total Valude of this order is "+ totalValue);
    }

    }[/CODE]
    Last edited by Ganon11; Oct 4 '07, 01:11 AM. Reason: Please use the [CODE] tags provided.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    What are you having trouble with?

    Comment

    • hhhhhhhh
      New Member
      • Oct 2007
      • 12

      #3
      Thanks for quick reply.

      I do not know how to set up the formula. When I run the file, it looks like this:

      init:
      deps-jar:
      compile-single:
      run-single:
      Enter product number (1-3):



      When I enter information, it becomes this:


      init:
      deps-jar:
      compile-single:
      run-single:
      Enter product number (1-3):
      1
      Enter quantity ordered:
      3
      BUILD SUCCESSFUL (total time: 6 minutes 31 seconds)



      Can you help how to figure out?
      Many thanks.

      Comment

      • madhoriya22
        Contributor
        • Jul 2007
        • 251

        #4
        Originally posted by hhhhhhhh
        Thanks for quick reply.

        I do not know how to set up the formula. When I run the file, it looks like this:

        init:
        deps-jar:
        compile-single:
        run-single:
        Enter product number (1-3):


        When I enter information, it becomes this:


        init:
        deps-jar:
        compile-single:
        run-single:
        Enter product number (1-3):
        1
        Enter quantity ordered:
        3
        BUILD SUCCESSFUL (total time: 6 minutes 31 seconds)


        Can you help how to figure out?
        Many thanks.
        Hi,
        Can tell us what are you trying to do with ur program ? And how come during compilation it is asking for input?
        Always use code tags instead of making the text bold.

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by hhhhhhhh
          Thanks for quick reply.

          I do not know how to set up the formula. When I run the file, it looks like this:

          init:
          deps-jar:
          compile-single:
          run-single:
          Enter product number (1-3):



          When I enter information, it becomes this:


          init:
          deps-jar:
          compile-single:
          run-single:
          Enter product number (1-3):
          1
          Enter quantity ordered:
          3
          BUILD SUCCESSFUL (total time: 6 minutes 31 seconds)



          Can you help how to figure out?
          Many thanks.
          The program does exactly what you told it to do.
          What was your expected output?

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            When I take a "bird's eye" view on that code I see:

            [code=java]
            int totalValue= 0;

            // a whole lot of things that all have nothing to do with
            // 'totalValue', i.e. it isn't changed at all by any of those
            // complicated statements and finally we reach:

            if (totalValue != 0) // sure, right
            ...
            [/code]

            kind regards,

            Jos

            Comment

            • hhhhhhhh
              New Member
              • Oct 2007
              • 12

              #7
              I do not know how to set a formula.

              I want to buy, for example 2* Product1 + 5*Product2 + 3*Product3 and the print will show the total amount of all products.

              Comment

              • hhhhhhhh
                New Member
                • Oct 2007
                • 12

                #8
                Originally posted by hhhhhhhh
                I am a new member.
                I got stuck using Switch in java. Could someone help me?

                This is my code:
                Thanks




                [CODE=java]import java.util.Scann er;

                public class MailOrder {

                public static void main(String[] args) {

                Scanner input = new Scanner( System.in );//declare Scanner object

                int productID=0;//productID
                double totalValue = 0; //total value of the order
                double Product1 = 2.98;
                double Product2 = 4.50;
                double Product3 = 6.87;

                System.out.prin tln("Enter product number (1-3): " );
                productID = input.nextInt() ; //read the product ID

                // determine the number sold of the item
                System.out.prin tln( "Enter quantity ordered: " );
                int quantity = input.nextInt() ;

                //please complete the switch statement
                switch (productID)
                {
                case 1:
                quantity += quantity;
                break;

                case 2:
                quantity += quantity;
                break;

                case 3:
                quantity += quantity;
                break;

                } // end switch

                if (totalValue !=0)
                System.out.prin tln ("Total Valude of this order is "+ totalValue);
                }

                }[/CODE]
                I made it work. I changed line #26, 30, and 34.
                However, I can enter only one product at a time. For example, the message asks: Enter product number (1-3), I can enter only 1 or 2 or 3.

                How do I set up to NOT enter 4 or to enter 1 and 2 and 3 a tthe same time?


                When I run the file, I got a message Enter product number (1-3). I entered 1 in the input box. However, the output looks like this:

                Enter product number (1-3):
                1

                How do I move # 1 after the colon? For example: Enter product number (1-3): 1

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by hhhhhhhh
                  I made it work. I changed line #26, 30, and 34.
                  However, I can enter only one product at a time. For example, the message asks: Enter product number (1-3), I can enter only 1 or 2 or 3.

                  How do I set up to NOT enter 4 or to enter 1 and 2 and 3 a tthe same time?


                  When I run the file, I got a message Enter product number (1-3). I entered 1 in the input box. However, the output looks like this:

                  Enter product number (1-3):
                  1

                  How do I move # 1 after the colon? For example: Enter product number (1-3): 1
                  Just use an if else to make sure that the number supplied is either 1,2 or 3.

                  Comment

                  • Ganon11
                    Recognized Expert Specialist
                    • Oct 2006
                    • 3651

                    #10
                    For your output message question, you are using System.out.prin tln(). The println stands for print line, so it always moves to a new line once it is finished outputting the String inside. You should use System.out.prin t() instead. It does exactly the same stuff as println, but doesn't print a newline character at the end. This will result in your desired output.

                    Comment

                    • hhhhhhhh
                      New Member
                      • Oct 2007
                      • 12

                      #11
                      Originally posted by Ganon11
                      For your output message question, you are using System.out.prin tln(). The println stands for print line, so it always moves to a new line once it is finished outputting the String inside. You should use System.out.prin t() instead. It does exactly the same stuff as println, but doesn't print a newline character at the end. This will result in your desired output.
                      Thanks for your idea. I tried but still did not work as I expected.
                      Here is the code

                      System.out.prin tln("\nEnter product number (1-3):");
                      productID = input.nextInt() ; //read the product ID

                      // determine the number sold of the item
                      System.out.prin tln( "Enter quantity ordered:" );
                      int quantity = input.nextInt() ;


                      And here is the output:

                      Enter product number (1-3):
                      2
                      Enter quantity ordered:
                      2
                      Total Valude of this order is $9.0

                      Comment

                      • Ganon11
                        Recognized Expert Specialist
                        • Oct 2006
                        • 3651

                        #12
                        Originally posted by hhhhhhhh
                        Thanks for your idea. I tried but still did not work as I expected.
                        Here is the code

                        [CODE=java]System.out.prin tln("\nEnter product number (1-3):");
                        productID = input.nextInt() ; //read the product ID

                        // determine the number sold of the item
                        System.out.prin tln( "Enter quantity ordered:" );
                        int quantity = input.nextInt() ;[/CODE]
                        OK, you're still using System.out.prin tln().

                        Change this to System.out.prin t();

                        Comment

                        Working...