作者zzhiphop6 (老頭)
看板C_and_CPP
標題[問題] while迴圈的問題
時間Thu Feb 26 13:40:44 2015
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
dev c++
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
問題(Question):
問題出現在這段程式碼
quardilateral = 1;
while(quardilateral = 0) {
預期的正確結果(Expected Output):
quardilateral = 1;
while(quardilateral = 0) {
while不執行
quardilateral = 0;
while(quardilateral = 0) {
while執行
quardilateral = 1;
while(quardilateral = 1) {
while執行
quardilateral = 0;
while(quardilateral = 1) {
while不執行
錯誤結果(Wrong Output):
實際結果卻是
quardilateral = 1;
while(quardilateral = 0) {
while不執行
quardilateral = 0;
while(quardilateral = 0) {
while不執行
quardilateral = 1;
while(quardilateral = 1) {
while執行
quardilateral = 0;
while(quardilateral = 1) {
while執行
程式碼(Code):(請善用置底文網頁, 記得排版)
// 引入標準程式庫中相關的輸入、輸出程式
#include <iostream>
// 引入標準程式庫中相關的字串程式
#include <string>
#include <math.h>
//std 為標準程式庫的命名空間
using namespace std;
int main(void) {
double a;
double b;
double c;
double d;
printf("please enter the fixed edge of a quardrilateral\n");
cin >>a;
printf("please enter others edge of quardrilateral\n");
cin >>b;
cin >>c;
cin >>d;
double sita;
sita=0;
double diagonal;
diagonal=pow(pow(a,2)+pow(b,2)-2*a*b*cos(sita),0.5);
int quardilateral ;
quardilateral = 1;
while(quardilateral = 0) {
if (c+d>diagonal,c+diagonal>d,d+diagonal>c){
quardilateral = 1;
sita=sita+0.01;
cout << sita << endl;
}
else{
quardilateral = 0;
cout << quardilateral << endl;
}
}
system("pause");
return 0;
}
補充說明(Supplement):
while執行與否完全無規則
有時候判斷true執行
有時候判斷false執行
怎麼會這樣
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.249.92.60
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1424929246.A.75C.html
→ diabloevagto: =跟==是不同的 02/26 13:49
→ a771116123: 樓上突破盲點啦XD 我還看半天 02/26 13:50
→ zzhiphop6: 哪尼!!!!!!!!!!!! 02/26 13:53
→ petercoin: ==才是真的等於 =是賦值 02/26 14:30
→ zzhiphop6: 另外再請問 我如果要判斷三個條件都要滿足 02/26 15:32
→ zzhiphop6: 只要一個不滿足就執行else 這樣要怎麼寫呢 02/26 15:33
→ bibo9901: 用 && 把條件串起來 02/26 15:39
推 a27417332: 恩...這問題我認為應該是不需要版友的關鍵字就是了@@ 02/26 16:13
→ a27417332: 阿不小心按到推囧 02/26 16:14
→ bigpigbigpig: 這是初學者常犯的錯誤,混淆了 = 和 == 運算子 02/26 20:42
→ PkmX: -Wall -Wextra -pedantic不用想直接開下去就是了 02/26 22:29
→ MOONRAKER: 有的人會寫 while (1==expression) 這樣誤寫成=也會錯 02/26 23:36
→ loveme00835: 第一個想到是 <iso646.h> 只可惜沒 eq.. xD 02/27 00:04
→ s1000: == 02/27 10:54
→ MasTerNBHD: 第一頁就看到 "=" 是問題了... 02/27 13:49
→ softseaweed: 這... 02/27 18:42
→ MashiroKinji: 如果是VB轉過來的常常會做這種事情..我就是.... 02/28 05:29
推 sss22213: == 才是比較運算子 03/04 12:49