How to resolve the algorithm Keyboard input/Obtain a Y or N response step by step in the Clojure programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Keyboard input/Obtain a Y or N response step by step in the Clojure programming language

Table of Contents

Problem Statement

Obtain a valid   Y   or   N   response from the keyboard. The keyboard should be flushed, so that any outstanding key-presses are removed, preventing any existing   Y   or   N   key-press from being evaluated. The response should be obtained as soon as   Y   or   N   are pressed, and there should be no need to press an   enter   key.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Keyboard input/Obtain a Y or N response step by step in the Clojure programming language

Source code in the clojure programming language

(ns yprompt.core
  (:import jline.Terminal)
  (:gen-class))

(defn yes? [k]
  (if (or (= k 89) (= k 121)) true false))

(defn prompt []
    (println "\nPrompt again [Y/N]?")
    (let [term (Terminal/getTerminal)
          ykey (yes? (.readCharacter term System/in))]
      (if-not ykey
        (recur)
        (println "Yes!"))))

(defn -main [& args]
  (prompt))


  

You may also check:How to resolve the algorithm Singly-linked list/Traversal step by step in the Ela programming language
You may also check:How to resolve the algorithm Greatest element of a list step by step in the Ela programming language
You may also check:How to resolve the algorithm Combinations with repetitions step by step in the Clojure programming language
You may also check:How to resolve the algorithm Fortunate numbers step by step in the Raku programming language
You may also check:How to resolve the algorithm CSV to HTML translation step by step in the Prolog programming language