[問題] while迴圈的問題

作者: zzhiphop6 (老頭)   2015-02-26 13:40:44
開發平台(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執行
怎麼會這樣
作者: diabloevagto (wi)   2015-02-26 13:49:00
=跟==是不同的
作者: a771116123 (Kerwin)   2015-02-26 13:50:00
樓上突破盲點啦XD 我還看半天
作者: zzhiphop6 (老頭)   2015-02-26 13:53:00
哪尼!!!!!!!!!!!!
作者: petercoin (彼得幣)   2015-02-26 14:30:00
==才是真的等於 =是賦值
作者: zzhiphop6 (老頭)   2015-02-26 15:32:00
另外再請問 我如果要判斷三個條件都要滿足只要一個不滿足就執行else 這樣要怎麼寫呢
作者: bibo9901 (function(){})()   2015-02-26 15:39:00
用 && 把條件串起來
作者: a27417332 (等號卡比)   2015-02-26 16:13:00
恩...這問題我認為應該是不需要版友的關鍵字就是了@@阿不小心按到推囧
作者: bigpigbigpig (To littlepig with love)   2015-02-26 20:42:00
這是初學者常犯的錯誤,混淆了 = 和 == 運算子
作者: PkmX (阿貓)   2015-02-26 22:29:00
-Wall -Wextra -pedantic不用想直接開下去就是了
作者: MOONRAKER (㊣牛鶴鰻毛人)   2015-02-26 23:36:00
有的人會寫 while (1==expression) 這樣誤寫成=也會錯
作者: loveme00835 (髮箍)   2015-02-27 00:04:00
第一個想到是 <iso646.h> 只可惜沒 eq.. xD
作者: s1000 (s1000)   2015-02-27 10:54:00
==
作者: MasTerNBHD (NBHD)   2015-02-27 13:49:00
第一頁就看到 "=" 是問題了...
作者: softseaweed (Gladys von Wackenheim)   2015-02-27 18:42:00
這...
作者: MashiroKinji (MashiroKinji)   2015-02-28 05:29:00
如果是VB轉過來的常常會做這種事情..我就是....
作者: sss22213 (sss22213)   2015-03-04 12:49:00
== 才是比較運算子

Links booklink

Contact Us: admin [ a t ] ucptt.com