infinite loop when asking for number - java

Hi all thanks for taking the time, I am continuosly getting an infinate loop when i am inserting the variable m could anyone please take a look thanks.
public static void main program7_2(String args [])
{
Scanner sc = new Scanner (System.in);
System.out.println("Please enter the first number: ");
int n = sc.nextInt();
while((n%2)== 0)
{
System.out.println("The number you entered is incorrect please enter an odd number:");
n = sc.nextInt();
}
System.out.println("Please enter the second number: ");
int m = sc.nextInt();
while((m%2)== 0)
{
System.out.println("The number you entered is incorrect please enter an odd number:");
m = sc.nextInt();
}
int sum =0;
for (int i = n; n<=m; i++)
{
if ((i%2) != 0)
sum = sum + i;
}
System.out.println("Sum of the numbers between "+n+ " and " +m+": " + sum);
}
The problem of the program is to enter 2 odd numbers and get the sum of the odd numbers in between
Thanks and regards

Instead of n<=m in your for loop use i<=m since you are using i as your counter and not n

This is the error :
for (int i = n; n<=m; i++) /* terminating condition "n<=m" is never met here */
{
if ((i%2) != 0)
sum = sum + i;
}
Why : this loop increments i but terminating condition is n<=m which is never met....so either do i<n Or i<m which ever suits you for terminating the condition!!

if u want to get the sum of nubers between those two use
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the first number: ");
int n = sc.nextInt();
while ((n % 2) == 0) {
System.out.println("The number you entered is incorrect please enter an odd number:");
n = sc.nextInt();
}
System.out.println("Please enter the second number: ");
int m = sc.nextInt();
while ((m % 2) == 0) {
System.out.println("The number you entered is incorrect please enter an odd number:");
m = sc.nextInt();
}
int sum= 0;
for (int i = n; i <= m; i++) {
sum = sum + i;
}
System.out.println("Sum of the numbers between " + n + " and " + m + ": " + sum);
}
}

Related

Factorials with count-controlled or sentinel loops in java

This is what I am supposed to do:
Write a Java program that will compute the factorial of some numbers n (input from the user, accept only range 1 - 10). For each valid number in input, the output should be the value of n!. Your program should use a loop, to allow the user to input more than one number (count-controlled or sentinel-controlled, your choice)
This is what I have at the moment:
public static void main(String[] args)
{
Scanner console = new Scanner(System.in);
int num, result = 1;
String str;
System.out.print("Do you want to start? (y/n) ");
str = console.next();
System.out.println();
while(str.charAt(0) == 'y')
{
System.out.print("Enter an integer (1 - 10): ");
num = console.nextInt();
if(num < 1 || num > 10)
{
System.out.print("NUMBER OUT OF RANGE!");
num = console.nextInt();
}
else
{
int i = 2;
while(i <= num)
{
result = result * i;
i++;
}
System.out.println(num + "! = " + result);
System.out.println();
System.out.print("Do you want to continue? ");
str = console.next();
}
System.out.println();
}
}
But this is my result:
Do you want to start? (y/n) y
Enter an integer (1 - 10): 1
1! = 1
Do you want to continue? y
Enter an integer (1 - 10): 2
2! = 2
Do you want to continue? y
Enter an integer (1 - 10): 3
3! = 12
Do you want to continue? y
Enter an integer (1 - 10): 4
4! = 288
I can't get the output to display the right results, that is the only problem I am having with my program. Also, it shows the correct result the first time you are asked to enter an integer, but after that, everything goes wrong
Try this code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
String str;
System.out.print("Do you want to start? (y/n) ");
str = console.next();
System.out.println();
int num, result;
while (str.charAt(0) == 'y') {
System.out.print("Enter an integer (1 - 10): ");
num = console.nextInt();
while (num < 1 || num > 10) {
System.out.println("NUMBER OUT OF RANGE!");
System.out.print("Enter an integer (1 - 10): ");
num = console.nextInt();
}
int i = 2;
result = 1;
while (i <= num) {
result = result * i;
i++;
}
System.out.println(num + "! = " + result);
System.out.println();
System.out.print("Do you want to continue? ");
str = console.next();
System.out.println();
}
}
}
You need to set result = 1 inside your outer while loop, not before it. What's happening here is that the numbers you're multiplying are getting multiplied by the result from the previous iteration of the outer while loop.
You are not resetting the result when you iterate for a new value.
Just reset the result to 1 before you start calculating factorial
e.g. -
while(str.charAt(0) == 'y')
{
result =1;
I think you're having a logical error with your if statement. Having a range of 1-10, your if statement should look something like this:
if(userInput>0 && userInput<=10);
Also, as what David Wallace has stated, you need to re-initialize your variable i back to 1 after your inner "factorial while loop" finishes. In this way, your variable i would always have the starting value of 1 whenever it enters your inner factorial while loop.
I would begin by creating a lookup table for the valid range of factorials. That might be done iteratively, create a fact array and fill it. Something like,
int[] fact = new int[10];
int i;
fact[0] = i = 1;
while (i < fact.length) {
fact[i] = (i + 1) * fact[i - 1];
i++;
}
Then you can use an infinite loop with your Scanner (breaking on non-integer input) and the fact lookup table like,
Scanner scan = new Scanner(System.in);
while (true) {
System.out.println("Enter a number 1-10 for the factorial (or a "
+ "non-number to quit): ");
if (!scan.hasNextInt()) {
break;
}
i = scan.nextInt();
if (i < 1 || i > 10) {
System.out.println("NUMBER OUT OF RANGE!");
continue;
}
System.out.printf("%d! = %d%n", i, fact[i - 1]);
}

How to correct my sentinel value?

My code is supposed to ask for a name, ask for a number between one and ten, print the numbers from 1 to the number the user entered except every third number should
be the user's name that was entered at the beginning of the program, print the even numbers, continually ask the user for numbers until the user enters the sentinel, and then print the total of the numbers entered. (I know, that's a lot.) My code is running fine, the only problem I am having is with the last part. Even when the user enters the sentinel, which in this case is -1, the program still asks for another entry.
Did I do something wrong when declaring the variable or can someone explain how to fix my problem? Here is my code.
import java.util.Scanner;
/**
*
* #author Home
*/
public class NewClass1 {
public static void main(String[] args) {
int number;
Scanner scan = new Scanner( System.in);
System.out.print( "Enter your name: ");
String name = scan.nextLine();
System.out.print( "Please enter a number between 1 and 10: ");
number = scan.nextInt();
//asks for a number between one and ten until I get number within that range,
while (number < 1 || number > 10) {
System.out.print( "No, between 1 and 10: ");
number = scan.nextInt();
}
for (int i = 1; i <= number; i++) {
if (i % 3 == 0) {
System.out.print(name + " ");
} else {
System.out.print(i + " ");
}
}
System.out.println();
for(int i =2; i<=number; i+=2)
System.out.print(i + " ");
System.out.print("are the even numbers.");
final int SENTINEL = -1;
int inputNumber;
int total = 0;
System.out.println(" Enter a number or -1 to finish. " );
inputNumber = scan.nextInt();
while ( inputNumber != SENTINEL )
{
total += number;
System.out.print("Enter the next number or '-1' to finish. ");
number = scan.nextInt();
}
System.out.println( "The total is " + total);
}
}
Solution:
You get input from user and saving that input in varible called number but you are checking your while against inputNumber.
while ( inputNumber != SENTINEL )
{
total += number;
System.out.print("Enter the next number or '-1' to finish. ");
inputNumber = scan.nextInt(); <<< not number should be inputNumber
}
public class NewClass1 {
public static void main(String[] args) {
int number;
Scanner scan = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scan.nextLine();
System.out.print("Please enter a number between 1 and 10: ");
number = scan.nextInt();
//asks for a number between one and ten until I get number within that range,
while (number < 1 || number > 10) {
System.out.print("No, between 1 and 10: ");
number = scan.nextInt();
}
for (int i = 1; i <= number; i++) {
if (i % 3 == 0) {
System.out.print(name + " ");
} else {
System.out.print(i + " ");
}
}
System.out.println();
for (int i = 2; i <= number; i += 2) {
System.out.print(i + " ");
}
System.out.print("are the even numbers.");
final int SENTINEL = -1;
int inputNumber;
int total = 0;
do {
System.out.println(" Enter a number or -1 to finish. ");
inputNumber = scan.nextInt();
if(inputNumber!= SENTINEL){
total+=inputNumber;
}
} while (inputNumber != SENTINEL);
System.out.println("The total is " + total);
}
}

Java addition program which keeps addup the numbers you input ing until you enter a zero. Average of SumPositive and SumNegative

In this java program you (the user) keeps entering numbers until you enter a zero, which is when the list terminates. It will compute the sum of positive even & sum of odd even and negative numbers. It will also compute the average.
I'm stuck at the part to get the average of sum of even positive numbers, sum of odd positive numbers, and sum of negative numbers.
import java.util.*;
class sumPositiveAverage {
public static void main (String[] args) {
Scanner sc = new Scanner (System.in);
System.out.println ("Enter numbers. List terminates when you enter a zero.
Enter a zero when you want to begin the addition.");
int a = sc.nextInt();
int esum=0;
int osum=0;
int nsum=0;
while (a !=0)
{
if (a>0)
{
if (a%2==0)
{
esum = esum+a;
}// end of 3rd innermost if statement
else
{
osum = osum+a;
}// end of 3rd else statement
}//end of 2nd middle if-else-loop
else if (a<0)
{
nsum=nsum+a;
}//end of 2nd middle else statement
}//end of while loop
System.out.println ("The sum of even positive numbers is "+esum);
System.out.println ("The sum of odd positive numbers is "+osum);
System.out.println ("The sum of negative numbers is "+nsum);
}//end of main
}//end of class2
import java.util.*;
public class sumPositiveAverage {
public static void main(String[] args)
{
int sum_even = 0;
int sum_odd = 0;
int sum_negative = 0;
int sum_positive = 0;
int count_even = 0;
int count_odd = 0;
int count_negative = 0;
int count_positive = 0;
double avg_sum_even = 0;
double avg_sum_odd = 0;
double avg_sum_negative = 0;
double avg_sum_positive = 0;
int sum = 0;
int count = 0;
Scanner input = new Scanner(System.in);
int data = 0;
do
{
System.out.print("Type in a positive or negative number and press enter key, if 0 is entered program stops: ");
data = input.nextInt();
if(data > 0){
if(data%2 == 0){
//even number
sum_even = sum_even + data;
count_even++;
}else{
//odd
sum_odd = sum_odd + data;
count_odd++;
}
sum_positive = sum_positive + data;
count_positive++;
}else if(data < 0) {
//negative number
sum_negative = sum_negative + data;
count_negative++;
}
}
//Stops if the value of data is ZERO(0) and continues if it's not
while(data != 0);
//here means zero has been entered
if(count_positive > 0) { avg_sum_positive = (double)sum_positive/count_positive; }
if(count_negative > 0) { avg_sum_negative = (double)sum_negative/count_negative; }
if(count_even > 0) { avg_sum_even = (double)sum_even/count_even; }
if(count_odd > 0) { avg_sum_odd = (double)sum_odd/count_odd; }
System.out.println("Sum of Positive Numbers = " + sum_positive);
System.out.println("Sum of negative Numbers = " + sum_negative);
System.out.println("Sum of odd Numbers = " + sum_odd);
System.out.println("Sum of even Numbers = " + sum_even);
System.out.println("Count of Positive Numbers = " + count_positive);
System.out.println("Count of negative Numbers = " + count_negative);
System.out.println("Count of odd Numbers = " + count_odd);
System.out.println("Count of even Numbers = " + count_even);
System.out.println("Average of Positive Numbers = " + avg_sum_positive);
System.out.println("Average of negative Numbers = " + avg_sum_negative);
System.out.println("Average of odd Numbers = " + avg_sum_odd);
System.out.println("Average of even Numbers = " + avg_sum_even);
}//closing main
} // closing class

Positive and Negative numbers in String

I have to make a program that allows the user to enter integer numbers until he press '0'. The program has to print:
1)The total number of the entered numbers
2) The number of the positive ones
3) The average of the positive ones
4) The number of the negative ones
5) The sum of the negatives
So far, all I could do is make the "enter untill '0' is pressed" and find the number of the entered numbers, which is a lot for me and my programing skills. I am having troubles trying to find out if the number is positive or negative. Probably I am not comparing them right, so I would love if I get a little help from someone advanced.
Here's my code so far:
import java.io.*;
class Nums {
public static void main(String args[])
throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String str;
int EnteredNumbers = -1;
int Positive = 0;
int Negative = 0;
int NegativeSum = 0;
double AveragePositive = 0;
System.out.println("Enter '0' to quit.");
System.out.println("Enter Numbers: ");
do {
EnteredNumbers++;
str = br.readLine();
} while(!str.equals("0"));
System.out.println("You have have entered "+EnteredNumbers+ " numbers!");
System.out.println("You have have entered "+Positive+ " Positive numbers!");
System.out.println("The Average of the Positive Numebers is "+AveragePositive+ "!");
System.out.println("You have have entered "+Negative+ " Negative numbers!");
System.out.println("The Sum of the Negative numbers is "+NegativeSum+ "!");
}
}
Firstly as pointed out Integer.parseInt is used in java to convert from String to int.
Secondly you need to have a extra variable to store and accumulate the total of positive numbers too. I have added a try-catch exception block to handle errors.
Here is a code for reference.
import java.io.*;
public class Nums {
public static void main(String args[])
throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String str;
int EnteredNumbers = -1;
int Positive = 0;
int Negative = 0;
int NegativeSum = 0;
int PositiveSum = 0; // Added extra variable
double AveragePositive = 0;
System.out.println("Enter '0' to quit.");
System.out.println("Enter Numbers: ");
try{
do {
EnteredNumbers++;
str = br.readLine();
int num = Integer.parseInt(str);
if (num>0)
{
Positive++;
PositiveSum+=num;
}
else if (num<0)
{
Negative++;
NegativeSum+=num;
}
} while(!str.equals("0"));
AveragePositive = (double)PositiveSum/(double)Positive;
System.out.println("You have have entered "+EnteredNumbers+ " numbers!");
System.out.println("You have have entered "+Positive+ " Positive numbers!");
System.out.println("The Average of the Positive Numebers is "+AveragePositive+ "!");
System.out.println("You have have entered "+Negative+ " Negative numbers!");
System.out.println("The Sum of the Negative numbers is "+NegativeSum+ "!");
}
catch (java.lang.NumberFormatException e)
{
System.out.println("Wrong format");
}
}
}
Output if there are no errors
Enter '0' to quit.
Enter Numbers:
1
5
5
-5
-5
0
You have have entered 5 numbers!
You have have entered 3 Positive numbers!
The Average of the Positive Numebers is 3.6666666666666665!
You have have entered 2 Negative numbers!
The Sum of the Negative numbers is -10!
check by parsing String to int
Integer.parseInt(str)>0 //positive number
Integer.parseInt(str)<0 //negative
do {
EnteredNumbers++;
str = br.readLine();
int number= Integer.parseInt(str);
if(number>0){ //do positive stuffs}
else if(number<0){//do negative stuffs}
} while(!str.equals("0"));
Hope the below code achieves what you want to do
import java.io.*;
class Nums {
public static void main(String args[])
throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String str;
int EnteredNumbers = -1;
int Positive = 0;
int Negative = 0;
int NegativeSum = 0;
double AveragePositive = 0;
double PositiveSum = 0;
System.out.println("Enter '0' to quit.");
System.out.println("Enter Numbers: ");
do {
EnteredNumbers++;
str = br.readLine();
if(Integer.parseInt(str) > 0){
Positive++;
PositiveSum = PositiveSum + Integer.parseInt(str);
AveragePositive = PositiveSum/Positive;
}
if(Integer.parseInt(str) < 0){
Negative++;
NegativeSum = NegativeSum + Integer.parseInt(str);
}
} while(!str.equals("0"));
System.out.println("You have have entered "+EnteredNumbers+ " numbers!");
System.out.println("You have have entered "+Positive+ " Positive numbers!");
System.out.println("The Average of the Positive Numebers is "+AveragePositive+ "!");
System.out.println("You have have entered "+Negative+ " Negative numbers!");
System.out.println("The Sum of the Negative numbers is "+NegativeSum+ "!");
}
}
Create a list and keep adding the numbers into List. You should do something like:
List<Integer> numberList = new ArrayList<Integer>();
do {
str = br.readLine();
int number = Integer.parseInt(str);
} while(number != 0);
And then use for loop to do count like:
int negativeNumberSum = 0;
int positiveNumberSum = 0;
int positiveNumberCounts = 0;
for (int i =0; i<numberList.size(); i++) {
if (numberList.get(i) >= 0) {
positiveNumberSum += numberList.get(i);
positiveNumberCounts ++;
} else {
negativeNumberSum += numberList.get(i);
}
}
//print average of positive as positiveNumberSum /(double) positiveNumberCounts if positiveNumberCounts != 0
//print average of positive as negativeNumberSum / (double)(numberList.size() - positiveNumberCounts) same if (numberList.size() - positiveNumberCounts) != 0
//print positive count as positiveNumberSum and same for negative.
//
Try using Integer.parseInt() to get an int from your string str, and checking to see if the integer is < 0 (negative) or > 0 (positive)
I have added an extra variable called PositiveSum to assist with the AveragePositive calculation.
In order to perform this calculation, you need to ensure the value is an Integer so I have added the parse statement.
class Nums {
public static void main(String args[])
throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String str;
int EnteredNumbers = -1;
int Positive = 0;
int Negative = 0;
int NegativeSum = 0;
int PositiveSum = 0;
double AveragePositive = 0;
System.out.println("Enter '0' to quit.");
System.out.println("Enter Numbers: ");
do {
EnteredNumbers++;
str = br.readLine();
int number = Integer.parseInt(str);
if(number<0) {
Negative++;
NegativeSum += number;
} else if(number>0) {
Positive++;
PositiveSum += number;
}
} while(!str.equals("0"));
AveragePositive = PositiveSum / EnteredNumbers;
System.out.println("You have have entered "+EnteredNumbers+ " numbers!");
System.out.println("You have have entered "+Positive+ " Positive numbers!");
System.out.println("The Average of the Positive Numebers is "+AveragePositive+ "!");
System.out.println("You have have entered "+Negative+ " Negative numbers!");
System.out.println("The Sum of the Negative numbers is "+NegativeSum+ "!");
}
}
Change your do-while loop: it is using Strings, not integers:
do {
EnteredNumbers++;
num = sc.nextInt();
} while(num != 0);
Also, you need to import the scanner utility:
import java.util.Scanner;
Don't use a BufferedReader, but use:
static Scanner sc = new Scanner(System.in);
Also, you would want to use a checker for your positive and negative numbers inside your do-while loop:
if(Integer.parseInt(num)<0) { Negative++; }
else { Positive++; }
I've also added a try-catch statement to handle an error if someone doesn't enter an int:
try {
num = sc.nextInt();
}
catch(Exception e) {System.out.println("Invalid integer"); System.exit(0); }
I added a little bit of code that takes the 0 off when it prints the numbers, as I realise it will include 0 when you type it in to exit.
Change your class statement to public, it's just programming:
public class Nums {
Also, I add a negative-number counter in the main code, and an averager.
Here is the complete code, to avoid confusion:
import java.io.*;
import java.util.Scanner;
public class prime {
static Scanner sc = new Scanner(System.in);
public static void main(String args[])
{
int EnteredNumbers = 0;
int Positive = 0;
int Negative = 0;
int NegativeSum = 0;
double AveragePositive = 0;
int num = 0;
int PosTotal = 0;
System.out.println("Enter '0' to quit.");
System.out.println("Enter Numbers: ");
do {
try {
num = sc.nextInt();
}
catch(Exception e) {System.out.println("Invalid integer!"); System.exit(0); }
if(num<0) { Negative++; NegativeSum += num; }
if(num>0){ Positive++; PosTotal += num; }
EnteredNumbers++;
} while(num != 0);
AveragePositive = PosTotal / Positive;
EnteredNumbers -= 1;
System.out.println("You have have entered "+EnteredNumbers+ " numbers!");
System.out.println("You have have entered "+Positive+ " Positive numbers!");
System.out.println("The Average of the Positive Numbers is "+AveragePositive+ "!");
System.out.println("You have have entered "+Negative+ " Negative numbers!");
System.out.println("The Sum of the Negative numbers is "+NegativeSum+ "!");
}
}
Hope it helps!
Note I have trial-tested it, and it works brilliantly. If you have any errors with this please tell me :)

Taking scanner input and outputting min and max

so I'm trying to find the min, max, and average number of the numbers input. Everything works fine, although I do not want input less than 0 or greater than 100. When I input a number less than 0 or greater than 100 it still records it as the min/max. I do not want this! How would I not take input that is less than 0 or greater than 100?
Thanks!
import java.text.DecimalFormat;
import java.util.Scanner;
public class ExamGrades {
public static void main(String[]args){
Scanner scan = new Scanner(System.in );
int number = 0;
double total = 0;
int minimum;
int maximum = 0;
System.out.println("Please enter the first integer: ");
number = scan.nextInt();
minimum=number;
total += number;
for(int i = 2; i<11; i++){
if(number<0 || number >100){
System.out.println("Please enter a valid number: ");
number = scan.nextInt();
i--;
}
else{
System.out.println("Please enter integer " + i + ":");
number = scan.nextInt();
total += number;
if(number<minimum)
minimum = number;
if(number>maximum)
maximum = number;
}
}
DecimalFormat oneDecimalPlace = new DecimalFormat("##.#");
System.out.println("The minimum is: " + minimum);
System.out.println("The maximum is: " + maximum);
System.out.println("The average is: " + oneDecimalPlace.format((total) / 10.0 ));
}
}
Let's say you entered 5 for integer one, then 102 for the second integer. What's going to happen? Well before you entered 102, number was 5, so it will go into the else block.
It'll say:
Please enter integer 2:
then you type: 102
So then what? Well the next piece of code is:
number = scan.nextInt(); and then it goes through the if-statements right below it to determine if it's a maximum. Nothing is stopping it.
Try this code instead of your loop:
System.out.println("Please enter the first integer: ");
number = scan.nextInt();
// keep them in a loop until they enter a valid number
while (number < 0 || number > 100) {
System.out.println("Please enter a valid number: ");
number = scan.nextInt();
}
// after they entered a valid number, add it to the series
minimum = number;
total += number;
for(int i = 2; i < 11; i++) {
System.out.println("Please enter integer " + i + ":");
number = scan.nextInt();
// keep them in a loop until they enter a valid number
while (number < 0 || number > 100) {
System.out.println("Please enter a valid number: ");
number = scan.nextInt();
}
// after they entered a valid number, add it to the series
total += number;
if(number<minimum)
minimum = number;
if(number>maximum)
maximum = number;
}
problem:
if(number<0 || number >100)
It will return false when number is 100 or 0 thus accepting it and executing you else block.
Is 100>100 answer false because they are equal, not greater than the other
solution:
if(number<1 || number >99)
EDit:
public static void main(String[]args){
Scanner scan = new Scanner(System.in );
int number = 0;
double total = 0;
int minimum = 0;
int maximum = 0;
System.out.println("Please enter the first integer: ");
number = scan.nextInt();
if(number>1 && number <99)
{
minimum=number;
total += number;
}
for(int i = 2; i<11; i++){
if(number<1 || number >99){
System.out.println("Please enter a valid number: ");
number = scan.nextInt();
i--;
}
else{
System.out.println("Please enter integer " + i + ":");
number = scan.nextInt();
total += number;
if(number>1 && number <99)
{
if(number<minimum)
minimum = number;
if(number>maximum)
maximum = number;
}
}
}
DecimalFormat oneDecimalPlace = new DecimalFormat("##.#");
System.out.println("The minimum is: " + minimum);
System.out.println("The maximum is: " + maximum);
System.out.println("The average is: " + oneDecimalPlace.format((total) / 10.0 ));
}
Here is an Example.Exceptions are not handled so put a Try-catch to String to Integer Conversion If you want.
import java.util.Scanner;
import java.text.DecimalFormat;
public class Numbers{
public static void main(String x[]){
Scanner scn=new Scanner(System.in);
int Total=0,Max=0,Min=Integer.MAX_VALUE;
for(int i=0;i<10;){
System.out.print("Enter Number :");
int NumberOne=Integer.parseInt(scn.nextLine());//Put a Try catch If needed
if(NumberOne>0&&NumberOne<100){
Total+=NumberOne;
Max=(Max>NumberOne)?Max:NumberOne;
Min=(Min<NumberOne)?Min:NumberOne;
i++;
}else{
System.out.print("Number Invalid");
}
}
DecimalFormat oneDecimalPlace = new DecimalFormat("##.#");
System.out.println("The minimum is: " + Min);
System.out.println("The maximum is: " + Max);
System.out.println("The average is: " + oneDecimalPlace.format((Total) / 10.0 ));
}
}

Categories

Resources