CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2018
    Posts
    2

    double variable help

    I'm modifying an already coded thing. It's just small. It computes the average of num1 and num2. I want to add a num3 that the user inputs manually after being asked by the program then to find the average of all 3 numbers. I have some code down but it's not working and I can't figure out how to fix it.

  2. #2
    Join Date
    Mar 2018
    Posts
    2

    Re: double variable help

    Here is the code by the way:

    Code:
    ///////////////////////////////////////////////// 
    //  Author: Joe Smith
    //  Modified by Mackenzie Cutler 
    //  Objective: 
    //  The following program should be modified  
    //  according to the assignment statement. 
    //  Date: Feb. 26, 2016 
    //  Date: Feb. 28, 2016 (modified)
    ////////////////////////////////////////////////
    
    import java.util.Scanner;
    public class Average3
    {
        public static void main(String[] args)
        {
            Scanner k = new Scanner(System.in);
            //  Create integer variables named num1 and num2    
            int num1 = 75;
            int num2 = 137;
            int num3 = 
            System.out.print("Please input the value of num3: ");
            double num3 = k.nextDouble();
        
            // Compute the average of num1 and num2
            double.class average =(num1+num2)/2.0;
    
            // Output the result
            System.out.println("The average of "+num1);
            System.out.println("and "+num2+" is "+average);
        }
    }
    Last edited by 2kaud; March 5th, 2018 at 01:04 PM. Reason: Added code tags

  3. #3
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: double variable help

    it's not working
    Please explain.
    Copy and post the console from when you execute the code and add some comments describing any problems.

    If there are error messages, copy the full text and paste it here.
    Norm

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: double variable help

    [When posting code, please use code tags. Go Advanced, select the formatted code and click '#'.]

    Cheers!

    Code:
    int num3 =
    That is an invalid statement. As num3 is defined later, I don't think you need that line.

    To find the average of 3 numbers, you need to sum these and then divide by 3.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured