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:
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";
System.out.print("Enter subtotal: ");
double subtotal = sc.nextDouble();
double discountPercent = 0.0;
if (subtotal >= 200)
discountPercent = .2;
else if (subtotal >= 100)
discountPercent = .1;
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.print("Continue? (y/n): ");
choice = sc.next(); //I'm assuming that the commands to print the number of invoices, etc., would go here?
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.