ยืนยันคำตอบ


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

โปรแกรมทำงานและดึงแบบฟอร์มขึ้นทันทีด้วยฟิลด์ข้อความเดียวและOKปุ่ม
ผู้ใช้คลิกOKปุ่ม
ไม่มีอะไรเกิดขึ้น.
ผู้ใช้วาง "hello world" ลงในช่องข้อความและคลิกOKปุ่ม
โปรแกรมสิ้นสุดลง

ตัวอย่างที่ 2

ฟังก์ชั่นถูกเรียกใช้และแสดงบรรทัดว่างทันทีและเคอร์เซอร์กะพริบ ผู้ใช้กด
เคอร์เซอร์เลื่อนลงหนึ่งบรรทัด ผู้ใช้กด เคอร์เซอร์เลื่อนลงหนึ่งบรรทัด ผู้ใช้กด ฟังก์ชันส่งคืนEnter

Enter

PPCGEnter


อนุญาตให้นับบรรทัดว่างเท่านั้นหรือไม่?
12431234123412341234123

@ 12431234123412341234123 เลขที่
Adám

จำเป็นหรือไม่ที่จะต้องรอสายเต็มเมื่อผู้ใช้เริ่มป้อนข้อมูล?
Matteo Italia

ตกลงจากความคิดเห็นในคำตอบที่ถูกลบของ @ CodyGray ดูเหมือนว่าจะไม่ได้รับอนุญาต
Matteo Italia

@MatteoItalia ใช่ถูกต้อง สายของการป้อนข้อมูล
Adám

คำตอบ:


16

TI-BASIC, 2 ไบต์

:Prompt X

TI-BASIC ทำสิ่งนี้โดยอัตโนมัติ เมื่อป้อนข้อมูลแล้วจะปิด

นี่คือ GIF:

enter image description here

ชมการนับจำนวนที่enterสำคัญในประวัติการกดปุ่ม ที่สร้างขึ้นด้วยTI-SmartView CEและezgif.com


1
อืม .. นี่คือ 2 ไบต์?
Geeky ฉัน

9
TI-ขั้นพื้นฐานตามโทเค็น
Scott Milner

และจะไม่ใช้คำตอบสำหรับเข้า? มีทุกที่ที่จะทดสอบออนไลน์นี้หรือไม่
Adám

@ Adámจะไม่ได้รับคำตอบ เท่าที่ฉันทราบไม่มีสถานที่ที่คุณสามารถไปออนไลน์ได้โดยไม่ต้องมีภาพ TI ROM ฉันสามารถโพสต์. gif ได้เมื่อฉันกลับถึงบ้าน
Scott Milner

1
@ Adámผิดกฎหมายในการทดสอบ TI-BASIC ออนไลน์โดยไม่ต้องมี ROM เอง
Erik the Outgolfer



15

JavaScript, 37 22 17 ไบต์

while(!prompt());

while(!prompt());

คำอธิบาย

whileคำหลักเริ่มwhileห่วง ในเงื่อนไขของลูป!prompt()ขอให้ป้อนข้อมูลและตรวจสอบว่าได้รับหรือไม่ หากไม่ได้รับเนื้อความของลูปจะถูกดำเนินการซึ่งในกรณีของเรานั้นว่างเปล่าล่ามจะกลับไปที่เงื่อนไขลูป กระบวนการเดียวกันเกิดขึ้นซ้ำแล้วซ้ำอีกจนกว่าผู้ใช้จะให้อินพุต


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

@ Adám Wow ฉันแค่คิดเกี่ยวกับวิธีทำให้โซลูชันเล็กลงและคุณก็ได้คำตอบมาด้วย!
Arjun

ดูเหมือนชัดเจน อย่างไรก็ตามคอนโซลเบราว์เซอร์ของฉันปฏิเสธที่จะดำเนินการได้โดยไม่ต้องต่อท้ายในขณะที่การแสดงออกของคุณเริ่มต้นทำงานได้ดีโดยไม่ต้อง; ;มีความคิดอะไรบ้าง
Adám

ไม่ Chrome ดังนั้นหากเครื่องมือจาวาสคริปต์ใด ๆ สามารถจัดการกับการละเว้นของนิพจน์ปัจจุบัน;คุณสามารถบันทึกไบต์ :-) นั้น
2560

1
@ Adámเพิ่งทราบสาเหตุที่แท้จริงของข้อผิดพลาดที่เกิดขึ้นหลังจากการลบเซมิโคลอนออกจากซอร์สโค้ด เหตุผลที่เกิดขึ้นคือในขณะที่วนรอบคาดว่าจะมีคำสั่ง (หรือกลุ่มของคำสั่ง) เป็นร่างกายห่วงหลังจากเงื่อนไขวง การเพิ่มเครื่องหมายอัฒภาคทำให้คิดว่าร่างกายวนซ้ำ อย่างไรก็ตามการลบเครื่องหมายอัฒภาคทำให้คาดว่าคำสั่งถัดไปเป็นร่างกายห่วง แต่ในกรณีของเราไม่มีข้อความใหม่เกิดขึ้นหลังจากเงื่อนไขลูป ดังนั้นมันจะพ่นข้อผิดพลาด ฉันหวังว่ามันชัดเจนตอนนี้ ฉันอธิบายสิ่งต่าง ๆ แย่มาก !! : p
Arjun

9

Java, 55 ไบต์

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

ถ้าฉันจำได้อย่างถูกต้อง (มันผ่านมาซักพักแล้วที่ PPCG) โปรแกรมของฉันก็สามารถใช้งานได้

This is system specific; it only works on systems where the end-of-line character is a single newline. If the end-of-line character is instead a carriage return, replace the 10 with 13. On Windows, this doesn't work, as end-of-line on windows is \r\n.

This makes use of the fact that I can read directly from System.in.


2
Welcome back, if only for this answer! :D
Conor O'Brien

3
Welcome back @Justin!
Digital Trauma

1
Not planning on being active; just dropping by for a question here and there
Justin

To avoid the "doesn't work on Windows" problem, can't you just test the first character and see if it is \r, as opposed to testing the entire string, and then running into problems with there being 2 EOL characters?
Cody Gray

@CodyGray เลขที่ฉันอ่าน 1 ตัวอักษรในเวลาเดียวกัน; นั่นคือทั้งหมดที่System.inให้คุณทำโดยตรง (ดีคุณ chan อ่านอาร์เรย์ของตัวอักษรในเวลา) เนื้อความวน (ว่าง) ของฉันทำงานสำหรับตัวอักษรทุกตัวในอินพุต
Justin

9

HTML5, 33 22 ไบต์

<form><input required>

<form><input required>

คำอธิบาย

requiredแอตทริบิวต์ใน<input>สาเหตุของเบราว์เซอร์ที่จะแจ้งให้ผู้ใช้ "ข้อมูลนี้จะต้อง" จัดเรียงของข้อความถ้าพวกเขาไม่ป้อนค่า อย่างไรก็ตามเมื่อพวกเขาป้อนค่าค่าจะถูกส่งไปยัง URL ของactionแอตทริบิวต์ของ<form>(ซึ่งในกรณีของเราคือไฟล์ปัจจุบันเองเนื่องจากเราไม่ได้ระบุค่าใด ๆ ไว้อย่างชัดเจน)

สิ่งนี้ได้รับการทดสอบกับ Google Chrome รุ่นล่าสุด (รุ่น 55.0) อาจทำงานในเบราว์เซอร์และรุ่นอื่น ๆ


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.


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




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

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

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.



โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.