标签: while-loop

Makefile while 循环

即使将 SHELL 设置为 bash 而不是 sh,以下操作也不起作用:

doc:
    while read line; do \
      eval echo "$$line" > $(DOC) \
    done < $(DOC).templ

/bin/bash: -c: line 3: syntax error: unexpected end of file
Run Code Online (Sandbox Code Playgroud)

我想要做的是有一个模板文件,其中包含 bash 参数理解等 ( $(), ${}) 并在编译时“构建”它。有一个更好的方法吗?

bash templates makefile while-loop

2
推荐指数
1
解决办法
5561
查看次数

Java For 循环到递归函数中

public class For {
 public static void main(String[] args){
          for(int i=2; i<=1024; i *= 2){
           System.out.println("Count is: " + i);
      }
 }




public class While {
    public static void main(String[] args){
        int i = 1;
        while (i < 1024) {
            i *= 2;
            System.out.println("Count is: " + i);
      }
 }


public class DoWhile {
     public static void main(String[] args){
        int i = 1;
        if (i < 1024) {
            do { i*=2;
                System.out.println("Count is: " + i);
            } while (i < …
Run Code Online (Sandbox Code Playgroud)

java recursion for-loop while-loop

2
推荐指数
1
解决办法
1万
查看次数

while 循环中的 try-catch 方法?

我有这个代码,我想把 try-catch 放在一个 while 循环中。逻辑是,“当有输入错误时,程序会继续要求正确的输入”。我将如何做到这一点?提前致谢。

public class Random1 {

  public static void main(String[] args) {

    int g;

    Scanner input = new Scanner(System.in);
    Random r = new Random();
    int a = r.nextInt(10) + 1;


    try {
        System.out.print("Enter your guess: ");
        g = input.nextInt();
        if (g == a) {

            System.out.println("**************");
            System.out.println("*  YOU WON!  *");
            System.out.println("**************");
            System.out.println("Thank you for playing!");

        } else if (g != a) {
            System.out.println("Sorry, better luck next time!");
        }
    } catch (InputMismatchException e) {
        System.err.println("Not a valid input. Error …
Run Code Online (Sandbox Code Playgroud)

java try-catch while-loop

2
推荐指数
1
解决办法
6万
查看次数

sed 在读取行时替换变量

我正在处理一个 shell 脚本,我需要将文件不同行中的一些字符串更改为 while read 语句。结构需要这样,因为每行都会计算String_Search和String_result。

while read line 
do  
   varA="String_Search"   
   resA="String_Result"
   line=`echo $line | sed -e "s/$varA/$resA"`
   echo $line >> outputFile.txt
done < "inputFile.txt"
Run Code Online (Sandbox Code Playgroud)

该脚本不起作用并向我显示此错误消息:

sed: -e expression #1, char 31: unterminated `s' command
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮助我吗?

谢谢大家

variables loops sed while-loop

2
推荐指数
1
解决办法
7882
查看次数

带异常处理的while循环

这是我第一次使用异常处理,所以要温柔。我有一个简单的 blob 类,它接受一个 ID,该 ID 必须在 30 到 50 之间,否则会引发异常。

public class Blob {
int id;

public Blob() {

}

public Blob(int id) throws Exception {
    this.id = id;
    if (id < 30 || id > 50)
        throw new Exception ("id = " +id+ ", must be between 30 and 50 inclusive");
}
}
Run Code Online (Sandbox Code Playgroud)

它应该提示用户输入一个 id,如果它不在 30 到 50 之间,则抛出异常,并且应该继续直到用户输入有效的输入,然后只显示 id 号。

public class BlobCreator {

public static void main(String[] args) {
    int id;
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter …
Run Code Online (Sandbox Code Playgroud)

java exception-handling try-catch throw while-loop

2
推荐指数
1
解决办法
2万
查看次数

while 循环中的多个条件 - Javascript

我想提示用户输入,如果它不是有效的选项,则在输入有效输入之前重新提示。

现在,无论输入正确与否,它总是会重新提示。

这是代码:

var userChoice = prompt("Do you choose rock, paper, scissors or rope?");
while (userChoice != "rock" || "paper" || "scissors" || "rope") {
    userChoice = prompt("Sorry invalid input, please enter either: rock, paper,scissors, or rope.");
}
Run Code Online (Sandbox Code Playgroud)

看起来这应该是一件简单的事情,我是否误解了如何使用 while 循环?也许运营商错了?没有找到任何有用的东西,任何帮助表示赞赏!

javascript while-loop

2
推荐指数
1
解决办法
1万
查看次数

Python - 将函数存储在变量中

我有一个概念,我将函数存储在变量中,这对我来说更容易。但是我遇到的问题是变量中的值不是每次都动态调用函数。所以它一直返回相同的值。为了弄清楚这个问题,我做了一个代码片段来以简单的方式说明它:

def value():
        resp = requests.get('http://www.google.com').elapsed.total_seconds()
        return resp

test = value()

while True:
        print test
        time.sleep(10)
Run Code Online (Sandbox Code Playgroud)

输出:

0.00649
0.00649
Run Code Online (Sandbox Code Playgroud)

在这种情况下,在while true我打印测试时,它返回相同的值,即使我正在调用函数value()。我该如何解决这个问题?我知道我可以把这个函数放在 while 循环中,但我想把它作为一个变量。

python variables function while-loop storing-data

2
推荐指数
3
解决办法
3万
查看次数

How to abort a while loop by clicking a button using C#

I have a function that displays numbers using a while loop but I want to stop execution of a while loop at random variable value using c# by clicking a button.

For Example:

private void FrqSent()
{
    int i = 1;
    while (i <= 5)       
    {  
        i = i + 1;
    }  
}
Run Code Online (Sandbox Code Playgroud)

c# loops break while-loop

2
推荐指数
1
解决办法
2104
查看次数

Ag Silver Searcher:在while循环中使用时的输出文件名

在Bash while 循环中使用ag( edit: version 0.33.0 ) 时,我无法让它显示任何文件名:

#!/bin/bash
cat <<__m | while read mod; do ag --nogroup --filename --ignore=local "^use $mod" ./; done
CGI
CGI::Push
__m
Run Code Online (Sandbox Code Playgroud)

输出:

use 4.08;
use 4.08;
...
Run Code Online (Sandbox Code Playgroud)

当直接在命令行上执行它时,它的工作原理是:

ag --nogroup --filename --ignore=local "^use CGI" ./
Run Code Online (Sandbox Code Playgroud)

输出:

dir/file/ModA.pm:12:use CGI 4.08;
dir/file/ModB.pm:1:use CGI 4.08;
Run Code Online (Sandbox Code Playgroud)

我怎样才能始终获得内联文件名?

bash while-loop ag

2
推荐指数
1
解决办法
780
查看次数

c# 循环直到 Console.ReadLine = 'y' 或 'n'

我对 c# 还很陌生,正在编写一个简单的控制台应用程序作为练习。我希望应用程序提出一个问题,并且只有在用户输入等于“y”或“n”时才进入下一段代码。这是我到目前为止所拥有的。

static void Main(string[] args)
{

    string userInput;
    do
    {
        Console.WriteLine("Type something: ");
        userInput = Console.ReadLine();
    }   while (string.IsNullOrEmpty(userInput));

    Console.WriteLine("You typed " + userInput);
    Console.ReadLine();

    string wantCount;
    do
    {
        Console.WriteLine("Do you want me to count the characters present? Yes (y) or No (n): ");
        wantCount = Console.ReadLine();
        string wantCountLower = wantCount.ToLower();
    }   while ((wantCountLower != 'y') || (wantCountLower != 'n'));
}
Run Code Online (Sandbox Code Playgroud)

从那string wantCount;以后我就遇到麻烦了。我想要做的是询问用户是否要计算字符串中的字符数,然后循环该问题,直到输入 'y' 或 'n'(不带引号)。

请注意,我还想满足输入的大写/小写的需求,所以我想将 wantCount 字符串转换为小写 - 我知道我目前的方式string wantCountLower在循环内设置时不起作用,所以我然后不能在while 子句中的循环外引用。 …

c# loops console-application string-comparison while-loop

2
推荐指数
1
解决办法
9830
查看次数