When my program prints the sum values you enter, it doesnt include the first num
ID: 3890719 • Letter: W
Question
When my program prints the sum values you enter, it doesnt include the first number in the series ..... how do i tell my program to include it ???
/**
* Assignment3
*
* @author Me
* @version 3.0
*/
import java.util.Scanner;
public class Assignment3
{
public static void main (String[]args)
{
Scanner Mescan = new Scanner(System.in);
int num = 0;
double sum = 0;
double last = 0;
double avg = 0;
double smallest = 0;
double largest = 0;
System.out.println("Please type in double values and to enter 0 when you are finished");
last = mescan.nextDouble();
if(last == 0)
{
System.out.println("You didn't type in anything");
}
while(last != 0)
{
sum = sum + last;
num++;
avg = sum / num;
System.out.println("Please type in a number ");
last = Mescan.nextDouble();
if(num == 1)
{
largest = last;
smallest = last;
}
if((last > largest)&&(last != 0))
{
largest = last;
}
if((last < smallest)&&(last != 0))
{
smallest = last;
}
if (last == 0)
{
System.out.println("The sum of your input is:" + sum);
System.out.println("The total number of inputs you provided was:" + num);
System.out.println("The average of your input is:" + avg);
System.out.println("The highest number of your input is:" + largest);
System.out.println("The smallest number of your input is:" + smallest);
}
}
Mescan.close();
}
}
Prints:
Type in double values and enter 0 when you are finished
-1
Type in another number
-2
Type in another number
-3
Type in another number
0
The sum of your input is:-6.0
The total number of inputs you provided was:3
The average of your input is:-2.0
The highest number of your input is:-2.0 ( this should say The highest number of your input is:-1.0) ...... Initial input value is not included
The smallest number of your input is:-3.0
Explanation / Answer
Hi,
I have fixed the issue and highlighted the code changes below.
Assignment3.java
import java.util.Scanner;
public class Assignment3
{
public static void main (String[]args)
{
Scanner Mescan = new Scanner(System.in);
int num = 0;
double sum = 0;
double last = 0;
double avg = 0;
double smallest = 0;
double largest = 0;
System.out.println("Please type in double values and to enter 0 when you are finished");
last = Mescan.nextDouble();
if(last == 0)
{
System.out.println("You didn't type in anything");
}
while(last != 0)
{
sum = sum + last;
num++;
avg = sum / num;
if(num == 1)
{
largest = last;
smallest = last;
}
if((last > largest)&&(last != 0))
{
largest = last;
}
if((last < smallest)&&(last != 0))
{
smallest = last;
}
System.out.println("Please type in a number ");
last = Mescan.nextDouble();
if (last == 0)
{
System.out.println("The sum of your input is:" + sum);
System.out.println("The total number of inputs you provided was:" + num);
System.out.println("The average of your input is:" + avg);
System.out.println("The highest number of your input is:" + largest);
System.out.println("The smallest number of your input is:" + smallest);
}
}
Mescan.close();
}
}
Output:
Please type in double values and to enter 0 when you are finished
-1
Please type in a number
-2
Please type in a number
-3
Please type in a number
0
The sum of your input is:-6.0
The total number of inputs you provided was:3
The average of your input is:-2.0
The highest number of your input is:-1.0
The smallest number of your input is:-3.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.