How to Determine the Max and Min Values Read in From a Text File in Java

前端 未结 3 967
不思量自难忘°
不思量自难忘° 2021-01-25 08:33

I am doing a homework assignment for a class, and am looking for some helpful pointers, not full solutions. Basically, I have to write a Java program that reads in a text file a

3条回答
  •  天涯浪人
    2021-01-25 08:35

    Highest Seen So Far: -Infinity
        (or any really low number so that any number you see next will be higher)
    Lowest Seen So Far: Infinity
        (or any really high number so that any number you see next will be lower)
    
    Walk through each data point "d":
        is d higher than your latest "highest seen so far"?
            -> if yes, your new highest seen so far is now d
        is d lower than your latest "lowest seen so far"?
            -> if yes, your new lowest seen so far is now d
    
    Your highest seen so far is now the highest data point
    Your lowest seen so far is now the lowest data point
    

    In pseudocode:

    highest = -inf
    lowest = inf
    
    for d in dataset:
      if d > highest:
        highest = d
      if d < lowest:
        lowest = d
    
    print "highest: " + highest
    print "lowest: " + lowest
    

    here is an example

    let's say your dataset is 5 2 8 4

    Step 0
        Highest: -inf
        Lowest: inf
    
    Step 1
      See d = 5...that's higher than highest -inf, so new highest is 5.
      See d = 5...that's lower than lowest -inf, so new lowest is 5
        Highest: 5
        Lowest: 5
    
    Step 2:
      See d = 2...that's not higher than highest 5...highest is still 5
      See d = 2...that is lower than lowest 5...new lowest is 2
        Highest: 5
        Lowest: 2
    
    Step 3:
      See d = 8...that's higher than highest 5...new highest is 8
      See d = 8...that's not lower than lowest 2...lowest is still 2
        Highest: 8
        Lowest: 2
    
    Step 4:
      See d = 4...that's not higher than highest 8...highest is still 8
      See d = 4...that's not lower than lowest 2...lowest is still 2
        Highest: 8
        Lowest: 2
    
    Result:
        Highest: 8
        Lowest: 2
    

提交回复
热议问题