从这个答案,我看到了这个:
unsigned long
hash(unsigned char *str)
{
unsigned long hash = 5381;
int c;
while (c = *str++)
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash;
}
Run Code Online (Sandbox Code Playgroud)
问题是我没有看到while停止执行的方式.一般来说,作业不是真的吗?我的意思if(a = 5)是真的.
编译器也给了我这个警告:
warning: suggest parentheses around assignment used as truth value [-Wparentheses]
Run Code Online (Sandbox Code Playgroud)
任何人都可以告诉我关于循环的确切情况吗?我强烈建议一个例子是这里最好的方法.
括号还应该在哪里使这个更清楚?
以下是我的PHP代码,它只是i =0在循环中给出我正在递增$i但它总是返回i as 0和while循环只工作一次,虽然我的查询SELECT * FROM events WHERE DATE(event_date) < CURDATE()在exectuing时返回7条记录phpmyadmin.让我知道我在这里做错了什么?
代码 -
<?php
include_once $_SERVER['DOCUMENT_ROOT'].'/app/'."config.php";
error_reporting(E_ALL);
if( $_POST['number'] == 'all' ) {
$eventArr = array();
$myarray = array();
$query = "SELECT * FROM events WHERE DATE(`event_date`) < CURDATE()";
$result = mysql_query($query);
$i =0;
while($row = mysql_fetch_assoc($result)) {
$eventArr[$i] = array('event_data'=> $row);
// Get image For an event
$event_id = $row['id'];
$query = "SELECT * FROM event_images WHERE event_id = $event_id …Run Code Online (Sandbox Code Playgroud) 我如何检查它是否是我while loop在C#中的第一次迭代?
while (myCondition)
{
if(first iteration)
{
//Do Somthin
}
//The rest of the codes
}
Run Code Online (Sandbox Code Playgroud) 我是C编程的新手.我写了一个简单的开关案例,但没有按预期执行.有人可以告诉我这里有什么问题吗?
#include <stdio.h>
int main() {
int i;
char yes;
bool flag = true;
while(flag) {
printf("Enter the value");
scanf("%d",&i);
switch(i) {
case 1:
printf("Hi");
break;
case 2:
printf("Hello");
break;
}
printf("Enter Y or N to continue");
scanf("%c",&yes);
if (yes == 'N') {
flag = false;
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我期待的结果是:
Enter the Value
1
Hi
Enter Y or N to continue
Y
Enter the Value
2
Hello
Enter Y or N to continue
N
Run Code Online (Sandbox Code Playgroud)
但我得到的结果是:
Enter the value …Run Code Online (Sandbox Code Playgroud) 我在Perl .pl文件中有以下代码。您是否认为此代码有任何问题(我不明白它的工作方式,因为第二行中有一个“ |”字符,而后面没有命令)
while ( $temp ne "" ) {
open( PS, "ps -ef | grep deploy.sh | grep ssh | grep -v grep|" );
$temp = <PS>;
close(PS);
print "The Deploy scripts are still running. Now sleeping 20\n";
sleep 20;
}
Run Code Online (Sandbox Code Playgroud) 我认为我的程序正在跳过我的while循环,但老实说我不确定究竟发生了什么.该函数应该通过找到GCD然后将分子和分母除以该数来减少分数.
class Rational {
private int numerator, denominator;
//Constructor
public Rational (int num, int den) {
numerator = num;
denominator = den;
}
//Method for multiplying fractions
public Rational times (Rational that) {
Rational x = new Rational (this.numerator*that.numerator, this.denominator*that.denominator);
x = x.reduce();
return x;
}
//Method for displaying fractions as strings
public String toString() {
return new String(numerator+"/"+denominator);
}
//Method for adding fractions
public Rational plus(Rational that) {
Rational x = new Rational ((this.numerator*that.denominator)+(that.numerator*this.denominator),
this.denominator*that.denominator);
//x = x.reduce();
return …Run Code Online (Sandbox Code Playgroud) 所以我正在使用do while循环显示菜单,如下所示,我希望用户通过菜单提示,直到他们进行有效选择 - 输入数字1,2,3或4.我想要使用switch case语句来识别用户选择并执行相关的代码块.但是,当涉及输入验证时,我如何考虑用户输入字母而不是数字?下面的代码成功地继续下一次迭代,以便在用户输入字母时重新启动用户,除了它进入连续循环.
int selection;
do{
cout << "Which option would you like to select? (select 1, 2, or 3)";
cout << "1: Option 1" << endl;
cout << "2: Option 2" << endl;
cout << "3: Option 2" << endl;
if(!(cin >> selection)){
cout << "Please select an integer from 1-4." << endl;
cin.clear()
}
}while(selection != (1) or (2) or (3) or (4));
Run Code Online (Sandbox Code Playgroud)
我尝试使用istringstream插入下面的代码,将用户响应从字符串流式传输到while循环中的int,作为尝试解决问题的替代方法,但无济于事.
string temp;
cin >> temp;
clearInputBuffer();
istringstream is(temp);
is >> selection;
Run Code Online (Sandbox Code Playgroud)
更新的代码 …
为什么在使用for和while循环时会得到不同的结果?
for (int i = 1; i < 100; i*= 2)
{
Console.Write(i);
}
//1248163264
Run Code Online (Sandbox Code Playgroud)
.
int i = 1;
while (i < 100)
{
i *= 2;
Console.Write(i);
}
//248163264128
Run Code Online (Sandbox Code Playgroud) 我是C的完全初学者,很抱歉,如果这个问题听起来太过微不足道了.
我的理解getchar(),并putchar()为他们处理文本流一次一个字符.假设我有这个程序从用户那里获取文本输入并将其显示在屏幕上:
#include <stdio.h>
main(){
int c;
c = getchar();
while(c!= EOF){
putchar(c);
c=getchar();
}
}
Run Code Online (Sandbox Code Playgroud)
这就是我的想法:
假设我运行这个程序,我输入单词Hi.getchar读取第一个字符(即H)并将其存储在中c.然后程序进入while循环并进入H屏幕.然后它获取下一个字符(i)并打印它.然后是EOF,当getchar将值赋给c时,while循环结束.因此,根据对所发生情况的这种解释,程序应在打印完所有字符并到达文件末尾后结束.
但是,当我运行程序时,在打印字符串后,程序等待接收额外的输入,这意味着它不会结束,而是等待用户输入更多的文本流.
为什么会发生这种情况?我在哪里弄错了?
void firstSentence(void){
string incorrectSentence;
string correctSentence = "I have bought a new car";
cout << "Your sentence is: I have buy a new car" << endl;
cout << "Try to correct it: ";
cin >> incorrectSentence;
if(incorrectSentence == correctSentence){
cout << "Goosh. Your great. You've done it perfectly.";
}
else{
firstSentence();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我试图在我的程序中调用的函数.但是我很少生气,因为我无法找到自己的解决方案.它的作用是,如果"if语句"中的条件为真,我的输出就不是我的预期.输出重复5次"尝试纠正它.你的判决是:我买了一辆新车..
为什么它重复5次,无论如何,那里发生了什么以及它为什么不起作用?