坚持答案


31

在运行时,不断提示输入一行,直到用户输入了某些内容(空换行符除外),即不只是按EnterOK。既不需要也不禁止输出或结果。

伪代码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号
亚当

用户开始输入数据后是否有必要等待全行?
Matteo Italia

好的,从@CodyGray删除的答案中的评论看来,这是不允许的。
Matteo Italia

@MatteoItalia是的,正确的;一行输入
2015年

Answers:


16

TI-BASIC,2个字节

:Prompt X

TI-BASIC会自动执行此操作。输入后,它将退出。

这是GIF:

enter image description here

观看enter按键历史记录中按键的计数。使用TI-SmartView CEezgif.com创建


1
嗯..这是2个字节?
极客我


而且不需要Enter输入答案吗?有什么地方可以在线测试吗?
亚当

@Adám不需要回车就可以了。据我所知,没有TI ROM映像,您无法在任何地方上网。不过,我回家后可以发布.gif。
斯科特·米尔纳

1
@Adám如果不自己拥有ROM,则在线测试TI-BASIC是非法的。
大公埃里克(Erik the Outgolfer)'17年


15

sed,4

/./q

等待具有1个或多个字符的输入行,然后退出。

在线尝试。但它在实时shell上效果更好:

sed '/./q'

15

JavaScript,37 22 17字节

while(!prompt());

while(!prompt());

说明

while关键字开始while循环。在循环的情况下,!prompt()要求输入并检查是否输入。如果未给出,则执行循环主体(在我们的情况下为空),然后解释器返回到循环条件。一遍又一遍地进行相同的过程,直到用户输入为止。


while(""==prompt(""));
2015年

@Adám哇。我只是在考虑如何缩小解决方案的范围,您想出了一个答案,谢谢!
Arjun

似乎很明显。但是,我的浏览器控制台拒绝不带尾随执行;,而没有时您的初始表达式可以正常工作;。知道为什么吗?
阿达姆(Adám),

不,Chrome。因此,如果任何JavaScript引擎都可以处理当前表达式的省略,则;可以保存该字节:-)
–Adám,

1
@Adám刚刚找出了从源代码中删除分号后产生错误的真正原因。发生这种情况的原因是,while循环期望在循环条件之后有一个语句(或一组语句)作为循环体。添加分号使其认为循环主体已结束。但是,删除分号会使它期望下一条语句为循环体。但是在我们的例子中,循环条件之后没有新的语句出现。因此,它引发一个错误。我希望现在已经清楚了。我在解释东西方面太糟糕了!:p
Arjun

9

Java,55个字节

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

如果我没记错(自从我从事PPCG以来已经有一段时间了),我的程序就可以是一个函数。

这是系统特定的;它仅适用于行尾字符为单个换行符的系统。如果结束行的字符是代替回车,替换1013。在Windows上,这是行不通的,因为Windows上的行尾是\r\n

这利用了我可以直接从阅读的事实System.in


2
欢迎回来,如果只是为了这个答案!:D
科纳·奥布莱恩

3
欢迎回来@Justin!
Digital Trauma'5

1
不打算积极活动;只是到处问一个问题
贾斯汀

为了避免出现“在Windows上不起作用”的问题,您是否只能测试第一个字符并查看它是否为\r,而不是测试整个字符串,然后遇到两个EOL字符的问题?
科迪·格雷

@CodyGray否。我一次读取1个字符;这就是System.in您直接进行的所有操作(嗯,您一次可以读取一个字符数组)。我的循环体(空)针对输入中的每个字符运行
贾斯汀

9

HTML5,33 22个字节

<form><input required>

<form><input required>

说明

required该属性<input>使浏览器通知用户“这个字段是必须的”消息-sort-的-,如果他们不输入值。然而,当他们输入一个值,该值被发送到的URL action的属性<form>(在本例中是当前文件本身,因为我们没有明确指定任何值)。

已在最新版本的Google Chrome(版本55.0)上进行了测试。可能在其他浏览器和版本中工作。


action=/可能在某些浏览器上有效。
尼尔

1
您可能会action完全放弃该属性。
毛茸茸的

1
@Arjun这action=y.p不是必需的,因为如果没有action具体说明,大多数用户代理将提交到相同的位置
MayorMonty

@Arjun我已经尝试<form><input required>使用最新版的Google Chrome浏览器,并且可以正常使用。如果您对此表示怀疑,请使用action=#。这样可以节省2个字节。
Ismael Miguel's

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)是从链接堆栈弹出两个链接的快捷方式:条件()和主体。

是一个单原子:平逻辑非。如果先前的返回值是伪造的(此处为0或空字符串),则返回1并调用主体。

ɠ是一个尼拉德原子;它从STDIN读取原始行并返回结果。

一旦ɠ读取一个非空行,返回0,我们打破循环了。


这么快吧?呃,好吧。
阿达姆(Adám)'17年



5

Perl 5、8 + 1(-p或-n标志)字节

/./&&die

regex匹配除换行符以外的任何内容,然后从stdin接受输入并终止。


5

C,52字节33字节,29字节

-19字节,感谢贾斯汀

-4个字节感谢Christoph

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

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.
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.