w3resource

Java: Accepts two double variables and test the specified condition


Test Doubles Between 0 and 1

Write a Java program that accepts two double variables and test if both strictly between 0 and 1 and false otherwise.

Sample Solution:

Java Code:

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        // Creating a Scanner object to take input from the user
        Scanner in = new Scanner(System.in);
        
        // Prompting the user to input the first number
        System.out.print("Input first number: ");
        double n1 = in.nextDouble();
        
        // Prompting the user to input the second number
        System.out.print("Input second number: ");
        double n2 = in.nextDouble();
        
        // Checking if both numbers are greater than 0 and less than 1
        System.out.println(n1 > 0 && n1 < 1 && n2 > 0 && n2 < 1);
    }
} 

Sample Output:

Input first number:  1.25
Input second number:  2.15
false
Input first number:  .55
Input second number:  .75
true

Flowchart:

Flowchart: Java exercises: Accepts two double variables and test the specified condition.



For more Practice: Solve these Related Problems:

  • Write a Java program to test if three given double values are strictly between 0 and 1.
  • Write a Java program to check if two double values lie within the range (0,1) using an epsilon comparison for precision.
  • Write a Java program to validate if one double is between 0 and 1 and another lies between 1 and 2.
  • Write a Java program to determine if two double values are in the interval (0,1) and are also non-integer values.

Go to:


PREV : Check Four Numbers Equal.
NEXT : Print Boolean Matrix.


Java Code Editor:

Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.