0

I was just brushing through the free Java course offered on University of Helsinki and ran into a problem that I could not figure out how to solve. It takes three points (in this case temperatures) and plots it into a graph then asks you to omit any entries below -30 and above 40 degrees. Plotting was simple enough as there was an accompanying program written to actually execute the points but I can't figure out how to set the program to work within the said parameters.

This is what I have:

import java.util.Scanner;
public class Temperatures {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);

        while (true) {
            System.out.println("Input numbers:");
            double number1 = Double.parseDouble(reader.nextLine());
            double number2 = Double.parseDouble(reader.nextLine());
            double number3 = Double.parseDouble(reader.nextLine());
            Graph.addNumber(number1);
            Graph.addNumber(number2);            
            Graph.addNumber(number3); 

            if ((number1 > 40 || < -31) && (number2 > 40 || < -31) && (number3 > 40 || < -31)) {
                System.out.println("Invalid parameters");
            } else {
                System.out.println("");
            }    
            break;           
        }
    }
}

Am I approaching this problem completely wrong?

vqdave
  • 2,361
  • 1
  • 18
  • 36
dirtgrub
  • 5
  • 6

2 Answers2

2

You are approaching on the right way, but you can implement this code more efficiently and you have to fix the syntax errors.

Advice for the efficiency:

  • You can use a data structure like array keeping the temperature values.
  • You can check the values using a loop. So, you can have more readable and modular code.

Syntax Error(s):

(number1 > 40 || < -31)

You can not use a structure like this in java. You have to fix it as below:

(number1 > 40 || number1 < -31)
Bilgehan
  • 117
  • 4
1

You can use an if statement (such as you did).

In your code you have some syntax errors however.

You can not say

 if(number1 > 40 || < -31)

it has to be

if(number1 > 40 || number1 < -31)
King Twix
  • 52
  • 8