Bestehen Sie auf eine Antwort

31

Fordern Sie zur Laufzeit so lange eine Eingabezeile an, bis der Benutzer etwas eingibt (außer einer leeren neuen Zeile), dh nicht mehr einfach Enteroder drückt OK. Ausgabe oder Ergebnis ist weder erforderlich noch verboten.

Pseudocode 1

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

Pseudocode 2

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

Beispiel 1

Das Programm wird ausgeführt und öffnet sofort ein Formular mit einem einzelnen Textfeld und einer OKSchaltfläche.
Benutzer klickt auf die OKSchaltfläche.
Nichts passiert.
Der Benutzer fügt "Hallo Welt" in das Textfeld ein und klickt auf die OKSchaltfläche.
Programm bricht ab.

Beispiel 2

Die Funktion wird aufgerufen und zeigt sofort eine leere Zeile und einen blinkenden Cursor an.
Benutzer drückt Enter.
Der Cursor bewegt sich eine Zeile nach unten.
Benutzer drückt Enter.
Der Cursor bewegt sich eine Zeile nach unten.
Benutzer drückt PPCGEnter
Funktion kehrt zurück.

Adam
quelle
Dürfen nur Leerzeilen als leer gezählt werden?
12431234123412341234123
@ 12431234123412341234123 Nr.
Adám
Muss auf die vollständige Zeile gewartet werden, sobald der Benutzer mit der Dateneingabe beginnt?
Matteo Italia
Ok, aus einem Kommentar in @ CodyGrays gelöschter Antwort geht hervor, dass dies nicht erlaubt ist.
Matteo Italia
@ MatteoItalia Ja, richtig; eine Eingabezeile .
Adám,

Antworten:

16

TI-BASIC, 2 Bytes

:Prompt X

TI-BASIC macht das automatisch. Sobald die Eingabe erfolgt ist, wird sie beendet.

Hier ist ein GIF:

Bildbeschreibung hier eingeben

Beobachten Sie die Anzahl der enterTasten in der Tastendruck-Historie. Erstellt mit TI-SmartView CE und ezgif.com .

Scott Milner
quelle
1
Hmm .. das sind 2 Bytes?
Geeky ich
9
TI-BASIC basiert auf Token .
Scott Milner
Und es wird nicht Enter für eine Antwort dauern? Gibt es irgendwo diese online zu testen?
Adám
@ Adám Es wird keine Antwort nötig sein. Soweit mir bekannt ist, gibt es keinen Ort, an dem Sie online gehen können, ohne bereits ein TI ROM-Image zu haben. Ich kann allerdings ein .gif posten, wenn ich nach Hause komme.
Scott Milner
1
@ Adám Es ist illegal, TI-BASIC online zu testen, ohne selbst ein ROM zu besitzen.
Erik der Outgolfer
15

Python 3 , 18 Bytes

while''==input():0

Probieren Sie es online!

Dennis
quelle
19
Fügen Sie also alle 9 Minuten eine weitere Sprache hinzu?
Adám
15

sed, 4

/./q

Wartet auf eine Eingabezeile mit 1 oder mehr Zeichen und wird dann beendet.

Probieren Sie es online aus . Aber es funktioniert besser auf einer Live-Shell:

sed '/./q'
Digitales Trauma
quelle
15

JavaScript, 37 22 17 Bytes

while(!prompt());

while(!prompt());

Erläuterung

Das whileSchlüsselwort startet die whileSchleife. Fordert im Zustand der Schleife eine !prompt()Eingabe an und prüft, ob diese gegeben ist oder nicht. Wenn es nicht gegeben ist, wird der Rumpf der Schleife ausgeführt, der in unserem Fall leer ist, dann kehrt der Interpreter zur Schleifenbedingung zurück. Der gleiche Vorgang geschieht immer wieder, bis der Benutzer die Eingabe vornimmt.

Arjun
quelle
while(""==prompt(""));
Adám
@ Adám Wow. Ich habe nur darüber nachgedacht, wie ich die Lösung verkleinern kann, und Sie haben eine Antwort gefunden, danke!
Arjun
Es schien offensichtlich. Meine Browserkonsole lässt sich jedoch nicht ohne ein Trailing ausführen ;, während Ihr anfänglicher Ausdruck ohne gut funktioniert hat ;. Irgendeine Idee warum?
Adám
Nein, Chrome. Wenn also eine JavaScript-Engine das Auslassen der aktuellen Ausdrücke verarbeiten ;kann, können Sie dieses Byte speichern :-)
Adám
1
@ Adám Habe gerade die wahre Ursache für den Fehler herausgefunden, der nach dem Entfernen des Semikolons aus dem Quellcode aufgetreten ist. Der Grund dafür ist, dass while-Schleife eine Anweisung (oder eine Gruppe von Anweisungen) als Schleifenkörper nach der Schleifenbedingung erwartet. Durch Hinzufügen des Semikolons wird der Eindruck erweckt, dass der Schleifenkörper beendet ist. Wenn Sie jedoch das Semikolon entfernen, wird die nächste Anweisung als Schleifenkörper erwartet. In unserem Fall kommen jedoch keine neuen Anweisungen nach der Schleifenbedingung. Es wird also ein Fehler ausgegeben. Ich hoffe es ist jetzt klar. Ich bin schrecklich, wenn es darum geht, Dinge zu erklären !! : p
Arjun
9

Java, 55 Bytes

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

Wenn ich mich richtig erinnere (es ist schon eine Weile her, seit ich bei PPCG aktiv bin), kann mein Programm nur eine Funktion sein.

Dies ist systemspezifisch. Dies funktioniert nur auf Systemen, bei denen das Zeilenendezeichen eine einzelne neue Zeile ist. Wenn das Zeilenendezeichen stattdessen ein Zeilenumbruch ist, ersetzen Sie das Zeichen 10durch 13. Unter Windows funktioniert dies nicht, wie dies bei Windows der Fall ist \r\n.

Dies macht sich die Tatsache zunutze, dass ich direkt auslesen kann System.in.

Justin
quelle
2
Willkommen zurück, wenn auch nur für diese Antwort! : D
Conor O'Brien
3
Willkommen zurück @Justin!
Digitales Trauma
1
Ich habe nicht vor, aktiv zu sein. Komme einfach hier und da auf eine Frage vorbei
Justin
Um das Problem "Funktioniert nicht unter Windows" zu vermeiden, können Sie nicht einfach das erste Zeichen testen und prüfen, ob dies der Fall ist \r, anstatt die gesamte Zeichenfolge zu testen, und dann Probleme mit zwei EOL-Zeichen bekommen?
Cody Grey
@CodyGray Nein. Ich lese jeweils 1 Zeichen. Das ist alles, was System.inSie direkt tun können (Sie können mehrere Zeichen gleichzeitig lesen). Mein Schleifenkörper (leer) wird für jedes Zeichen in der Eingabe ausgeführt
Justin
9

HTML5, 33 22 Bytes

<form><input required>

<form><input required>

Erläuterung

Das requiredAttribut auf <input>veranlasst den Browser, den Benutzer über die Nachricht "Dieses Feld ist erforderlich" zu informieren, wenn er keinen Wert eingibt. Wenn sie jedoch einen Wert eingeben, wird der Wert an die URL des actionAttributs von gesendet <form>(in unserem Fall die aktuelle Datei selbst, da wir keinen Wert explizit angegeben haben).

Dies wurde auf der neuesten Version von Google Chrome (Version 55.0) getestet. Funktioniert möglicherweise in anderen Browsern und Versionen.

Arjun
quelle
action=/ might work on some browsers.
Neil
1
You could probably ditch the action attribute altogether.
Shaggy
1
@Arjun The action=y.p isn't necessary, as most User Agents will submit to the same location if no action is specificed
MayorMonty
@Arjun I've tried <form><input required> on the latest Google Chrome version and works as intended. And if you're skeptical, just use action=#. That saves 2 bytes.
Ismael Miguel
3
@Arjun That is a terrible idea for code golfing. I can't say about every browser, since I don't own any Android 1.0, 1.6, 2.0, 2.1, 3.0, 3.1, 4.x.x, 5.x, 6.x, ..., iPhone 2 - 7, every single release of Google Chrome, Firefox 3.0+ and on and on and on and on... In fact, not all HTML5 browsers implement the required attribute! Also, # is a path available since HTML 1.0, if I'm correct. And adding no action to your form is the same as action=".", which is the file itself.
Ismael Miguel
8

Jelly, 3 bytes

ɠṆ¿

Not much to look at on TIO, I'm afraid.

Try it online!

How it works

This is a niladic program, meaning that it takes no input arguments. The implicit argument and return value are both 0 in this case.

¿ (while) is a quick that pops two links from the link stack: a condition () and a body.

is a monadic atom: flat logical NOT. If the previous return value is falsy (here, 0 or an empty string), it returns 1 and the body is called.

ɠ is a niladic atom; it reads a raw line from STDIN and returns the result.

Once ɠ reads a non-empty line, returns 0 and we break out of the loop.

Dennis
quelle
That fast, eh? Ah, well.
Adám
7

Pyth, 3 bytes

W!w

Try it online!

Translated to Python:

while Pnot(input()):
 pass
Leaky Nun
quelle
6

brainfuck, 26 bytes

+[>,----------[[-]<[-]>]<]

Try it online!

Leaky Nun
quelle
1
That TIO link is all nice and well, but it is a bit hard to tell what happens when run… ;-)
Adám
1
@Adám you can append ,. to the end to verify.
Leaky Nun
5

Perl 5, 8+1 (-p or -n flag) bytes

/./&&die

Takes input from stdin and dies as soon as the regex matches anything except a newline.

Chris
quelle
5

C, 52 bytes, 33 bytes, 29 bytes

-19 bytes thanks to Justin

-4 bytes thanks to Christoph

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

10 is equal to '\n' - In case this isn't obvious.

Skynet
quelle
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);}ist genug, keine Notwendigkeit für die Standard-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
Nicolas Fischer
quelle
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!

Leaky Nun
quelle
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.

Pavel
quelle
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.

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

Ruby, 13 bytes

gets until/./

I wish gets took a Regexp argument.

histocrat
quelle
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.

rturnbull
quelle
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())
Skynet
quelle
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!

Maxim Mikhaylov
quelle
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.

Brian
quelle
3

Mathematica, 26 20 bytes

For[,Input[]==Null,]
A Simmons
quelle
1
Shave off six bytes with For[,Input[]==Null,]. Works just as well.
Kelly Lowder
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!

nimi
quelle
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|
L3viathan
quelle
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!

OldBunny2800
quelle
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
Skidsdev
quelle
Haha, you implemented input an hour after the challenge was posted
ASCII-only
@ASCII-only had to do it eventually, haha
Skidsdev
2

Charcoal, 4 bytes

W¬Sω

Explanation

W      While
  ¬S   Logical not of string input
     ω  Print ""
ASCII-only
quelle
2

05AB1E, 5 bytes

[IõÊ#

Explanation:

[     Start infinite loop
 I    One line of input
   Ê  Not equal to
  õ   Empty string
    # Break if true

Try it online!

Okx
quelle
These are 5 Unicode code points, not 5 bytes; they are 7 bytes in UTF-8, 10 bytes in UTF-16 or 20 bytes in UTF-32.
Matteo Italia
@MatteoItalia 05AB1E uses its own encoding, therefore each of these characters are 1 byte.
Okx
Uh, sorry, I stand corrected!
Matteo Italia
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.

colsw
quelle
2

Swift, 22 bytes

while readLine()==""{}
Caleb Kleveter
quelle
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.
Business Cat
quelle