Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8785883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:28:11+00:00 2026-06-13T21:28:11+00:00

Here is my code. I am not sure how to exit the program using

  • 0

Here is my code. I am not sure how to exit the program using a return value. Any ideas? THis is the last step from my assignment.
Important areas are marked with ///////
I hear return works but when I change void in main to int, the program says main must be void.

import java.util.Scanner;


public class CommissionCalculator {

    public static void main(String args[]) {
        // Initialize a Scanner to read input from the command line
        Scanner ItemSelect = new Scanner(System.in);
        double comp = 200.00;
        double item1 = 239.99;
        double item2 = 129.75;
        double item3 = 99.95;
        double item4 = 350.89;
        double comm = 0.09;
        int choice;


        /* Note that we'll be doing this at least once and most likely multiple times...
         * Prompt the user with a menu of the four items and their values (this information is included in the problem statement)
         */
        System.out.println("Item\tValue");
        System.out.println("1\t$239.99");
        System.out.println("2\t$129.75");
        System.out.println("3\t$99.95");
        System.out.println("4\t$350.89"); 
        /* Display the user's current compensation */
        System.out.printf("Current compensation: $%.2f", comp);


        /* 
         * Prompt and take input from the user (you may assume that they will only enter int values)
         * They'll enter an item number (1 - 4) to record its sale or 0 to exit
         * 
         * NOTE: THE U0SER DOES NOT ENTER PRICES DIRECTLY... THEY ENTER ITEM NUMBERS TO INDICATE WHAT WAS SOLD
         * NOTE: THE USER MAY ENTER THE SAME ITEM NUMBRER MULTIPLE TIMES
         * 
         * If the user provides invalid input (a value other than 0 - 4) display "ERROR: Invalid input!" and prompt them again
         */
        do
        {

            System.out.print("\nPlease select an item from the " +
                               "list above (or enter 0 to exit): ");
            choice = ItemSelect.nextInt();

            {   
                if (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 0)
                {
                    do
                      {
                        System.out.print("ERROR: Invalid Input!\n");
                        System.out.println("Item\tValue");
                        System.out.println("1\t$239.99");
                        System.out.println("2\t$129.75");
                        System.out.println("3\t$99.95");
                        System.out.println("4\t$350.89");
                        System.out.printf("Current compensation: $%.2f", comp);
                        System.out.print("\nPlease select an item from the " +
                                   "list above (or enter 0 to exit): ");
                        choice = ItemSelect.nextInt();
                        if (choice == 1)
                        {
                            comp += (comm * item1);
                            System.out.printf("Current compensation: $%.2f", comp);
                        }
                        if (choice == 2)
                        {
                            comp += (comm * item2);
                            System.out.printf("Current compensation: $%.2f", comp);
                        }
                        if (choice == 3)
                        {
                            comp += (comm * item3);
                            System.out.printf("Current compensation: $%.2f", comp);
                        }
                        if (choice == 4)
                        {
                            comp += (comm * item4);
                            System.out.printf("Current compensation: $%.2f", comp);
                        }
                        if (choice == 0)
                        {
                            System.out.printf("Total Earnings: $%.2f", comp);
                            System.exit(0); ///////
                        }

                      }while (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 0); 

                }
                else
                {
                    if (choice == 1)
                    {
                        comp += (comm * item1);
                        System.out.printf("Current compensation: $%.2f", comp);
                    }
                    if (choice == 2)
                    {
                        comp += (comm * item2);
                        System.out.printf("Current compensation: $%.2f", comp);
                    }
                    if (choice == 3)
                    {
                        comp += (comm * item3);
                        System.out.printf("Current compensation: $%.2f", comp);
                    }
                    if (choice == 4)
                    {
                        comp += (comm * item4);
                        System.out.printf("Current compensation: $%.2f", comp);
                    }
                    if (choice == 0)
                    {
                        System.out.printf("Total Earnings: $%.2f", comp);
                        System.exit(0);
                    }
                }

            }
        }while (choice != 0);

        /* After the user enters 0, display the salesperson's earnings in the format "Total earnings: $NNN.NN" and exit
         * For example, if the salesperson sold two item 3s this week the final output would be "Total earnings: $217.99"
         */
        if (choice == 0)
        {
            System.out.printf("Total Earnings: $%.2f", comp);
            System.exit(0);   ///////
        }
        ItemSelect.close();
        System.exit(0);    ///////
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:28:12+00:00Added an answer on June 13, 2026 at 9:28 pm

    As long as you don’t need to return custom exit code (other than 0, as returned by System.exit(0)) and don’t start new threads, you can terminate your program by doing

    return; 
    

    in your main() method. Note that there is no value returned, so that you don’t need to change your main() method from void to int.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Not sure how to do this, here is my code Dim oWeb As New
Here's my fiddle : http://jsfiddle.net/JWww3/ I'm not sure to understand why this code is
not sure why I find this so difficult. Here is the html code: <tr>
I'm not sure what the problem is here, I've inherited some code form Zend
What's the proper syntax here? If (@timestamp < (Select PromoStartTimestamp From @promo)) RAISERROR('Code not
Code is not running on .click when I have this: $(.cancel).click(function() { alert(got here);
Got this line of code here but its not working. private void Button_Click(object sender,
I have some code that compiles, but won't link. I'm not sure why this
What am I doing wrong here? Declare @starttimestamp datetime = getdate(); RAISERROR(N'Code not valid
Here's my code that is not working: print To: ; my $to=<>; chomp $to;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.