[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Sum Of Numbers

Posted on 2009-07-01
2
Medium Priority
?
1,982 Views
Last Modified: 2013-12-14
Im trying to write a program that asks the user for a positive nonzero integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user entered a 50, the loop will find the sum of 1,2,3,4,....50.

i think the code i posted may be wrong but ive spent hours trying to figure it out and my teacher isnt helpful at all. :(

i really would appreciate it if a full solution code is posted. This will help me study for my other programs im having trouble with.
public class SumOfNumbers
{
        public static void main( String [] args)
        {
                
            double nonZero;
            double numbers;
            double totalNumbers;
            String input;
                
        
            for( int count = 1; count <= nonZero; count++ )
            {
                input = JOptionPane.showInputDialog(null, "Please enter a nonzero positive integer.");
 
              nonZero = Double.parseDouble(input);
 
            }   
                
            JOptionPane.showMessageDialog(null, "The sum of the integers is: " + nonZero);
        }
}

Open in new window

0
Comment
Question by:AgentC4
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Accepted Solution

by:
Opalelement earned 2000 total points
ID: 24760313
Not sure if this will work since I don't have a java environment to test in ATM, but it has comments so you can see what it is supposed to do.
public class SumOfNumbers
{
        public static void main( String [] args)
        {
            double number; // The entered number after it is parsed
            double totalNumber = 0; // Total after adding
            String input; // Input from the input dialog
                
            input = JOptionPane.showInputDialog(null, "Please enter a nonzero positive integer."); // Ask for the number
            number = Double.parseDouble(input); // change it to a number
            for( int count = 1; count <= number; count++ )
            {
              totalNumber += count; // Loop through and keep adding the numbers
            }   
                
            JOptionPane.showMessageDialog(null, "The sum of the integers is: " + totalNumber); // Show the final
        }
}

Open in new window

0
 

Author Closing Comment

by:AgentC4
ID: 31599057
You're a life saver, thank you so much yes this works perfectly. ill definitely study it and work more and do examples.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question