答えを主張する


31

実行時に、ユーザーが(空の改行以外の)何かを入力するまで、つまり単にEnterまたはを押すまで、入力行の入力を求め続けますOK。出力または結果は必須でも禁止でもありません。

擬似コード1

myform = new form("GUI")
myform.mytxt = new editfield("")
myform.ok = new button("OK")
repeat
  waitfor(myform.ok,"click")
until myform.mytxt.content <> ""

擬似コード2

LET TEXT = ""
WHILE TEXT = "" DO
  TEXT = PROMPT("")
ENDWHILE

例1

プログラムが実行され、すぐに1つのテキストフィールドとOKボタンを含むフォームがポップアップ表示されます。
ユーザーがOKボタンをクリックします。
何も起こりません。
ユーザーが「hello world」をテキストフィールドに貼り付けて、OKボタンをクリックします。
プログラムは終了します。

例2

関数が呼び出され、すぐに空白行と点滅カーソルが表示されます。
ユーザーがを押しEnterます。
カーソルが1行下に移動します。
ユーザーがを押しEnterます。
カーソルが1行下に移動します。
ユーザーがPPCGEnter
機能リターンを押します。


スペースのみの行を空としてカウントすることはできますか?
12431234123412341234123

@ 12431234123412341234123いいえ
アダム

ユーザーがデータの入力を開始したら、行全体を待つ必要がありますか?
マッテオイタリア

OK、@ CodyGrayの削除された回答のコメントから、許可されていないようです。
マッテオイタリア

@MatteoItaliaはい、正しいです。入力の行
アダム

回答:


16

TI-BASIC、2バイト

:Prompt X

TI-BASICはこれを自動的に行います。入力が与えられると、終了します。

GIFは次のとおりです。

enter image description here

enterキープレス履歴でキーのカウントを確認します。で作成したTI-のSmartView CEおよびezgif.com


1
うーん、これは2バイトですか?
オタク私


そして、それは答えのためにEnterを必要としませんか?これをオンラインでテストする場所はありますか?
アダム

@Adámそれは答えにエンターを必要としません。私が知っている限りでは、TI ROMイメージをまだ持っていなくてもオンラインにアクセスできる場所はありません。ただし、帰宅したら.gifを投稿できます。
スコットミルナー

1
@Adám自分でROMを所有せずにTI-BASICをオンラインでテストすることは違法です。
エリックアウトゴルファー



15

JavaScript、37 22 17バイト

while(!prompt());

while(!prompt());

説明

whileキーワードが始まりwhileループを。ループの状態で、!prompt()入力を求め、それが与えられたかどうかを確認します。指定されていない場合、ループの本体が実行されます。この場合、空の場合、インタープリターはループ条件に戻ります。ユーザーが入力するまで、同じプロセスが何度も繰り返されます。


while(""==prompt(""));
アダム

@Adámうわー。ソリューションを小さくする方法を考えていたところ、回答をありがとうございます!
アルジュン

当たり前のように思えた。ただし、私のブラウザーコンソールは、末尾なしで実行を拒否し;ますが、最初の式はなしで正常に機能しました;。理由は何ですか?
アダム

いいえ、Chrome。だから、任意のJavaScriptエンジンは現在の式の漏れを扱うことができるならば;、あなたはそのバイトを保存することができます:-)
アダム

1
@Adámは、ソースコードからセミコロンを削除した後に生じるエラーの本当の原因を突き止めました。これが発生する理由は、whileループでは、ループ条件の後、ループ本体としてステートメント(またはステートメントのグループ)が必要だからです。セミコロンを追加すると、ループ本体が終わったと思われます。ただし、セミコロンを削除すると、次の文がループ本体として期待されます。しかし、この場合、ループ条件の後に新しいステートメントはありません。そのため、エラーがスローされます。私はそれが今明確であることを望みます。私は物事を説明するのがひどいです!! :p
アルジュン

9

Java、55バイト

void f()throws Exception{while(System.in.read()==10);}}

正しく覚えていれば(PPCGで活動してからしばらく経ちます)、私のプログラムは単なる機能になります。

これはシステム固有です。行末文字が単一の改行であるシステムでのみ機能します。行末文字ではなく、キャリッジリターンである場合は、交換してください1013。Windowsでは、Windowsの行末がであるため、これは機能しません\r\n

これは、から直接読み取ることができるという事実を利用していSystem.inます。


2
この回答のみを歓迎します。:D
コナーオブライエン

3
ようこそ@Justin!
デジタル外傷

1
アクティブになる予定はありません。あちこちで質問のために立ち寄る
ジャスティン

「Windowsで動作しない」問題を回避するために、\r文字列全体をテストしてから2つのEOL文字が存在するという問題に直面するのではなく、最初の文字をテストして、それが正しいかどうかを確認できませんか?
コーディグレー

@CodyGrayいいえ。一度に1文字ずつ読んでいます。それがSystem.inあなたが直接できるようにすることです(まあ、一度に文字の配列を読むことができます)。入力内のすべての文字に対してループボディ(空)が実行されます
ジャスティン

9

HTML5、33の 22バイト

<form><input required>

<form><input required>

説明

required属性<input>により、値を入力しない場合、ブラウザはユーザーに「このフィールドは必須です」-sort-of-messageを通知します。ただし、値を入力すると、値はaction属性のURLに送信されます<form>(明示的に値を指定していないため、この場合は現在のファイル自体です)。

これは、最新バージョンのGoogle Chrome(バージョン55.0)でテストされました。他のブラウザおよびバージョンで動作する場合があります。


action=/一部のブラウザで動作する可能性があります。
ニール

1
おそらくaction属性を完全に捨てることができます。
シャギー

1
@Arjun action=y.pほとんどのユーザーエージェントactionは、特定されていない場合は同じ場所に送信されるため、必要ありません
MayorMonty

@Arjun <form><input required>最新バージョンのGoogle Chromeを試してみましたが、意図したとおりに動作します。そして、あなたが懐疑的であるなら、ただ使用してくださいaction=#。これで2バイト節約できます。
イスマエルミゲル

3
@Arjunこれはコードゴルフのひどいアイデアです。Android 1.0、1.6、2.0、2.1、3.0、3.1、4.xx、5.x、6.x、...、iPhone 2-7を所有していないため、すべてのブラウザーについて言うことはできませんGoogle Chrome、Firefox 3.0+のすべてのリリース、およびオンおよびオンおよびオン...実際、すべてのHTML5ブラウザーがこのrequired属性を実装しているわけではありません!また、#私が正しいのであれば、HTML 1.0以降で利用可能なパスです。またaction、フォームにno を追加することaction="."は、ファイル自体であると同じです。
イスマエルミゲル

8

ゼリー、3バイト

ɠṆ¿

TIOをあまり見る必要はありません、怖いです。

オンラインでお試しください!

使い方

これはniladicプログラムであり、入力引数を取りません。暗黙の引数と戻り値は両方ともこの場合、 0です。

¿(while)は、リンクスタックから2つのリンク(条件()と本文)をポップするクイックです。

は単項アトムです:フラット論理NOT。前の戻り値が偽(ここでは0または空の文字列)の場合、1を返し、本文が呼び出されます。

ɠニラド原子です。STDINから生の行を読み取り、結果を返します。

一度ɠ、非空行を読み込み返す0を、我々はループから抜け出します。


速いですか?まぁ。
アダム



5

Perl 5、8 + 1(-pまたは-nフラグ)バイト

/./&&die

正規表現が改行以外のものと一致するとすぐに、stdinからの入力を受け取り、終了します。


5

C、52バイト33バイト、29バイト

ジャスティンのおかげで-19バイト

クリストフのおかげで-4バイト

main(){while(getchar()==10);}

10は '\ n'と等しい-これが明らかでない場合。


1
You can golf it by 1 char by using a ; instead of the {} for the while loop
Justin

1
Although not standards compliant, you can remove the #include<stdio.h> if you switch to while(getchar()==10): int main(){while(getchar()==10);}.
Justin

2
@Justin main(){while(getchar()==10);} is enough no need for default int.
Christoph

1
main(){scanf("%s");} would also work, if space-only lines can count as empty.
12431234123412341234123

1
@DanielEarwicker You are right. That didn't seem clear to me the first time I read the question, but now I see what you are saying. In that case, there are many non-conforming answers to this question.
Justin

5

Bash, 51 bytes (39 without "#!/bin/bash")

It's my first time participating in PPCG, so dont be to rude ;D

#!/bin/bash
read a
while [ -z "$a" ]
do
read a
done

1
Welcome to PPCG.
Adám

3
Welcome to PPCG!, if you're planning on using Bash as a golfing language you might find This Post Interesting, it has a bunch of tips for making bash programs as small as humanly possible, there's also a More General Version you might want to skim through too.
colsw

@ConnorLSW Wow, thanks, that Post will help me a lot. I'll try to edit my Answer with this new tips ASAP.
Nicolas Fischer

4
you do not need #!/bin/bash
12431234123412341234123

Well, also the first time for me :) But you could perhaps use recursion: [ -z `line` ] && $0 or if the deprecated line is not on your system: [ -z `head -n1` ] && $0. That should either be 20 or 24 bytes.
Yeti

4

Java, 128 126 bytes

2 bytes thanks to Kevin Cruijssen.

import java.util.*;class a{public static void main(String[]a){for(Scanner s=new Scanner(System.in);s.nextLine().isEmpty(););}}

Try it online!


1
You can golf it by two bytes: Change the while to a for and put the Scanner s=new Scanner(System.in); inside it. And change the .equals("") to .isEmpty().
Kevin Cruijssen

I'm pretty sure you can remove the import if you inline the use of the scanner; pretty sure leaking a scanner works just fine: while(new java.util.Scanner(System.in).nextLine().isEmpty());
Justin

4

C# (.NET Core), 66 bytes

class C{static void Main(){for(;System.Console.ReadLine()!="";);}}

Try it online!

-6 bytes thanks to raznagul.


1
You can save 6 bytes by writing System.Console.ReadLIne directly and drop the using-Statement.
raznagul

A while loop would be the same number of bytes, but methinks a more idiomatic way of writing the code than a for loop.
Cody Gray

you can save 24 bytes by writing it as a lambda in the form () => {}
Ceshion

or alternatively, 7 by making it an instance method
Ceshion

4

QBasic 4.5, 15 bytes

INPUT a$:IF a$=""THEN RUN

Asks for input, then checks if any was given. If not, RUN restarts the program.


2
Didn't know about the RUN. +1. (That rhymes too; RUN and PLUS ONE :) )
Arjun


4

R, 27 24 23 22 bytes

while(!sum(scan()^0))t

Takes input from stdin, and repeat as long as input is of length 0. Cut off some bytes due to Jarko Dubbeldam and MickyT. Replaced the {} with t to save another byte.


I think you can omit the ,'', since neither input (string or numeric) nor way of terminating was specified in the challenge.
JAD

@JarkoDubbeldam: good catch! I didn't realize errors were a valid termination.
rturnbull

would while(!sum(scan()^0)){} work as well?
MickyT

@MickyT Seems to work. Apparently even Inf^0 is 1. Nice catch with the sum(). Too bad the ^0 is need to handle 0 as input.
JAD

@MickyT That's a great find with the use of sum, thanks! I've updated the answer.
rturnbull

3

PHP, 18 bytes

while(!readline())

Does it check that the line of text is not empty?
Daniel Earwicker

1
Yes, as soon as the line is not empty readline() returns something else than an empty string. Non empty strings evaluate to true in php - atleast most of them do. "0" seems to be an exception as I just read in the docs. Well I guess my answer is wrong now.
Skynet

3

AWK, 8 11 bytes

NF{exit}

Wait for input. If the number of fields in input is more than 0, exit.

EDIT:

I've just realized that this doesn't work for input containing whitespace characters only. IFS needs to be set to empty string using -F "" command line option. Therefore +3 bytes.

Try it online!


Actually, you could replace NF with 1. Then you don't need to set IFS. And grumble you beat me to and AWK solution.
Robert Benson

1
@RobertBenson I tried replacing NF with 1. In that case the program exits given any input, including empty newline.
Maxim Mikhaylov

I missed that part of the spec.
Robert Benson

3

SpecBAS - 34 bytes

1 DO : INPUT a$: LOOP UNTIL a$<>""

Just loops until non-empty string is entered.



3

Haskell, 19 17 bytes

f=do""<-getLine;f

Defines a function f that if it reads the empty line calls itself again. When reading a non-empty line an exception is raised and the recursion is stopped.

Edit: @Laikoni replaced the parameter with a pattern match and saved 2 bytes. Thanks!


Didn't think Haskell IO code could be as short as this!
Geeky I

17 bytes: f=do""<-getLine;f
Laikoni

@Laikoni: nice catch. Thanks!
nimi

3

Aceto, 9 5 4 bytes

read a value, negate it (!; implicitly casting to a boolean), and mirror horizontally if the value is truthy (|).

!
r|

Wait, Aceto runs **up**‽ And they say APL is strange for running from right to left…
Adám

3
@Adám Aceto runs along a 2D Hilbert curve.
L3viathan

3

Java, 66 64 60 bytes

int a(){return!System.console().readLine().isEmpty()?0:a();}

My first Java answer, would love some tips!


You can probably remove the space between return and !. .equals("") == .isEmpty(). You can return int instead of String. OTOH, return void but either while(System...) or if(System...)a();
Justin

you can do int a(){..?0:a();}, saves 3 bytes
Khaled.K

@Khaled.K actually 4!
OldBunny2800

To @Justin and Khaled.K, thank you for your advice!
OldBunny2800

2

Braingolf, 8 bytes [non-competing]

1[{?:+|]

Non competing because {, which reads input from STDIN, was added to braingolf after this challenge's post date.

Explanation:

1         Pushes 1 to the stack
 [        Begins while loop
  {       Reads from STDIN, returns -1 if no input given
   ?      Checks if last item in stack is > 0
    :     Else
     +    Adds the last 2 items together, however there is only 1 item, so add it to itself
      |   Endif
       ]  End While, decrement first item in stack, or exit if first item is 0

Haha, you implemented input an hour after the challenge was posted
ASCII-only

@ASCII-only had to do it eventually, haha
Skidsdev



2

PowerShell, 20 Bytes

for(;!(read-host)){}

runs a for loop,

read-host prompts for input

if read-host returns nothing it evals to false, so we invert that !(...) and use that as the loop end check.

much shorter than any do{$a=read-host}while($a-eq"") type solution involving variables.



2

CJam, 5 bytes

{l!}g

Try it online! (TIO doesn't really show the proper behaviour)

Explanation

{      e# Do:
 l     e#  Read next line of input
  !    e#  Boolean negate (empty strings are falsy)
   }g  e# Pop the top of stack, if truthy, repeat.
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.