简体   繁体   中英

Java- (While loop) how to keep last input loop out of printed variables?

i'm writting a sleep tracker and when my while loop break it takes the "break" input as a valid input for my array, i'll share the code below for more context:

double optHours;
    int nightCount = 0;
    double sleepHistory;
    double historySum = 0;
    int belowAVG = 0;

    Scanner sc = new Scanner(System.in);

    System.out.println("Please enter your optimal sleep time (in hours):");
    optHours = sc.nextDouble();

    System.out.println("Next, enter your recorded sleep history values (in hours) \n" +
                        "Enter a negative value when done:");
    
    sleepHistory = sc.nextDouble();
    double minSleep = Double.MIN_VALUE;

    while (sleepHistory != -1) {
        sleepHistory = sc.nextDouble();
        historySum += sleepHistory;
        nightCount++;
        
        if (sleepHistory < optHours) {
            belowAVG++;
        }

        if (sleepHistory < minSleep) {
            minSleep = sleepHistory;

        }
        
    }

    double avgSleepTime = historySum / nightCount;

    System.out.println("Your sleep report: \n" +
                        nightCount + " nights total. \n" +
                        belowAVG + " nights below your optimal sleep time \n" + 
                        "Lowest sleep time: " + minSleep + "\n" +
                        "Average sleep time: " + avgSleepTime);
}

}

now when i'm running my code it takes the last input used to break the loop (-1) and put it in the lowest sleep time variable (minSleep), also it enters this same input in my average calculations making it unreliable. i'll add output example for even more context:

Please enter your optimal sleep time (in hours):
5
Next, enter your recorded sleep history values (in hours)
Enter a negative value when done:
6
6
6
6
7
8
8.5
-1
Your sleep report:
7 nights total.
1 nights below your optimal sleep time
Lowest sleep time: -1.0
Average sleep time: 5.785714285714286

now, what i'm aiming for is to avoid negative numbers (break inputs) in my calculations and when assigning the minSleep variable a value

The problem is that you check if sleephistory is negative but immediately set it to the next double without checking if that next double is negative. I modified the while loop, try if it works better that way.

while (sleepHistory < 0) {
        if(sc.nextDouble() > 0){
            sleepHistory = sc.nextDouble();
        }
        historySum += sleepHistory;
        nightCount++;
        
        if (sleepHistory < optHours) {
            belowAVG++;
        }

        if (sleepHistory < minSleep) {
            minSleep = sleepHistory;

        }
        
    }

You can modify the code to take in input from the user and check it against your condition in the while loop. By checking that the value entered is greater than or equal to 0, the code will continue to execute until any negative value is entered. This will make your code cleaner and easier to follow. Also you want to set your minimum sleep value to the biggest double there is because when you set it to the smallest number possible it can't be beat. Which was resulting in the same Double.MIN_VALUE every execution of your program.

double optHours;
int nightCount = 0;
double sleepHistory;
double historySum = 0;
int belowAVG = 0;

Scanner sc = new Scanner(System.in);

System.out.println("Please enter your optimal sleep time (in hours):");
optHours = sc.nextDouble();

System.out.println("Next, enter your recorded sleep history values (in hours) \n" +
                    "Enter a negative value when done:");

double minSleep = Double.MAX_VALUE; //set to the biggest value

while ((sleepHistory = sc.nextDouble()) >= 0) //grab data and check it every iteration
{
    historySum += sleepHistory;
    nightCount++;
    
    if (sleepHistory < optHours) {
        belowAVG++;
    }

    if (sleepHistory < minSleep) {
        minSleep = sleepHistory;

    }
    
}

double avgSleepTime = historySum / nightCount;

System.out.println("Your sleep report: \n" +
                    nightCount + " nights total. \n" +
                    belowAVG + " nights below your optimal sleep time \n" + 
                    "Lowest sleep time: " + minSleep + "\n" +
                    "Average sleep time: " + avgSleepTime);

Output

Please enter your optimal sleep time (in hours):
8
Next, enter your recorded sleep history values (in hours) 
Enter a negative value when done:
8
7
8
3
0
5
8
-5
Your sleep report: 
7 nights total. 
4 nights below your optimal sleep time 
Lowest sleep time: 0.0
Average sleep time: 5.571428571428571

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM