Results 1 to 5 of 5
  1. #1
    FAkamo is offline Member
    Join Date
    Oct 2011
    Posts
    6
    Rep Power
    0

    Default Quick question about my program

    I have this program and I'm almost done with it except I keep getting the wrong output. My Total Cost keeps coming out to 0, and I dont know if thats because my calculation is wrong or something. If someone could help me real quick I'd really appreciate it. Thanks.

    Java Code:
    import java.util.Scanner;
    public class FriendlyBookStore
    {
    
    
        public static void main(String[] args)
        {
        	Scanner input = new Scanner(System.in);
        	int choice = 0;
        	int noBooks = 0;
        	int novel = 0;
        	int usedTextBook = 0;
    		int totalCost = 0;
    
        	System.out.print("Choices:\n1- Novel\n2- Used Text Book\n");
    
        	System.out.print("\nSelect your choice: ");
        	choice = input.nextInt();
    
        	System.out.print("\nHow many books are needed? ");
        	noBooks = input.nextInt();
    
        	if( choice == 1)
        	{
        		novel = 6;
        	}
    
        	else
        	{
    
        		usedTextBook = 12;
        	}
    
    
    
    		System.out.printf("\nTotal Cost: %d", totalCost);
    
    
    
    
        } //end Main
    
        public static int calculatePrice(int choice2, int noBooks2 )
        {
    
    		int totalCost;
    
        	totalCost = choice2 * noBooks2;
    
    		return totalCost;
    
        }
    
    
    }
    Last edited by pbrockway2; 01-24-2012 at 07:32 AM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Quick question about my program

    When you post code, please use the code tags: put [code] at the start of the code and [/code]. This will help make the code formatted and readable.

    -----

    In main() you have a totalCost variable, but you don't ever call calculatePrice() and assign the value it returns to totalCost. As a result totalCost stays zero as you've found.

  3. #3
    FAkamo is offline Member
    Join Date
    Oct 2011
    Posts
    6
    Rep Power
    0

    Default Re: Quick question about my program

    Ok will do, and thanks. Also I'm currently not on a computer where I can try to see if it works, but from my understanding your saying I have to put calculatePrice(totalCost); in my main method correct?

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Quick question about my program

    Try that and see what happens. In general, though, you *must* pass a method the number of arguments it is declared with and you probably should do something with the value it returns.

    But that will become clearer when you get near a computer with a compiler.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Quick question about my program

    @RajAryan55: what you posted belongs in the reviews/advertising forum. Feel free to go there, start a new thread and repost the content.

    Do not post advertising in the technical forums.

Similar Threads

  1. Quick Question...
    By FatalSylence in forum New To Java
    Replies: 4
    Last Post: 10-15-2010, 03:38 PM
  2. quick question
    By vouslavous in forum Java Applets
    Replies: 4
    Last Post: 04-24-2009, 09:35 PM
  3. Hello everyone! quick question.
    By irishhokie in forum New To Java
    Replies: 5
    Last Post: 04-03-2009, 05:13 AM
  4. One last quick question
    By jigglywiggly in forum New To Java
    Replies: 7
    Last Post: 01-26-2009, 09:53 AM
  5. Quick Question
    By Spenc in forum New To Java
    Replies: 3
    Last Post: 09-22-2008, 03:26 PM

Posting Permissions

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