473,781 Members | 2,280 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple stats with input numbers program

6 New Member
writing a program that asks how many numbers the users want to enter, then input an integer for the amount of numbers said, output: number of integers entered, sum of them, average of them, maximum value entered and min value entered. the min is where i am having a problem i can get everything else to print out but i am doing something wrong in my while loop can someone please help me.

Expand|Select|Wrap|Line Numbers
  1.   import javax.swing.*; // gets option pane
  2.  
  3.     class Program3    {
  4.  
  5.        public static void main (String[] args)  {
  6.  
  7.          String userInput;
  8.          String userInput2;
  9.          String output = "";
  10.          int     amountEntered;
  11.          int      integerEntered;
  12.          int         sum = 0; 
  13.          float average = 0; 
  14.          int        maxNum;
  15.          int      minNum;
  16.          int     number;
  17.          float         numberOfIntegers = 0;
  18.  
  19.       // asking for input
  20.          userInput = JOptionPane.showInputDialog("How many numbers would you like to enter?");   
  21.  
  22.          if (userInput == null){
  23.             JOptionPane.showMessageDialog(null, "Cancel was selected. Program will exit.",
  24.                         "Program3 (Edward Powers)", JOptionPane.ERROR_MESSAGE);        
  25.             System.exit(0);
  26.          }
  27.  
  28.          amountEntered = Integer.parseInt(userInput);
  29.          numberOfIntegers = amountEntered;
  30.  
  31.          if (amountEntered < 0) {
  32.             JOptionPane.showMessageDialog(null, "You entered a negative number, so the program terminates.",
  33.                         "Program3 (Edward Powers)", JOptionPane.ERROR_MESSAGE);        
  34.             System.exit(0);
  35.          }
  36.  
  37.          if (amountEntered == 0) {
  38.             JOptionPane.showMessageDialog(null, "You requested no numbers, so there \n" + 
  39.                                                             "is no sum, average, minimum or maximum.",
  40.                         "Program3 (Edward Powers)", JOptionPane.ERROR_MESSAGE);        
  41.             System.exit(0);
  42.          }
  43.  
  44.  
  45.  
  46.          while(amountEntered > 0)
  47.          {
  48.  
  49.             userInput2 = JOptionPane.showInputDialog("enter an integer:");
  50.  
  51.             if (userInput2 == null)
  52.             {
  53.                JOptionPane.showMessageDialog(null, "Cancel was seleceted, program will exit.",
  54.                   "Program 3 (Edward Powers)", JOptionPane.ERROR_MESSAGE );
  55.                System.exit(0);
  56.             }
  57.  
  58.             integerEntered = Integer.parseInt(userInput2);
  59.                     number = integerEntered;
  60.                  maxNum = number;
  61.                     minNum = number;
  62.  
  63.                if (number > maxNum)
  64.             {
  65.                maxNum = number;
  66.             }
  67.             if (number < minNum)
  68.             {
  69.                minNum = number;
  70.             }
  71.  
  72.             sum += integerEntered;
  73.             average = sum / numberOfIntegers;
  74.  
  75.             amountEntered--;
  76.  
  77.             output = "Number of integers entered :" + (int)numberOfIntegers + "\n" +
  78.                    "Sum of those integers is : " + sum + "\n" +
  79.                    "Average of those integers is : " + average + "\n" +
  80.                    "Maximum value of those integers : " + maxNum + "\n" +
  81.                    "Minimum value of those integers : " + minNum ;
  82.  
  83.          }         
  84.  
  85.  
  86.  
  87.  
  88.          JOptionPane.showMessageDialog(null,output,"Program3 (Edward Powers)", 
  89.                         JOptionPane.PLAIN_MESSAGE);
  90.  
  91.       }
  92.    }
Sep 29 '07 #1
3 1860
powerej
6 New Member
my true problem is i know i can initialize my max and min values to like a million and i will get the correct min value but my teacher said this i weak programming and i cant think of a way to initialize these values with an arbitrary number
Sep 29 '07 #2
Ganon11
3,652 Recognized Expert Specialist
powerej - Please don't double post your question. There's no need for 2 threads addressing the same topic.
Sep 30 '07 #3
Ganon11
3,652 Recognized Expert Specialist
As to your question, think about the situation where the user has entered only 1 number. As of right now, what's the largest number? The only number the user has entered. And the smallest number? It also happens to be the only number the user has entered. Once you handle this simple case, your while loop will take care of the rest.
Sep 30 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

13
4702
by: Roy Riddex | last post by:
2nd post of the day! I'm just learning about Arrays at College and have met a problem. I have 5 text boxes for number input, a command button to add the numbers to the array, and a command button which displays the array contents in 5 labels. My program works fine. The problem is that I use a simple If statement to check for any empty boxes. I've to cut the IF statement and use a For Each...Next statement to check for empty text boxes but...
17
6524
by: savesdeday | last post by:
In my beginnning computer science class we were asked to translate a simple interest problem. We are expected to write an algorithm that gets values for the starting account balance B, annual interest rate I, and annual service charge S. Your algorithm would then compute and print out the total amount of interest earned during the year and the final account balance at the end of the year (assuming that interest is compounded monthly, and...
6
4964
by: hoover_richard | last post by:
I am a newbie to C++ and I need help with a simple program I am trying to write. My program is designed to print all of the odd integers contained in an array and output the sum of the odd integers. My code is listed below, but the problem is that my output of sum is wrong. For example, I am using 1347830 for my integers and the program outputs 373 and after adding 373 you should get 13 for the total, but that doesn't happen. Any help...
31
1888
by: Mike | last post by:
I'm after a *simple* script to log each visitor to a website. All I actually need is the visitor's reported IP address whether genuine, proxied or hidden. Their reported browser type would be an advantage, regardless of what browser is actually used. Anyone know of anything simple and preferably free that will do this? The site is hosted on a FreeBSD server with access to CGI-Bin but it does not seem to have a server-side stats log. ...
3
5213
by: Paul | last post by:
I want to make a simple calculator program but dont know where to get started. This is not GUI but a simple terminal program. It would get input like this Enter number: 5 + 10
0
1263
by: Utter Newbie | last post by:
I know we have access to log files and web statistics can be pulled from those. But the guy who wants it built was thinking we should keep track of every time a page is hit seperately in MS sql server. He's thinking it would be better for clients to be able to tell how many page hits we're getting whenever they want - mainly concerned with advertising banners (how many views they are getting). I'm concerned about hitting the database for...
10
1712
by: Aravindh | last post by:
A 'C' program that takes two numbers and produces two other numbers. None of the four numbers must be similar. printf ("%d%d", & num1, & num2) ; if (num1 > num2) { num1 ++ ; num2 -- ; } else { num1 --; num2 ++ ; }
1
3333
by: gzeng | last post by:
Hi Everybdy: I am a beginner in C#. I'd like to write a simple online calculator in C#. This calculator will ask a user to input two numbers and then add them and display the result. So, it has two text boxes for a user to input two numbers. It also has a button for the user to get the result after adding these two numbers from input. Can somebody write such a program in C#? I think the program is small. How do I make it online so that...
7
3321
by: isinc | last post by:
I have an assignment that I'm working on and am having trouble. Not too familiar with graphics. Any help/guidance would be much appreciated to see if what I have so far is okay and what I should do for the loop part. I will post assignment details and what i have so far. You are given a dictionary named Stats, which is part of the template provided for this assignment. The template can be found at...
0
9636
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10139
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10075
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9931
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8961
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5504
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2869
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.