• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calculate coupon discount in Java  RSS feed

 
Francais Libble
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, i finished up a project here and seem to be running into some issues. I figured its my notation,
when i ask a user to enter a number, if its below 10, it should *only* print "No Coupon." but what appears to happen
is that the program prints "No Coupon" Plus the "discount coupon and % of purchase."

Its strange.

 
Liutauras Vilda
Sheriff
Posts: 4918
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Francais Libble wrote:...if its below 10, it should *only* print "No Coupon." but what appears to happen
is that the program prints "No Coupon" Plus the "discount coupon and % of purchase."

Its strange.



Hello Francais,

Well, it's not strange, it is a standard beginners mistake, to start cutting corners and do not indent code properly.

The outcome: bad code identation, poor code readability and missing curly braces, which cause unexpected program behaviour.

Start fixing your code from line 16 - unexpected curly brace.
Pay attention to line 20 - missing curly braces.

Also, check this out, you might find this helpful about if-then and if-then-else statements http://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html
 
Francais Libble
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Francais Libble wrote:...if its below 10, it should *only* print "No Coupon." but what appears to happen
is that the program prints "No Coupon" Plus the "discount coupon and % of purchase."

Its strange.



Hello Francais,

Well, it's not strange, it is a standard beginners mistake, to start cutting corners and do not indent code properly.

The outcome: bad code identation, poor code readability and missing curly braces, which cause unexpected program behaviour.

Start fixing your code from line 16 - unexpected curly brace.
Pay attention to line 20 - missing curly braces.

Also, check this out, you might find this helpful about if-then and if-then-else statements http://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html


thank you for the help! My mind was trying to cut some corners and i realized i had indentation mistakes which was causing the code to execute following arguments.
I tried correcting it and it works, but if you'd like to give advice on how to make it look cleaner, I'm all ears.

 
Liutauras Vilda
Sheriff
Posts: 4918
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


See how it is easier readable?

Beside that:
Class names by the convention suppose to start with capital letter. Take it serious, as it can become a bad habit.
Also, use variables cost, coupon, and x as a local variables. Declare them inside main method without static identifier. Don't worry at this stage why is that.


And welcome to the Ranch
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!