Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Quick, beginner-level Java question.

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Quick, beginner-level Java question.

    I need to modify this code for my Java class and I'm doing something wrong because it's not compiling correctly.

    I need to modify the code so that when the user ends the program it displays the number of invoices, the average invoice amount, and the average discount amount.

    I'm not sure if I'm just not initializing the variables correctly, or if I'm doing it in the wrong place ...

    Here is the code:

    import java.util.Scanner;

    public class InvoiceApp
    {
    public static void main(String[] args)
    {

    System.out.println("Welcome to the Invoice Total Calculator");
    System.out.println(); // print a blank line

    Scanner sc = new Scanner(System.in); // do I need to initialize variables here? Or would I just work with the variables below?

    String choice = "y";
    while (!choice.equalsIgnoreCase("n"))
    {

    System.out.print("Enter subtotal: ");
    double subtotal = sc.nextDouble();

    double discountPercent = 0.0;
    if (subtotal >= 200)
    discountPercent = .2;
    else if (subtotal >= 100)
    discountPercent = .1;
    else
    discountPercent = 0.0;
    double discountAmount = subtotal * discountPercent;
    double total = subtotal - discountAmount;

    String message = "Discount percent: " + discountPercent + "\n"
    + "Discount amount: " + discountAmount + "\n"
    + "Invoice total: " + total + "\n";
    System.out.println(message);

    System.out.print("Continue? (y/n): ");
    choice = sc.next(); //I'm assuming that the commands to print the number of invoices, etc., would go here?
    System.out.println();
    }
    }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Quick, beginner-level Java question.

    I'm not really sure what you want us to do. You haven't told us what the actual problem is- are you seeing an Exception? Strange behavior? What did you expect this program to do? What did it do instead?

    Also, when posting code, please use the code or highlight tags. Nobody likes to read unformatted code. Use any other post as an example, or consult the link in my signature.

    As for when you need to initialize the variables, it really depends on what you're going to do with them.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Quick easy Java question.
    By DHG in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 25th, 2011, 03:59 PM
  2. scope - quick question
    By bbr201 in forum Java Theory & Questions
    Replies: 4
    Last Post: July 28th, 2010, 08:30 AM
  3. quick question - new keyword
    By bbr201 in forum Java Theory & Questions
    Replies: 2
    Last Post: July 18th, 2010, 09:43 PM
  4. Quick Question about Mergesort
    By Shadow703793 in forum Algorithms & Recursion
    Replies: 4
    Last Post: March 4th, 2010, 05:48 PM
  5. Quick JComboBox Event Question
    By -tr in forum AWT / Java Swing
    Replies: 2
    Last Post: December 12th, 2009, 05:35 PM