标签: while-loop

如何从while循环返回2个布尔值?Java的

我尝试返回correctPiece和correctDest但是

return correctPiece;
Run Code Online (Sandbox Code Playgroud)

加下划线并出现错误"无法访问的代码".

我怎么能两个都回来?

   while(correctPiece && !correctDest) {

            System.out.println("Click on a destination");

            toXCo = s.getToXInt();
            toYCo = s.getToYInt();

            Move found = null;

            for( Move m : moves){
                //checks if move can be done
                if (m.ToX() == toXCo && m.ToY() == toYCo){
                    //if move is allowed- exit loop
                    found = m; 
                    correctDest = true;
                }
            }

            if (found == null) {
                //if move can't be, ask for new co-ordinates
                System.out.println("This move is not legal \n");
                    correctDest = false;
                    correctPiece = …
Run Code Online (Sandbox Code Playgroud)

java boolean return while-loop

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

如何运行多个while循环,在每次循环后关闭?

我对Python(以及一般的编程)相当陌生,我想知道是否有办法在同一时间运行2个或更多while循环,以便在每个循环的每一轮之后切换到另一个循环,而不必重新开始在零回合.这是我到目前为止所拥有的:

def loop1():
rounds = 0
while True:
    if rounds == 1:
        print('A')
    elif rounds == 2:
        print('B')
    else:
        rounds = 0
    rounds = rounds + 1

def loop2():
    rounds = 0
    while True:
        if rounds == 1:
            print('a')
        elif rounds == 2:
            print('b')
        else:
            rounds = 0
        rounds = rounds + 1

loop1()
loop2()
Run Code Online (Sandbox Code Playgroud)

现在,当我这样做时,它所做的就是无限地运行loop1()并且永远不会进入loop2(),但是我想知道一种方法来获得输出:

A
a
B
b
A
a
B
b
A
a
... and so on. 
Run Code Online (Sandbox Code Playgroud)

谁知道我怎么做到这一点?

另请注意,我是初学者,并没有真正涉及所有的编程术语,所以请使用小词:)提前谢谢.(我也在使用Python 3(虽然我认为你可能已经知道了吗?))

python loops infinite-loop while-loop

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

虽然遇到NULL字符后C中的循环不会中断

这是添加字母数字字符串中的数字的代码:

#include<stdio.h>
#include<stdlib.h>
int main()
{
int total=0;
char ch;
printf("enter the string\n");
ch=getchar();
while(ch!='\0')
{
    printf("I am here !!");
    if (!(isalpha(ch)))
        total+=(int)ch;
    ch=(char)getchar();
    printf("I am here !!");
}
printf("\ntotal is %d",total);
return 0;
}
Run Code Online (Sandbox Code Playgroud)

无论我输入什么字符,它都会为每个字符提供4个"我在这里".

我试着用

while((ch=getchar())!='\0');
Run Code Online (Sandbox Code Playgroud)

但它给出了同样的问题.

c while-loop

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

如何在表中显示mysql多行/ mysql_fetch_array结果?

我试图mysql_fetch_array()在表格中显示结果.

我想表明guests name,their country以及他们agreed time在同一个日期旅行的人.

以下代码工作正常.代码获取行值并将其打印出来.

 $select_guests = mysql_query('SELECT name FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error()); // query for getting guests for the same date
while($row = mysql_fetch_array($select_guests, MYSQL_ASSOC)) { //visitor / guest loop starts here
    echo $row['name'].'<br/>';
}
$select_country = mysql_query('SELECT country FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error()); // query for getting guests for the same date
while($row = mysql_fetch_array($select_country, MYSQL_ASSOC)) { //country of visitor …
Run Code Online (Sandbox Code Playgroud)

php mysql arrays html-table while-loop

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

让我的程序回到"顶部"(if语句)(Java)

我有一个计算闰年的程序,但它只能在1750年左右开始工作,所以我希望我的用户只能在1750年之后输入数字.

System.out.println("Enter a year after 1750:");
    leapYear = in.nextInt();

    if(leapYear<1750){
        System.out.println("You have entered a year before 1750, please try again :");
        leapYear  = in.nextInt();
    }
Run Code Online (Sandbox Code Playgroud)

我的解决方案是提出一个if语句.然而,我意识到,只有当用户输入一次低于1750的东西时,这才有效.如果他们再次这样做,程序将继续.有没有办法可以执行声明"你已经在1750年之前进入了一年,请再试一次:"我需要多次而不重写它?

我想也许一个while语句可以工作(如果一个while语句工作怎么能这样做而不会导致无限循环)?

java if-statement while-loop leap-year

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

如何从主循环(C编程)中逃脱?

如果按下外部停止键,我试图逃离主循环.目前,通过RS485通信将AT32UC与ATmega128通信,其中实现了START和STOP键.如果在接收器侧有要处理的数据,则调用RS485接收器中断,其中0x10 =开始,0x11 =停止.

我的问题是启动和停止键被很好地识别并且如果启动则继续主循环,并且如果按下停止键我想终止主循环.所以我相应地设置了开始标志和停止标志.但是,我正在努力停止(逃避)实施.下面是中断例程和主循环的简要片段.

__attribute__((__interrupt__)) static void rs485RxInterrupt(void)
{
uint32_t data;
static char RxDatalength = 98;
data = AVR32_USART2.RHR.rxchr;

    if(data & 0x00000100) // rs485 9 bit check 
    {
        if((data & 0x000000ff) == 0x92) //dsp board address = 0x92
        {
            rxBuf[0] = data;
            addr_flag = true;
            rxInCtr = 1;
        }
        else
        {
            addr_flag = false;
            return;
        }
     }

     else if (addr_flag == true) // if 9 bit is checked
     {
         rxBuf[rxInCtr++] = data;
         if(rxInCtr == 2)           // command check
         {
            if(data …
Run Code Online (Sandbox Code Playgroud)

c embedded interrupt while-loop rs485

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

编译器涉嫌优化while循环

在嵌入式编程讲座的这一部分中,Samek博士解释了编译器如何能够实现"原始代码"的效率(可能是由于这意味着由语法的逻辑顺序决定的实现):

我希望你注意到反汇编代码实现了与我为while循环描述的不同的控制流.原始代码应该首先测试条件,然后如果条件不正确则跳过循环体.编译后的代码以无条件分支开始,并反转循环体的顺序和条件的测试.但是,当你考虑它时,那两个控制流是等价的,除了生成的控制流更快,因为它在循环的底部只有一个条件分支.

在他的解释中,他参考了所示的两个流程图.描述编译代码的图表说明了"它在循环底部只有一个条件分支".我似乎无法看到它在实践中是如何工作的:

  • 这是我拍摄的屏幕截图,展示了如何实现原始代码.(初始)机器指令的顺序:

0x2815 CMP- > 0x1c40 ADDS- >repeat until condition is false

  • 这是另一个屏幕截图,显示了编译器使用的序列.(初始)机器指令的顺序:

0x2815 CMP- > 0xdbfc BLT.N- > 0x1c40 ADDS- >repeat until condition is false

我可以理解两种控制流是如何相等的(至少从图中看是如此),但肯定不是生成的(右边)如何更快.首先,两个图表最开始的箭头导致了相同的分支.其次,尽管分支确实似乎在右侧的原理图中发生,正如您从屏幕截图中看到的那样,模拟器似乎表明两种方法都是从与比较(0x2815 CMP)相关的机器指令开始的.

我如何协调流程图与我在实践中看到的内容?

c performance assembly while-loop

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

无限循环stackoverflow对象列表异常

我有这个代码:

public List<CsvUserData> CsvUserList = new List<CsvUserData>();

public CsvUserData()
{
    readCSV(@"C:\userdata.csv");
}

public string CSVEmailEditText { get; set; }
public string CSVNameEditText { get; set; }
public string CSVAddressEditText { get; set; }
public string CSVPostnumEditText { get; set; }
public string CSVCityEditText { get; set; }
public string CSVPhoneEditText { get; set; }
public string CSVCommentEditText { get; set; }
public string SelectPage { get; set; }

private void readCSV(string location)
{
    var reader = new StreamReader(File.OpenRead(location));
    string line; …
Run Code Online (Sandbox Code Playgroud)

c# csv list while-loop

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

基本的While循环查询 - 为什么记录"1"?

在此输入图像描述

为什么这个while循环在末尾打印"1"?我只想打印console.log语句.在使用Codecademy时看到了.

for (i = 0; i < 2; i++) {
    console.log("I understand for loops twice..lol");
};

var whileUnderstand = 0;

while(whileUnderstand < 2) {
    console.log("I understand while loops twice..lol");
    whileUnderstand++;
}  
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

这个问题没有直接回答我提出的问题.此外,它只包含console.log语句而不是循环.主要是,没有答案说"控制台只是输出语句的最后评估值." 这是解决我的问题的答案.

javascript loops for-loop while-loop

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

在bash中遍历文件时保留空白

我需要遍历bash中文本文件的所有行,并且我想保留空白。最终目标是提供可能包含空格的行词。

因此,给定一个包含

xdebug
var_dump
 dump(
pwet
meuh
coin
Run Code Online (Sandbox Code Playgroud)

当我尝试这个:

while read -r PHRASE
do
    echo "$PHRASE"
done < phrase_file
Run Code Online (Sandbox Code Playgroud)

输出为:

xdebug
var_dump
dump(
pwet
meuh
coin
Run Code Online (Sandbox Code Playgroud)

空格在dump()去之前在哪里,如何将其取回?

bash whitespace loops while-loop

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