Object-Oriented Programming with Java, part I – Exercise 23

This program takes user input in the range of -30 to 40 and uses it to output a graph.

My Code

import java.util.Scanner;

public class Temperatures {

    public static void main(String[] args) {

        Scanner reader = new Scanner(System.in);
        // Write your code here.
        while (true) {
            System.out.println("Enter a number: ");
            double number = Double.parseDouble(reader.nextLine());
            if (number > -30) {
                if (number < 40) {
                    Graph.addNumber(number);
                }
            }

        }
        // Remove or comment out these lines above before trying to run the tests.
    }
}

Model Code

import java.util.Scanner;

 

public class Temperatures {

 

    public static void main(String[] args) {

 

        Scanner reader = new Scanner(System.in);

        // Write your code here. 

 

        // Remove or comment out these lines above before trying to run the tests.

 

        while (true) {

            System.out.println("Give a temperature!");

            double temperature = Double.parseDouble(reader.nextLine());

 

            if (temperature >= -30 && temperature <= 40) {

                Graph.addNumber(temperature);

            }

        }

    }

}

Comments

Mine does the same thing but it isn’t as pretty. For instance I use a double if statement and the model code uses && to tie it all together.

Leave a comment