Bir cevapta ısrar etmek


31

Çalışma zamanında, kullanıcı (boş bir yeni satır dışında) bir şey girinceye kadar bir giriş satırı istemeye devam edin, yani sadece Enterveya tuşuna basmaz OK. Çıktı veya sonuç gerekli değildir ve yasaktır.

Sözde kod 1

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

Sözde kod 2

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

örnek 1

Program çalışır ve hemen tek bir metin alanı ve bir OKdüğme ile bir form açılır .
Kullanıcı OKdüğmeyi tıklar .
Hiçbir şey olmuyor.
Kullanıcı "merhaba dünyayı" metin alanına yapıştırır ve OKdüğmeyi tıklatır .
Program sonlandırılıyor.

Örnek 2

İşlev çağrılır ve hemen boş bir satır ve yanıp sönen bir imleç görüntüler.
Kullanıcı basar Enter.
İmleç bir satır aşağı hareket eder.
Kullanıcı basar Enter.
İmleç bir satır aşağı hareket eder.
Kullanıcı basar PPCGEnter
İşlev döner.


sadece boşluk içeren satırları boş olarak saymaya izin verilir mi?
12431234123412341234123

@ 12431234123412341234123 No.
09

Kullanıcı veri girmeye başladığında tam satır beklemek gerekli midir?
Matteo Italia

Tamam, @ CodyGray'in silinen yanıtındaki bir yorumdan izin verilmiyor gibi görünüyor.
Matteo Italia

@MatteoItalia Evet, doğru; bir giriş satırı .
Adám

Yanıtlar:


16

TI-BASIC, 2 bayt

:Prompt X

TI-BASIC bunu otomatik olarak yapar. Giriş verildikten sonra, sonlandırılacak.

İşte bir GIF:

enter image description here

enterTuşa Basma Geçmişindeki tuştaki sayımı izleyin . Oluşturulan TI-SmartView CE ve ezgif.com .


1
Hmm .. bu 2 bayt mı?
Geeky


Ve bir cevap için Enter almayacak? Bunu çevrimiçi olarak test etmek için herhangi bir yer var mı?
Adám

@ Adám Bir cevap için girilmez. Bildiğim kadarıyla, zaten bir TI ROM görüntüsüne sahip olmadan çevrimiçi olabileceğiniz hiçbir yer yok. Yine de eve gittiğimde .gif gönderebilirim.
Scott Milner

1
@ Adám TI-BASIC’i bir ROM’a sahip olmadan çevrimiçi olarak test etmek yasaktır.
Outgolfer Erik,



15

JavaScript, 37 22 17 bayt

while(!prompt());

while(!prompt());

açıklama

whileAnahtar kelime başlar whiledöngü. Döngü durumunda !prompt()girdi ister ve verilip verilmediğini kontrol eder. Eğer verilmezse, bizim durumumuzda boş olan döngünün gövdesi çalıştırılır, sonra yorumlayıcı döngü durumuna geri döner. Aynı işlem, kullanıcı girişi yapana kadar tekrar tekrar gerçekleşir.


while(""==prompt(""));
Adám

@ Adám Wow. Ben sadece çözümü nasıl daha küçük hale getireceğimi düşünüyordum ve bir cevap aldınız!
Arjun

Açık görünüyordu. Ancak, tarayıcı konsolum iz bırakmadan çalışmayı reddetti ;, ilk ifadeniz ise iyi çalıştı ;. Neden bir fikrin var mı?
Adám

Hayır, Chrome. Eğer herhangi bir JavaScript motoru mevcut ifadenin ihmalini kaldırabilirse ;, bu baytı kaydedebilirsiniz :-)
Adám

1
@ Adám Noktalı virgülün kaynak koddan çıkarılmasından sonra ortaya çıkan hatanın asıl nedenini çözdüm. Bunun olmasının sebebi, döngü koşulundan sonraki döngü döngü ifadesi olarak döngü ifadesi (veya bir grup ifadeler) beklemektir. Noktalı virgül eklemek, döngü gövdesinin bittiğini düşündürür. Ancak, noktalı virgül kaldırmak bir sonraki ifadeyi döngü gövdesi olarak beklemesine neden olur. Fakat bizim durumumuzda, döngü koşulundan sonra yeni bir ifade gelmez. Yani, bir hata atıyor. Umarım şimdi açıktır. Bir şeyler açıklamakta berbatım !! : p
Arjun

9

Java, 55 bayt

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

Doğru hatırlıyorsam (PPCG'de aktif olduğumdan bu yana bir süre geçti), programım sadece bir işlev olabilir.

Bu sisteme özgüdür; sadece satır sonu karakterinin tek bir yeni satır olduğu sistemlerde çalışır. Sonu hattı karakteri yerine satırbaşı ise, yerini 10ile 13. Windows'ta bu, Windows'ta satır sonu olduğu gibi çalışmaz \r\n.

Bu doğrudan okuyabildiğim gerçeğini kullanıyor System.in.


2
Sadece bu cevap için eğer tekrar hoş geldiniz! : D
Conor O'Brien,

3
@Justin'e tekrar hoş geldiniz!
Dijital Travma

1
Aktif olmayı planlamamak; burada ve orada bir soru sormak için
Justin

"Windows'ta çalışmaz" sorununu önlemek için, sadece ilk karakteri \rtest edip tüm dizgiyi sınamak yerine ve ardından 2 EOL karakteri olan sorunlarla karşılaşıp karşılaşmadığına bakamıyor musunuz?
Cody Gray,

@CodyGray Hayır. Her seferinde 1 karakter okuyorum; hepsi bu o System.inlet (iyi, chan bir anda karakter dizisi okuyun) doğrudan yapmak var. Döngü bedenim (boş) girdideki her karakter için çalıştırıldı
Justin

9

HTML5, 33 22 bayt

<form><input required>

<form><input required>

açıklama

requiredÜzerinde özellik <input>kullanıcıya bir değer girmezseniz, -Sıralama-of-mesajının "bu alan gereklidir" bilgilendirmek tarayıcı neden olur. Ancak, bir değer girdiklerinde değer, (açıkça bizim için bir değer belirtmediğimiz için, bizim durumumuzda mevcut dosyanın kendisidir) actionözniteliğinin URL'sine gönderilir <form>.

Bu, Google Chrome'un en son sürümünde (sürüm 55.0) test edildi. Diğer tarayıcılarda ve sürümlerde çalışabilir.


action=/bazı tarayıcılarda çalışabilir.
Neil

1
Muhtemelen bu actionniteliği tamamen bir kenara bırakabilirsiniz .
Shaggy,

1
@Arjun action=y.pen Kullanıcı Ajanlar aynı konuma sunacak hiçbir eğer gerekli değildir actionspecificed edilir
MayorMonty

@Arjun <form><input required>En son Google Chrome sürümünü denedim ve planlandığı gibi çalışıyor. Ve şüpheci iseniz, sadece kullanın action=#. Bu 2 bayt kaydeder.
Ismael Miguel,

3
@Arjun Bu kod golf oynamak için korkunç bir fikir. Her tarayıcı hakkında bir şey söyleyemem , çünkü Android 1.0, 1.6, 2.0, 2.1, 3.0, 3.1, 4.xx, 5.x, 6.x, ..., iPhone 2 - 7, Google Chrome'un her bir sürümü, Firefox 3.0+ ve açık ve açık ve açık ve açık ... Aslında, tüm HTML5 tarayıcıları bu requiredözelliği uygulamaz ! Ayrıca, #eğer haklıysam, HTML 1.0’dan beri kullanılabilen bir yol. Ve actionformunuza no eklemek action=".", dosyanın kendisi ile aynıdır .
Ismael Miguel

8

Jöle , 3 bayt

ɠṆ¿

TIO'ya bakacak çok şey yok, korkarım.

Çevrimiçi deneyin!

Nasıl çalışır

Bu, niladik bir programdır, yani giriş argümanları almaz. Örtük argüman ve dönüş değeri bu durumda hem 0'dır .

¿(while), bağlantı yığından iki bağlantı açan hızlı bir işlemdir: bir koşul ( ) ve bir gövde.

monadik bir atomdur: düz mantıksal NOT. Önceki dönüş değeri sahte ise (burada, 0 veya boş bir dize), 1 döndürür ve gövde çağrılır.

ɠbir niladik atomdur; STDIN'den ham bir satır okur ve sonucu döndürür.

Bir kez ɠ, boş olmayan bir satır okur döner 0 ve biz döngünün patlak.


That fast, eh? Ah, well.
Adám



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.


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.


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

Ruby, 13 bytes

gets until/./

I wish gets took a Regexp argument.


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

Mathematica, 26 20 bytes

For[,Input[]==Null,]

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!


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.
Sitemizi kullandığınızda şunları okuyup anladığınızı kabul etmiş olursunuz: Çerez Politikası ve Gizlilik Politikası.
Licensed under cc by-sa 3.0 with attribution required.