Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9214685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:01:28+00:00 2026-06-18T02:01:28+00:00

So I am trying to write a simple program for my Java class and

  • 0

So I am trying to write a simple program for my Java class and I have asked the teacher for help however, this is a distance learning class and am not getting much help from that direction. So here I am.

The problem we have been asked to solve is to create a program that will
(1) ask the user for a number ,
(2) find the sum of all user defined numbers ,
(3) find the average of these numbers ,
(4) then out put them back to the user.

I use the numbers not only to line through things I have finished but because they must be separate modules to be called.

How can i get the userNum variable to update durring the while statments. Currently they are not. OR is there a simpler way to do this that im overlooking. Any help is greatly appreciated. Thank you.

public class P2 {

   public static void main(String[] args) {

        int userNumCount = 1;

        double userNum = input();
        double userSum = sum(userNum);
        double userAverage = average(userSum, userNumCount);

        sum(userNum);

        while (userNum != 0 && sum(userNum) <= 100){
            ++userNumCount;          
            output(userSum, userAverage);
            input();
            sum(userNum);
            average(userSum, userNumCount);
        }//end else
        while (userNum != 0 && sum(userNum) >=100){
            ++userNumCount;
            JOptionPane.showMessageDialog (null, "Warning, your sum is currently over 100!!!!!"); 
            output(sum(userNum), userAverage);
            input();
            sum(userNum);
            average(userSum, userNumCount);
    }//end else
        if (input() == 0){
            output(userSum, userAverage);
            JOptionPane.showMessageDialog (null, "Thank you for using this program have a nice day.");
    }//end else if


    }//end main module
    public static double input(){
        String userNumString;
        userNumString = JOptionPane.showInputDialog("Please enter your number or input 0 to end the program.");
        double userInput = Double.parseDouble (userNumString); 
        return userInput;
    }//end input module
    public static double sum(double userNum){  
        double userSum =+userNum;
        return userSum;
    }//end sum module
    public static double average (double userSum, int userNumCount){
        double userAverage = userSum/userNumCount;
        return userAverage;
    }//end average module
    public static void output (double userSum, double userAverage){
        JOptionPane.showMessageDialog (null, "The sum of the numbers input so far is: " + userSum + ". And the Average is " + userAverage + "." );
    }//end output module
}//end class
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:01:29+00:00Added an answer on June 18, 2026 at 2:01 am

    This is incorrect.

    public static double sum(double userNum){  
        double userSum =+userNum;
        return userSum;
    }
    

    In a basic pseudo-language this is happening each time this method is called

    receive userNum
    create userSum
    set userSum = 0
    add userNum to userSum
    give userSum
    

    each time the method will return the value it is given, not the running total I think you are expecting.

    If you have two variables declared with the same name, they are still different. What you want to do is refer to the same variable, to do this you need to have the references in the scope where the variable is declared.

    to get the running total

    public class P2 {
    
        public static double userSum = 0; 
        //because this is a public member it's scope is global i.e. you can refer to it anywhere.
    
        ...
        public static void main(String[] args) {
        ... 
        /* do not declare userSum here.
           if you do any reference to userSum will use the local variable 
           not the global variable.
           double userSum = 0; <-- declare local variable it's scope is until the 
                                   end of this method nothing outside the method 
                                   can see it but you can pass it as a parameter
           userSum = 1; <-- local variable is set to 1
           P2.userSum = 2; <-- local variable is set to 1 global is set to 2
        */
           input();// for this the input method is called and returns a value,
                   // but you haven't said to put it anywhere so java will throw it away
                   // all that effort for nothing.
    
           userNum = input(); // in this case it will put the new value in userNum.
        }
    
        public static double sum(double userNum){  
            userSum =+userNum; // do not declare userSum use it from the class context
            return userSum;
        }
    
        ...
    

    }

    for further reference scope

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this strange problem. I'm trying to write a simple Hello World java
I am trying to write a simple java program which returns me all the
I'm trying to write a simple stock check program, and I have a Table
I have been trying to write a Java program which connects to mysql db
I have started learning Java and am having problem with a simple program that
I'm trying to write a simple java applet program, but it seems that I'm
I'm trying to write a simple java program in Eclipse that prints these four
I'm learning Java, and I'm trying to write a program that can read from
I'm new to java and trying to write a simple program using JDBC. It
I'm new to java and am trying to write a simple program that basicly

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.