我想有一个while循环做类似下面的事情,但这可能在c ++?如果是这样,语法如何?
do {
//some code
while( expression to be evaluated );
// some more code
}
我希望一旦while语句决定表达式不再为真,就会退出循环(即如果表达式为false,则表示不执行某些代码)
我正在尝试为编程分配格式化以空格分隔的用户输入.
本质上,输入由任意数量的表达式组成
L integer integer integer integer和C integer integer integer.
例如:L 1 1 5 7 C 4 5 3.
到目前为止,我已经设法根据初始字符提取整数,并可以使用scanf函数迭代字符串:
char a;
while(scanf("%c", &a) == 1){
if(a == 'C'){
int inputX, inputY, inputR;
scanf("%d %d %d", &inputX, &inputY, &inputR);
printf("%d %d %d\n", inputX, inputY, inputR);
}
else if(a == 'L'){
int x1, y1, x2, y2;
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
printf("%d %d %d %d\n", x1, y1, x2, y2);
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,虽然这会输出所需的整数,但循环(和用户输入提示)不会终止.
有人可以告诉我为什么会这样吗?
我正在看一些C代码,我找到了这一行.
if (temp==NULL)
while (1) ;
Run Code Online (Sandbox Code Playgroud)
根据我的理解,当你进入一个无限循环时,除非你打破,否则没有出局,这是如何工作的?如果if语句不为NULL,它是否会中断,如果是这样,是什么让它一遍又一遍地检查if语句?
有关更多信息,请查找realboy源代码,该文件为gboy_lcd.c
第304行
https://github.com/guilleiguaran/realboy/blob/ed30dee751c3f78964e71930a8f87d2074362b9b/gboy_lcd.c
对于linux来说,它是一个非常稳定和优秀的gameboy模拟器
我正在编写一个程序,它从键盘输入并以下面的螺旋方式打印方形矩阵
1 2 3
8 9 4
7 6 5
Run Code Online (Sandbox Code Playgroud)
我设法编写程序,但我遇到了一个奇怪的错误.在第26行,它给了我一个超出范围的索引异常
while (matrix[row, col] == 0 && col < matrix.GetLength(0) )
Run Code Online (Sandbox Code Playgroud)
但是,如果我在循环中切换两个语句的顺序,异常就消失了?这是否意味着while循环中两个语句的顺序很重要?如果是,为什么?如果两个语句都是真的执行循环,并且如果其中一个是假的,无论是哪一个,都应该停止执行它.
这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SpiralMatrixN
{
class Program
{
static void Main(string[] args)
{
//prompt the user to enter n
Console.WriteLine("Enter the value of n");
int n = int.Parse(Console.ReadLine());
int[,] matrix = new int[n,n];
Console.Clear();
System.Console.SetWindowSize(100, 30);
int value = 1;
int col = 0;
int row = 0;
if …Run Code Online (Sandbox Code Playgroud) 最近我遇到了这个问题
int i = 10;
while (i++ <= 10) {
i++;
}
System.out.print(i);
Run Code Online (Sandbox Code Playgroud)
答案是13,有些人可以解释一下它是13吗?
我是新手,对 Python 了解不多。有人知道如何在 while 循环中编写阶乘吗?
我可以在 if / elif else 语句中实现:
num = ...
factorial = 1
if num < 0:
print("must be positive")
elif num == 0:
print("factorial = 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print(num, factorial)
Run Code Online (Sandbox Code Playgroud)
但我想用一个 while 循环(无功能)来做到这一点。
当它从 JMeter 中的 CSV 读取 EOF 时,我想做“while”控制器转到下一个控制器。while 循环停止,但它又迭代了 1 次,我的意思是说它在检测到 EOF 时不需要迭代。当我将 EOF 值上的停止线程选择为“True”时。while 循环停止并且不进行下一个循环。
当 CSV 文件检测到 EOF 而不重复一个循环时,我需要转到下一个循环控制器。
提前致谢。
我正在尝试报告使用 grep 和 while 找到的行。
我知道您可以使用以下内容来比较来自 input.txt 的字符串列表,并在您的目标文件中找到它们,如下所示:
grep -f inputs.txt file_to_check
Run Code Online (Sandbox Code Playgroud)
我想要的是读取输入字符串的每一行并在循环中单独grep它们。
所以我尝试了以下方法:
cat inputs.txt | while read line; do if grep "$line" filename_to_check; then echo "found"; else echo "not found"; fi; done
Run Code Online (Sandbox Code Playgroud)
当我将输出重定向到文件时,这不会返回任何内容。
while read line
do
if grep "$line" file_to_check
then echo "found"
else
echo "not found"
fi
done < inputs.txt
Run Code Online (Sandbox Code Playgroud)
与第一个相同,但从我发现的更好。
我知道它会逐行迭代,因为我可以用 echo $line 替换 grep 并打印每一行;但任一方法都不会返回类似 grep -f 的内容,而是显示:
not found
not found
not found
.
. etc.
Run Code Online (Sandbox Code Playgroud)
所以我正在寻找的是它会遍历每一行并使用 if 语句通过 grep 检查它以确定 grep 是否真的找到它的东西。我知道我可能没有所有正确的逻辑,但我想要的输出应该是这样的: …
我在网上的 bash 脚本教程中遇到了以下 while 循环:
while(($#)) ; do
#...
shift
done
Run Code Online (Sandbox Code Playgroud)
我不明白在 while 循环中使用位置参数基数。我知道该shift命令的作用,但是 while 语句与shift?
我正在做一些阅读文件的练习,我的任务是为不平衡文件编写File Interleave代码.所以如果我有这样设置的东西它工作正常:
while ((inputLine1 = LineSequential.read(inFileStreamName1)) != null **&&** (inputLine2 = LineSequential.read(inFileStreamName2)) != null)
Run Code Online (Sandbox Code Playgroud)
注意&& ...但是如果我将它改为|| netbeans声明inputLine2尚未初始化(尽管它以这种方式看到inputLine1非常精细):
while ((inputLine1 = LineSequential.read(inFileStreamName1)) != null **||** (inputLine2 = LineSequential.read(inFileStreamName2)) != null)
Run Code Online (Sandbox Code Playgroud)
现在,如果inputLine1(这是来自文件A的数据)首先变为空,我只得到inputLine2中的每一行(来自文件B).在每次循环迭代中,它跳过一条线.我很确定它与我初始化inputLine2的位置有关..:
while ((inputLine1 = LineSequential.read(inFileStreamName1)) != null || LineSequential.read(inFileStreamName2) != null) {
inputLine2 = LineSequential.read(inFileStreamName2);
System.out.println(inputLine2);
if(inputLine1 != null && inputLine2 == null){
LineSequential.write(outFileStreamName, inputLine1);
}else if(inputLine1 == null && inputLine2 != null){
LineSequential.write(outFileStreamName, inputLine2);
}
else if(inputLine1 != null && inputLine2 != null){
LineSequential.write(outFileStreamName, inputLine1);
LineSequential.write(outFileStreamName, inputLine2);
}
}
Run Code Online (Sandbox Code Playgroud)