![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Sep 2005
Location: Anchorage, Alaska
Posts: 37
Rep Power: 0
![]() |
Help with program that takes in a list of numbers.
I'm taking a Programming Concepts class this semester and the class is based on Java. Right now I'm working on the first part of the second assignment but my code won't run, or perhaps it will run but it doesn't do what I need it to do. Can someone look at my code and tell me what I did wrong? The program is supposed to gain a list of numbers through user input then output the average, max, and min of the list.
You don't have to fix it for me just kind of point me in the right direction. Any help is greatly appreciated. ![]() import java.util.*;
public class ProgProj3
{
public static void main(String[] args)
{
final Scanner keyIn = new Scanner(System.in);
double sum = 0;
double nextNum = 0;
double max = keyIn.nextDouble();
double min = max;
double next = 0;
int amountOfNumbers = 0;
String answer;
do
{
System.out.println();
System.out.println("Please enter in a list of numbers");
System.out.println("ending the list with a negative number.");
while (next >= 0)
{
next = keyIn.nextDouble();
nextNum = next;
if (next > max)
max = next;
if (next < min)
min = next;
sum = sum + nextNum;
amountOfNumbers++;
next = keyIn.nextDouble();
nextNum = next;
}
if (amountOfNumbers > 0)
System.out.println("Your average is: " + (sum/amountOfNumbers));
else
System.out.println("No numbers to average!");
System.out.println("Your highest number is: " + max);
System.out.println("Your lowest number is: " + min);
System.out.println();
System.out.println("Would you like to try again?\n Type \"yes\" or \"no\".");
answer = keyIn.nextLine();
} while (answer.equalsIgnoreCase("yes"));
}
}
__________________
[ADM]art |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
The input gathered in 1., below, will be overwritten by the input gathered in 2. Get rid of that second input. I didn't look at the code thoroughly, but that stands out.
2. next = keyIn.nextDouble(); nextNum = next; if (next > max) max = next; if (next < min) min = next; sum = sum + nextNum; amountOfNumbers++; 1. next = keyIn.nextDouble(); nextNum = next;
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Also, the definition for max:
Java Syntax (Toggle Plain Text)
|
|
|
|
|
|
#4 |
|
Programmer
Join Date: Sep 2005
Location: Anchorage, Alaska
Posts: 37
Rep Power: 0
![]() |
All right
it all makes sense now....Thank You!
__________________
[ADM]art |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| linked list problems | bl00dninja | C++ | 6 | Feb 17th, 2008 11:30 AM |
| dev c++ software, template problem | cairo | C++ | 11 | Jun 2nd, 2006 1:42 PM |
| Median/Mode in arrays? {Need help} | Java|Tera | Java | 27 | Nov 29th, 2005 11:50 AM |
| User-defined creatNode and deleteNode functions for a doubly-linked list | jgs | C | 2 | Apr 28th, 2005 9:53 AM |
| airport Log program using 3D linked List : problem reading from file | gemini_shooter | C++ | 0 | Mar 2nd, 2005 5:12 PM |