Miguel
Miguel

Reputation: 131

how to get the MIN value of a user input to print

I have been having some trouble completing an exercise i was given in university. Basically they ask me to get a number of user inputs and then calculate the sum, average, smallest and largest inputs and also the range.

Everything works fine until i try to get the minimum value. I have looked thoroughly and tried it myself but i can't get it to work for some reason. Here is my code:

   import java.util.Scanner;

   public class IntSequence {
    public static void main ( String arg[]){

    Scanner input = new Scanner(System.in);
    int sum = 0;
    double avg = 0;
    int choice = 0;
    int i = 1;
    int smallestInput = Integer.MAX_VALUE;
    int largestInput = Integer.MIN_VALUE;
    int range = 0;


    System.out.println("Please enter an integer: ");
    for(; i <= 1000; i++){
        choice = input.nextInt();
        if (choice <= 0)
            break;
        sum = sum + choice;
        avg = sum / i;
        if(choice > largestInput){
            largestInput = choice;
        }
        if(smallestInput < choice){
            smallestInput = choice;
        }



    }
    System.out.println("The sum of all integers is " + sum);
    System.out.println("The average of the input integers is " + avg);
    System.out.println("The largest input is: " + largestInput);
    System.out.println("The smallest input is: " + smallestInput);




  }
}

Upvotes: 0

Views: 3220

Answers (3)

Sachintha Dilshan
Sachintha Dilshan

Reputation: 9

import java.util.Scanner;

class Example{
 public static void main(String args[]){
    Scanner scanner = new Scanner(System.in);
    int total= 0;
    int max = 0;
    int min = 100;
    for (int i=0;i<10;i++) {
        System.out.println("Enter mark "+(i+1));
        int mark = scanner.nextInt();
        total+=mark;
        if(min>mark){
            min=mark;
        }
        if(max<mark){
            max =mark;
        }
    }
    System.out.println("Total : "+total);
    System.out.println("Max : "+max);
    System.out.println("Min : "+min);
    System.out.println("Average : "+(total/10));
   }
   }

Upvotes: 0

Dimuthu Dilshan
Dimuthu Dilshan

Reputation: 1

import java.util.*;
class Cmjd{
    public static void main(String[] args) {  
        Scanner input=new Scanner(System.in);
        int tot=0;
        double avg=0.0;
        int min=100,max=0;
        
        for (int i = 0; i<10; i++){
            System.out.print("Input Marks "+(i+1)+" : ");
            int num = input.nextInt();
            
            tot+=num;
            
            if(min>num){
                min=num;
            }if(max<num){
                max=num;
            }
            avg=tot/10.0;
            
        }
        System.out.print("Total :"+tot);
        System.out.print("Max :"+max);
        System.out.print("Min :"+min);
        System.out.print("Average :"+avg);
        
    }  
}

Upvotes: 0

Bc96
Bc96

Reputation: 13

Try change choice to be less then smallestInput like so :

if(choice < smallestInput){
        smallestInput = choice;
    }

not so :

if(smallestInput < choice){
        smallestInput = choice;
    }

Please note 0 is still counted as an input by a user :

if (choice < 0) break;

Upvotes: 1

Related Questions