标签: while-loop

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万
查看次数

Python while 循环语法无效

在我展示代码之前,只是提供一些我正在处理的项目的背景知识。我目前正在开发一个 Python 脚本,该脚本将在 Raspberry Pi 上运行,以监控地下室污水泵的浮子开关。此代码将检查污水泵是否不符合以下两个标准:

  1. 如果开关打开超过三分钟
  2. 如果开关在三分钟内打开和关闭超过 10 次

我还没有完成其余的代码,但这是我所拥有的:

import time

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)
floatSwitch = GPIO.input(17)

import smtplib

running = True
log = open("sumpPumpLog.txt", "r+")
startTime = time.time()


def elapsedTime():
    """This function checks how much time
    has elapsed since the timer has started"""
    endtime = time.time()
    elapsed = endtime - starttime
    return elapsed


def sendEmail(*msg):
    """This function sends an email to selected recipients with a custom
    message as well as the log file …
Run Code Online (Sandbox Code Playgroud)

python while-loop

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

两个 while 循环一次?

我正在用 Python 制作一个 IRC 机器人。while每次从 IRC 服务器接收到数据时,都会有一个循环重复。我想有另一个while每分钟运行一次的循环,所以我想不出组合这些循环的方法。

有没有办法“背景”一个循环并允许程序的其余部分在它“做它的事情”时继续运行?

python irc loops while-loop

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

是否可以使用 if 和 while 将所有控制流图转换回?

我想知道从单个方法(不允许递归)的典型JVM 字节码(参见如何)获得的所有控制流图是否都可以转换回等效的s 和s 代码。ifwhile

如果不是,则无法转换回ifs 和whiles的最小 JVM 字节码序列是多少?

jvm if-statement bytecode while-loop control-flow-graph

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

在修改后的 Wordpress 循环中插入 DIV 容器

我正在研究一个修改过的 wordpress 循环,其中有一个 if 和 else 条件。代码在下面。

我想要做的是添加一个容器,该容器将容纳满足条件的每个帖子。这将对每个条件进行分组。

http://pastebin.com/1R2jsWkY

<div class="container">

<?php if (have_posts()) : ?>

<?php
$i = 1;
while (have_posts()) {
    the_post();

// Add a DIV CONTAINER that holds the FIRST CONDITION
    if ($i <= 3) {
        the_title();
        the_content();
// Add a DIV CONTAINER that holds the FIRST CONDITION



// Add a DIV CONTAINER that holds the SECOND CONDITION
    } elseif ($i <= 9) {
        the_title();
        the_permalink();
// Add a DIV CONTAINER that holds the SECOND CONDITION


// …
Run Code Online (Sandbox Code Playgroud)

html php wordpress containers while-loop

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

jQuery .replaceWith 在循环中冻结

今天我正在编码,我尝试了一些while循环和 jQuery.replaceWith但循环从未停止,我不知道为什么。

这是我的 html 代码:

<div class="x"></div><div class="x"></div><div class="x"></div>
Run Code Online (Sandbox Code Playgroud)

这是我的javascript代码:

while($(".x").length !== 0) {
 $(".x:nth-child(1)").replaceWith('<div id="x"></div>');
}
Run Code Online (Sandbox Code Playgroud)

这段代码冻结了浏览器,但我不知道为什么。

我也试过这个:

while($(".x").length !== 0) {
 $(".x:nth-child(1)").remove();
}
Run Code Online (Sandbox Code Playgroud)

这有效。

我想要做的是带班替换所有元素.x与<div id="x"></div>

知道我的代码出了什么问题吗?

html javascript jquery while-loop

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

简单的文件 I/O 程序 C++

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <process.h>
using namespace std;

int main(){
    system("cls");
    char mline[75];
    int lc=0;
    ofstream fout("out.txt",ios::out);
    ifstream fin("data.txt",ios::in);
    if(!fin){
        cerr<<"Failed to open file !";
        exit(1);
    }
    while(1){
        fin.getline(mline,75,'.');
        if(fin.eof()){break;}
        lc++;
        fout<<lc<<". "<<mline<<"\n";
    }
    fin.close();
    fout.close();
    cout<<"Output "<<lc<<" records"<<endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

上面的代码应该从文件“data.txt”中读取以下文本

“ ifstream 类型流的默认行为(在打开文件时)允许用户从文件中读取内容。如果文件模式是 ios::in 则仅对文本文件执行读取,如果文件模式还包括 ios:: binary 和 ios::in 然后,读取是在二进制模式下执行的。在二进制模式下不会发生字符转换,而在文本模式下会发生特定的转换。”

并创建一个文件 out.txt ,其中使用行号存储相同的文本(一行可以有 75 个字符或以 '.' 结尾 - 以较早者为准)。

每当我运行该程序时,它就会卡在控制台上 - 按下任何键都不会响应。

有人能告诉我这里发生了什么吗?

c++ file-io while-loop c++11 codelite

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