Bersikeras jawaban


31

Pada saat runtime, tetap meminta baris input sampai pengguna memasukkan sesuatu (selain baris baru yang kosong), yaitu tidak hanya menekan Enteratau OK. Output atau hasil tidak diperlukan atau dilarang.

Kode semu 1

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

Kode semu 2

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

Contoh 1

Program berjalan dan segera memunculkan formulir dengan bidang teks tunggal dan OKtombol.
Pengguna mengklik OKtombol.
Tidak ada yang terjadi.
Pasta pengguna "halo dunia" ke dalam bidang teks dan klik OKtombol.
Program berakhir.

Contoh 2

Fungsi dipanggil dan segera menampilkan garis kosong dan kursor yang berkedip.
Pengguna menekan Enter.
Kursor bergerak ke bawah satu baris.
Pengguna menekan Enter.
Kursor bergerak ke bawah satu baris.
Pengguna menekan PPCGEnter
Fungsi kembali.


apakah diizinkan untuk menghitung garis hanya ruang sebagai kosong?
12431234123412341234123

@ 12431234123412341234123 No.
Adám

Apakah perlu menunggu baris penuh setelah pengguna mulai memasukkan data?
Matteo Italia

Ok, dari komentar di jawaban yang dihapus @ CodyGray sepertinya tidak diizinkan.
Matteo Italia

@MatteoItalia Ya, benar; jalur input .
Adm

Jawaban:


16

TI-BASIC, 2 byte

:Prompt X

TI-BASIC melakukan ini secara otomatis. Setelah input diberikan, itu akan berhenti.

Ini GIF:

masukkan deskripsi gambar di sini

Tonton hitungan pada entertombol di Key Press History. Dibuat dengan TI-SmartView CE dan ezgif.com .


1
Hmm .. ini 2 byte?
Geeky I


Dan itu tidak akan mengambil Enter untuk jawaban? Apakah ada tempat untuk menguji ini secara online?
Adám

@ Adám Tidak perlu memasukkan jawaban. Sejauh yang saya ketahui, tidak ada tempat Anda bisa online tanpa memiliki gambar ROM TI. Saya dapat memposting .gif saat pulang ke rumah.
Scott Milner

1
@ Adám Adalah ilegal untuk menguji TI-BASIC online tanpa memiliki ROM sendiri.
Erik the Outgolfer


15

sed, 4

/./q

Menunggu baris input yang memiliki 1 atau lebih karakter, lalu berhenti.

Cobalah online . Tetapi ini bekerja lebih baik pada shell langsung:

sed '/./q'

15

JavaScript, 37 22 17 byte

while(!prompt());

while(!prompt());

Penjelasan

Kata whilekunci memulai whileperulangan. Dalam kondisi loop, !prompt()meminta input dan memeriksa apakah itu diberikan atau tidak. Jika tidak diberikan, tubuh loop dijalankan, yang dalam kasus kami kosong, maka penerjemah kembali ke kondisi loop. Proses yang sama terjadi berulang-ulang sampai pengguna memberikan input.


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

@ Adám Wow. Saya hanya berpikir tentang bagaimana membuat solusi lebih kecil, dan Anda mendapat jawaban terima kasih!
Arjun

Tampak jelas. Namun, konsol browser saya menolak untuk dieksekusi tanpa trailing ;, sedangkan ekspresi awal Anda berfungsi dengan baik tanpa ;. Ada yang tahu kenapa?
Adám

Tidak, Chrome. Jadi, jika mesin JavaScript mana pun dapat menangani penghilangan ekspresi saat ini ;, Anda dapat menyimpan byte itu :-)
Adám

1
@ Adám Baru tahu penyebab sebenarnya dari kesalahan yang dihasilkan setelah penghapusan titik koma dari kode sumber. Alasan mengapa itu terjadi adalah bahwa sementara loop mengharapkan pernyataan (atau sekelompok pernyataan) sebagai tubuh loop setelah kondisi loop. Menambahkan titik koma membuatnya berpikir bahwa badan loop sudah berakhir. Namun, menghapus titik koma menyebabkannya mengharapkan pernyataan berikutnya sebagai badan loop. Namun dalam kasus kami, tidak ada pernyataan baru yang muncul setelah kondisi loop. Jadi, ada kesalahan. Saya harap itu jelas sekarang. Saya payah dalam menjelaskan hal-hal !! : p
Arjun

9

Java, 55 byte

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

Jika saya ingat dengan benar (sudah lama sejak saya aktif di PPCG), program saya bisa saja berfungsi.

Ini spesifik sistem; ini hanya bekerja pada sistem di mana karakter end-of-line adalah satu baris baru. Jika karakter end-of-line bukan carriage return, ganti 10dengan 13. Di Windows, ini tidak berfungsi, seperti end-of-line di windows\r\n .

Ini memanfaatkan fakta bahwa saya dapat membaca langsung dari System.in.


2
Selamat datang kembali, jika hanya untuk jawaban ini! : D
Conor O'Brien

3
Selamat datang kembali @Justin!
Trauma Digital

1
Tidak berencana aktif; hanya mampir untuk pertanyaan di sana
Justin

Untuk menghindari masalah "tidak berfungsi pada Windows", tidak bisakah Anda hanya menguji karakter pertama dan melihat apakah itu \r, yang bertentangan dengan menguji seluruh string, dan kemudian mengalami masalah dengan adanya 2 karakter EOL?
Cody Grey

@CodyGray No. Saya sedang membaca 1 karakter sekaligus; hanya itu yang bisa System.inAnda lakukan secara langsung (well, Anda bisa membaca berbagai karakter sekaligus). Tubuh putaran saya (kosong) dijalankan untuk setiap karakter dalam input
Justin

9

HTML5, 33 22 byte

<form><input required>

<form><input required>

Penjelasan

The requiredatribut pada <input>menyebabkan browser untuk menginformasikan pengguna "bidang ini diperlukan" -sort-of-pesan, jika mereka tidak memasukkan nilai. Namun, ketika mereka memasukkan nilai, nilai tersebut dikirim ke URL actionatribut<form> (yang dalam kasus kami adalah file saat ini, karena kami belum menentukan nilai apa pun secara eksplisit).

Ini diuji pada versi terbaru Google Chrome (versi 55.0). Dapat bekerja di browser dan versi lain.


action=/mungkin berfungsi pada beberapa browser.
Neil

1
Anda mungkin bisa membuang actionatribut sama sekali.
Shaggy

1
@Arjun action=y.ptidak diperlukan, karena sebagian besar Agen Pengguna akan mengirim ke lokasi yang sama jika tidak ada yang actionditentukan
MayorMonty

@Arjun Saya sudah mencoba <form><input required>versi Google Chrome terbaru dan berfungsi sebagaimana dimaksud. Dan jika Anda skeptis, gunakan saja action=#. Itu menghemat 2 byte.
Ismael Miguel

3
@ Arjun Itu adalah ide yang buruk untuk kode golf. Saya tidak dapat mengatakan tentang setiap browser, karena saya tidak memiliki Android 1.0, 1.6, 2.0, 2.1, 3.0, 3.1, 4.xx, 5.x, 6.x, ..., iPhone 2 - 7, setiap rilis Google Chrome, Firefox 3.0+ dan lebih dan lebih dan lebih ... Bahkan, tidak semua browser HTML5 menerapkan requiredatribut! Juga, #apakah jalur tersedia sejak HTML 1.0, jika saya benar. Dan menambahkan tidak actionke formulir Anda sama dengan action=".", yaitu file itu sendiri.
Ismael Miguel

8

Jelly , 3 byte

ɠṆ¿

Tidak banyak yang terlihat di TIO, saya khawatir.

Cobalah online!

Bagaimana itu bekerja

Ini adalah program niladik, artinya tidak memerlukan argumen input. Argumen implisit dan nilai pengembalian keduanya 0 dalam kasus ini.

¿(while) adalah quick yang memunculkan dua tautan dari tumpukan tautan: condition ( ) dan body.

adalah atom monadik: logika datar TIDAK. Jika nilai pengembalian sebelumnya falsy (di sini, 0 atau string kosong), itu mengembalikan 1 dan tubuh dipanggil.

ɠadalah atom niladik; itu membaca garis mentah dari STDIN dan mengembalikan hasilnya.

Setelah ɠmembaca baris yang tidak kosong, mengembalikan 0 dan kami keluar dari loop.


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.
Dengan menggunakan situs kami, Anda mengakui telah membaca dan memahami Kebijakan Cookie dan Kebijakan Privasi kami.
Licensed under cc by-sa 3.0 with attribution required.