HarrJ

B5 Day 19 number compare

Aug 28th, 2022
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.08 KB | None | 0 0
  1. package mrb5week4;
  2. import java.util.Scanner;
  3.  
  4. public class Day19B {
  5.     public static void main(String[] args) {
  6.         Scanner sc = new Scanner(System.in);
  7.         double num1 = 0;
  8.         double num2 = 0;
  9.         double numTotal = 0;
  10.         String op = "+";
  11.         String userInput = "";
  12.        
  13.         System.out.print("num1: ");
  14.         userInput = sc.nextLine();
  15.         if (isNumeric(userInput)) {
  16.             num1 = Double.parseDouble(userInput);
  17.         }
  18.         System.out.print("operator: ");
  19.         op = sc.nextLine();
  20.         System.out.print("num2: ");
  21.         userInput = sc.nextLine();
  22.         if (isNumeric(userInput)) {
  23.             num2 = Double.parseDouble(userInput);
  24.         }
  25.         System.out.println(num1 + " " + op + " " + num2);
  26.     }
  27.    
  28.     public static boolean isNumeric(String strNum) {
  29.         if (strNum == null) {
  30.             return false;
  31.         }
  32.         try {
  33.             double d = Double.parseDouble(strNum);
  34.         } catch (NumberFormatException nfe) {
  35.             return false;
  36.         }
  37.         return true;
  38.     }
  39. }
  40.  
  41.  
Advertisement
Add Comment
Please, Sign In to add comment