-1

Very new to Java (please don't laugh)and trying to do a project that asks user for a number and will keep track of the highest and lowest number when the user enter a sentinel value. My error is the I cannot get max and min values to update when going through the while loop.

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Please enter a double number, or 'q' to quit");
    double currentNum = 0.0;
    double maxNum=0.0;
    double minNum=0.0;
    int count = 0;
    
    
    while(sc.hasNextDouble())
    {
    currentNum = sc.nextDouble();
    count++;
    System.out.println(count);
    if(count ==1)
    {
        minNum = currentNum;
        maxNum = currentNum;
        System.out.println("Please enter a double number, or 'q' to quit");
    }
    else if(count!=1){
    if (currentNum > maxNum)
    {
        currentNum = maxNum;
        System.out.println("You are in currentNum > maxNum");
        System.out.println(maxNum);
        System.out.println("Please enter a double number, or 'q' to quit");
        
        
        
        System.out.println(maxNum);
        
    }
    else if (currentNum < minNum)
    {
        currentNum = minNum;
        System.out.println("You are in currentNum < minum");
        System.out.println("Please enter a double number, or 'q' to quit");
        
        
        
    }
    
    }
    }
   System.out.println("Min num " + minNum);
   System.out.println("Max num " + maxNum);
    
}
}
    

    
    
        
        
        
etkoppy
  • 11
  • 1

2 Answers2

0

I think you got your logic mixed up.

When you get a number larger or smaller than maxNum you want to update maxNum to currentNum not the other way around.

Also the count == 1 if condition is redundant.

import java.util.Scanner;


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter a double number, or 'q' to quit");
        double currentNum = 0.0;
        double maxNum=(double)Integer.MIN_VALUE;
        double minNum=(double)Integer.MAX_VALUE;
        int count = 0;
        
        
        while(sc.hasNextDouble()) {
            currentNum = sc.nextDouble();
            count++;
            System.out.println(count);
            if (currentNum > maxNum){
                maxNum = currentNum;
                System.out.println("You are in currentNum > maxNum");
                System.out.println("Maximum : "+maxNum);
            }
            else if (currentNum < minNum){
                minNum = currentNum;
                System.out.println("You are in currentNum < minum");
                System.out.println("Minimum : "+minNum);
            }
            System.out.println("Please enter a double number, or 'q' to quit");
        }
       System.out.println("Min num " + minNum);
       System.out.println("Max num " + maxNum);
    }
}


moficodes
  • 781
  • 1
  • 6
  • 21
0

One thing that I noticed is that you're comparing double variables. Generally it's not a good idea comparing floating point values because they have precision problems. You should use long or int instead. If you really don't have any other choice and have to use double, you should checkout this question: How to compare two double values in Java?

As pointed out in comment, you're updating currentNum instead of maxNum/minNum. So your else-if block for updating min-max value should look like this:

    if (currentNum > maxNum) {
        System.out.printf("You are in currentNum(%d) > maxNum(%d)", currentNum, maxNum);
        //currentNum = maxNum; // <- Wrong, doesn't update maxNum; currentNum would be updated in next loop iteration anyway
        maxNum = currentNum; // <- What you should be doing
    }
    else if (currentNum < minNum) {
        System.out.printf("You are in currentNum(%d) < minNum(%d)", currentNum, minNum);
        //currentNum = minNum; // <- Wrong, doesn't update minNum
        minNum = currentNum;   // <- What you should be doing
    }
Rohan Kumar
  • 5,427
  • 8
  • 25
  • 40