标签: while-loop

cin >> 整数和 while 循环

使用下面的代码,

如果我输入一个字母或一个很长的数字,while 循环就会变得混乱,这是为什么呢?

void main()
{
    int n{ 0 };

    while (true)
    {
        cout << "Enter a number: ";
        cin >> n;
        cout << n << endl;
    }
}
Run Code Online (Sandbox Code Playgroud)

cin while-loop c++11

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

如何从 try/catch 内部中断循环?

我正在为学校做一个简单的课程项目,以试验 C# 中的继承和其他要点,但我在某些部分遇到了问题。我相信我需要在无条件 while 循环中尝试捕获,以确保用户以正确的形式输入数据,但我还需要能够从错误处理代码中跳出循环。我在给我带来问题的代码下面添加了注释。

class Program : students
{
    static void Main(string[] args)
    {
        students stu = new students();

        Console.Write("Number of students are you recording results for: ");
        int studNum = int.Parse(Console.ReadLine());
        stu.setNoOfStudents(studNum);
        Console.WriteLine();

        for (int a = 0; a < studNum; a++)
        {
            Console.Write("{0}. Forename: ", a + 1);
            stu.setForname(Console.ReadLine());
            Console.Write("{0}. Surname: ", a + 1);
            stu.setSurname(Console.ReadLine());
            while (0 == 0)
            {
                try
                {
                    Console.Write("{0}. Age: ", a + 1);
                    stu.setstudentAge(int.Parse(Console.ReadLine()));
                    Console.Write("{0}. Percentage: ", a + 1);
                    stu.setpercentageMark(int.Parse(Console.ReadLine())); …
Run Code Online (Sandbox Code Playgroud)

c# try-catch break while-loop

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

检查 Python While 循环中的值是否仍然保持不变

我想知道是否有一种优雅的方法来查看是否可以检查在 while 循环中不断变化的值,并在该值停止变化并保持不变时停止 while 循环。

例如:

Value = 0
while True:
    value changes everytime
    (if value still the same break)
Run Code Online (Sandbox Code Playgroud)

python loops imperative-programming while-loop python-2.7

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

php带有while循环的多个curl url

我正在开发一个小脚本,但使用几个curl 和while 循环有点搞砸了。

我想在其中一个 URL 向我提供信息时停止处理curl。注意:我有多个卷曲请求。

所以我的理念是,

我有几个 URL,我必须对其进行处理并从中获取信息。如果在特定 URL 上找到信息,它将给我一个字符串。如果找不到信息,它就不会给我任何价值。所以我大约有近 10 个 URL 需要处理。在所有情况下,任何一个 URL 都会向我提供信息,因此其余 URL 将不会产生任何价值。由于要处理大量 URL,因此延迟是一个问题。因此,假设在下面的示例代码中,如果以 value2.php 结尾的 url 给了我一个结果,那么我立即想停止处理其他 URL。因为我已经得到了结果,没有必要运行其他卷曲。最后我必须打印结果。

另外,我遇​​到的情况是,所有 URL 都不会产生任何结果,如果有人向我展示如何处理该问题,那就太好了。

我的示例代码。

<?php
///functions here
do {

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"example[dot]com/value1.php?process=$param");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$combined = curl_exec ($ch);
curl_close ($ch);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"example[dot]com/value2.php?process=$param");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$combined = curl_exec ($ch);
curl_close ($ch);


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"example[dot]com/value3.php?process=$param");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$combined = curl_exec ($ch);
curl_close ($ch);


$ch = curl_init();
curl_setopt($ch, …
Run Code Online (Sandbox Code Playgroud)

php url curl while-loop do-while

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

Bash while loop stops after first iteration when subshell is called

This contrived bash script demonstrates the issue.

#!/bin/bash
while read -r node ; do
    echo checking $node for Agent;
       PID=$(ssh $node ""ps -edf | grep [j]ava | awk '{print $2}'"")
       echo $PID got to here.
done < ~/agents_master.list
Run Code Online (Sandbox Code Playgroud)

agents_master.list contains 1 server per line:

server1
server2
server3
Run Code Online (Sandbox Code Playgroud)

Which only outputs the following:

checking server1 for Agent
Authorized use only
25176 got to here
Run Code Online (Sandbox Code Playgroud)

Server 2 and 3 aren't even echoed out to screen by the line echo checking $node...

If …

bash shell while-loop subshell

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

R 删除数据帧的第一行,直到第一行没有 NA

我正在数据框上应用 na.approx,如果 NA 恰好位于数据库的第一行或最后一行,则该方法将不起作用。

如何编写执行以下操作的函数:“当数据帧第一行的任何值为 NA 时,删除第一行”

数据框示例:

x1=x2=c(1,2,3,4,5,6,7,8,9,10,11,12)
x3=x4=c(NA,NA,3,4,5,6,NA,NA,NA,NA,11,12)
df=data.frame(x1,x2,x3,x4)
Run Code Online (Sandbox Code Playgroud)

此示例数据框的结果应如下所示:

result=df[-1:-2,]
Run Code Online (Sandbox Code Playgroud)

我当前的尝试看起来都类似于:

replace_na=function(df){
while(anyNA(df[1,])=TRUE){
  df=df[-1,],
  return(df)
}  
#this is where I would apply the na.approx function to the data frame
}
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激,谢谢!

r function while-loop dataframe na

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

如何检查用户是否在scanf中输入空行

如果用户按 Enter 键而不输入任何浮点值,我需要程序退出 while 循环。

printf("Enter scores\n");

float scores[10];
int n=0;    

while (n < 10){
    scanf("%f", &scores[n]);
    n++;
    if (THE USER PRESSES ENTER WITHOUT ENTERING ANYTHING)
        break;
}
Run Code Online (Sandbox Code Playgroud)

c scanf while-loop

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

java中如何使用循环超时?

我制作的这段代码。

public long detecFace(int threshold, int dir, JLabel... lbl) throws InterruptedException {

    long timeStart = Calendar.getInstance().getTimeInMillis();         
    BufferedImage[] finalImg = new BufferedImage[10];
    BufferedImage[] edgeImage = new BufferedImage[10];

    long t = System.currentTimeMillis();
    long end = t-300000;
    while (t < end){
        for (int i = 0; i < 5; i++) {
            ip.setFileImage(df.getFile()[i]);
            ip.procesImage(threshold, dir);
            edgeImage[i] = ip.getEdgeImage();
            //ImageDrawer.drawOriginalImage(edgeImage[i]); //BENER HASIL YANG KELUAR
            finalImg[i] = showSelectedFace(RHT(edgeImage[i]), ResearchData.getOriginalImage()[i]);
            //ImageDrawer.drawOriginalImage(finalImg[i]); //SALAH HASIL YANG KELUAR
            int count = 1;
            int total = count + i;
            System.out.println(total);

            if …
Run Code Online (Sandbox Code Playgroud)

java multithreading loops timeout while-loop

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

具有多个变量的 T-SQL SELECT

我尝试使用 SQL 语句和 NORTHWIND 数据库获取此输出:

Employee Name:Nancy Davolio
Number of Sales:345
Total Sales:192107.60

Employee Name:Andrew Fuller
Number of Sales:241
Total Sales:166537.75

Employee Name:Janet Leverling
Number of Sales:321
Total Sales:202812.84

Employee Name:Margaret Peacock
Number of Sales:420
Total Sales:232890.85

Employee Name:Steven Buchanan
Number of Sales:117
Total Sales:68792.28
Run Code Online (Sandbox Code Playgroud)

...还有 4 个条目

当我使用这个语句时:

USE Northwind

DECLARE @EmployeeName VARCHAR(40),
        @NumberOfSales INT,
        @TotalSales DECIMAL(10,2),
        @Counter TINYINT = 1,
        @NumEmployees INT = IDENT_CURRENT('dbo.Employees');

WHILE @Counter < @NumEmployees
BEGIN
    --SELECT @EmployeeName = E.FirstName+' '+E.LastName
    --SELECT @NumberOfSales = count(od.OrderID) …
Run Code Online (Sandbox Code Playgroud)

sql t-sql sql-server select while-loop

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

20 秒后退出 while 循环

如何在 20 秒过去后退出 while 循环?我想避免使用线程。

while (var) {
    // ...do something

    if (20secondsIsPassed) {
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)

java time jvm if-statement while-loop

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