Results 1 to 3 of 3
  1. #1
    Ben Bublil is offline Member
    Join Date
    Apr 2014
    Posts
    5
    Rep Power
    0

    Question 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!");
         }
       }
    }
    (I work in Dr. Java)
    Thanks all!
    Last edited by Ben Bublil; 04-05-2014 at 02:40 PM.

  2. #2
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default 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
    Please also try to describe the problem you're facing. State the expected output, and the actual output from running the code.

    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) {

  3. #3
    Ben Bublil is offline Member
    Join Date
    Apr 2014
    Posts
    5
    Rep Power
    0

    Default Re: Java Question - Help!

    Quote Originally Posted by jashburn View Post
    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) {


    I CANT BELIEVE IT! SOOOOO STUPID MISTAKE! NOW EVERYTHING WORKS PERFECTLY!!! LOL
    Thank you very much! :D

Similar Threads

  1. New to Java. Hello! and a question.
    By tzzt123 in forum New To Java
    Replies: 17
    Last Post: 05-13-2013, 02:08 AM
  2. Java question.
    By immortal in forum New To Java
    Replies: 8
    Last Post: 08-07-2012, 04:49 AM
  3. Java question?
    By Zeronitic in forum New To Java
    Replies: 1
    Last Post: 02-21-2012, 09:35 PM
  4. Java Question [Beginner Question]
    By joker760 in forum New To Java
    Replies: 3
    Last Post: 12-13-2011, 05:01 PM
  5. Java question
    By TGH in forum New To Java
    Replies: 12
    Last Post: 11-27-2009, 03:05 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •