#8756: 兩光法師占卜術 得到的結果不對


editorer (editorer)


 底下是我的程式碼

我設計月份只能輸入1~12,日期只能輸入1~31

但是我最後都是得到 "吉"

是不是哪裡出錯了

 #include <iostream>

using namespace std;

int M;
int D;
int S;

int main() {
    while ( M<1 || M>12) {
        cin >> M;
    }
    
    while ( D<1 || D>31) {
        cin >> D;
    }
    
    S = (M*2+D)%3 ;
    
    if ( S = 0 ) {
        cout << "普通" << endl;
    }
    
    else if ( S = 1 ) {
        cout << "吉" << endl;
    }
    
    else {
        cout << "大吉" << endl;
    }
    
    return 0;
}

#8758: Re:兩光法師占卜術 得到的結果不對


isu10103068A (暖暖兒)


 底下是我的程式碼

我設計月份只能輸入1~12,日期只能輸入1~31

但是我最後都是得到 "吉"

是不是哪裡出錯了

 #include

using namespace std;

int M;
int D;
int S;

int main() {
    while ( M<1 || M>12) {
        cin >> M;
    }
    
    while ( D<1 || D>31) {
        cin >> D;
    }
    
    S = (M*2+D)%3 ;
    
    if ( S = 0 ) {
        cout << "普通" << endl;
    }
    
    else if ( S = 1 ) {
        cout << "吉" << endl;
    }
    
    else {
        cout << "大吉" << endl;
    }
    
    return 0;
}

一開始沒有設定初始值 能判斷while嗎@@?

#8760: Re:兩光法師占卜術 得到的結果不對


editorer (editorer)


 底下是我的程式碼

我設計月份只能輸入1~12,日期只能輸入1~31

但是我最後都是得到 "吉"

是不是哪裡出錯了

 #include

using namespace std;

int M;
int D;
int S;

int main() {
    while ( M<1 || M>12) {
        cin >> M;
    }
    
    while ( D<1 || D>31) {
        cin >> D;
    }
    
    S = (M*2+D)%3 ;
    
    if ( S = 0 ) {
        cout << "普通" << endl;
    }
    
    else if ( S = 1 ) {
        cout << "吉" << endl;
    }
    
    else {
        cout << "大吉" << endl;
    }
    
    return 0;
}

*********************************

問題已經解決。

    if ( S == 0 ) {
        cout << "普通" << endl;
    }
    
    else if ( S == 1 ) {
        cout << "吉" << endl;
    } 

原來是忘了必須用 "==" 而不是 "="