Results 1 to 3 of 3
Thread: Java Question - Help!
- 04-05-2014, 02:47 AM #1
Member
- Join Date
- Apr 2014
- Posts
- 6
- Rep Power
- 0
Java Question - Help!
Hello guys, just wanted to say it's my first year in Java so I learn the basics at the moment.
So I got this java answer I've written to a question I was given to answer, and I don't know why the output is as it is.
Can someone help?
Here's the code (it would be great if someone would compile it to see the output):
Java Code:import java.util.*; class Shop { static Scanner reader=new Scanner(System.in); public static void main(String[]args) { int sumPrice=0, priceBeforeDis, counterItems=0, price, dis=0; boolean coupon=false, checkout=false, wantsToBuy=false; System.out.println(); System.out.println(" ~~~ Welcome to the shop! ~~~"); System.out.println("Would you like to buy something?"); String answer=reader.next(), answerForAnother; String[] yes = {"yes","yeS","yES","YES","YEs","Yes","YeS","yEs"}; String[] no = {"no","NO","No","nO"}; for(int i=0 ; i<yes.length ; i++) { if(answer.equals(yes[i])) { wantsToBuy=true; } } if(wantsToBuy=true) { System.out.println("Please enter the price of the first item you want to buy"); while(checkout=false) { if(counterItems>0) { System.out.println("Please enter the price of the next item"); } price=reader.nextInt(); while(price!=0 || price<0) { System.out.println("Please enter a vaild price number!"); price=reader.nextInt(); } sumPrice=sumPrice+price; counterItems++; System.out.println("Would you like to buy another item?"); answerForAnother=reader.next(); for(int i=0 ; i<no.length ; i++) { if(answerForAnother.equals(no[i])) { checkout=true; } } } priceBeforeDis=sumPrice; if(sumPrice>799) { dis=(sumPrice/800)*50; sumPrice=sumPrice-dis; } if(counterItems>3) { coupon=true; } if(dis>0) { System.out.println("The price for your purchase before a discount is "+priceBeforeDis+", but you got a "+dis+" Dollars discount!"); System.out.println("Now you need to pay only "+sumPrice+" Dollars for your purchase!"); } else { System.out.println("The price for your purchase is "+sumPrice+" Dollars."); } if(coupon=true) { System.out.println("You also got a 5 Dollars coupon for your next purchase!"); } System.out.println(); System.out.println("Thank you! Come again!"); } if(wantsToBuy=false) { System.out.println("Oh well.. so.. goodbye I guess!"); } } }
Thanks all!Last edited by Ben Bublil; 04-05-2014 at 02:40 PM.
- 04-05-2014, 02:13 PM #2
Senior Member
- Join Date
- Feb 2014
- Posts
- 219
- Rep Power
- 8
Re: Java Question - Help!
Please wrap your code with [code] tags so that it is easier to read, e.g.,
[code]
// your code here
[/code]
It preserves formatting and provides syntax highlighting, e.g.,
Java Code:// your code here
Just from a quick visual inspection of your code, the most obvious error is the use of the assignment operator (=) when you should be using the equality operator (==). See Operators (The Java™ Tutorials > Learning the Java Language > Language Basics). The errors are at:
Java Code:... if (wantsToBuy = true) { ... while (checkout = false) { ... if (coupon = true) { ... if (wantsToBuy = false) {
- 04-05-2014, 02:38 PM #3
Member
- Join Date
- Apr 2014
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
New to Java. Hello! and a question.
By tzzt123 in forum New To JavaReplies: 17Last Post: 05-13-2013, 02:08 AM -
Java question.
By immortal in forum New To JavaReplies: 8Last Post: 08-07-2012, 04:49 AM -
Java question?
By Zeronitic in forum New To JavaReplies: 1Last Post: 02-21-2012, 09:35 PM -
Java Question [Beginner Question]
By joker760 in forum New To JavaReplies: 3Last Post: 12-13-2011, 05:01 PM -
Java question
By TGH in forum New To JavaReplies: 12Last Post: 11-27-2009, 03:05 PM
Bookmarks