iT邦幫忙

1

C# 新手剛入學~請問&&如何與 if else 搭配使用? ~~~~感謝各位~~

  • 分享至 

  • twitterImage

假設題目:男生身高超過170公分,女生身高超過160公分,即入選。

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
1
海綿寶寶
iT邦大神 1 級 ‧ 2021-08-22 14:23:30

https://ithelp.ithome.com.tw/upload/images/20210822/20001787iYgd3HfJgA.png

using System.IO;
using System;

class Program
{
    static void Main()
    {
        String sGender="male";
        int iHeight = 180;
        if (((sGender=="male")&&(iHeight>170))||
            ((sGender=="female")&&(iHeight>160))) {
            Console.WriteLine("Qualified.");    
        } else {
            Console.WriteLine("Rejected.");    
        }
        
    }
}
annajhan iT邦新手 5 級 ‧ 2021-08-22 14:28:24 檢舉

感謝及時救援!! 謝謝大大!

通靈王....還是
你就是出題的老師啊.

/images/emoticon/emoticon72.gif

0
石頭
iT邦高手 1 級 ‧ 2021-08-22 14:26:29

你應該是想要找||而不&&

因為一般來說不會有人是男生又是女生...

if(男生.身高>170 || 女生.身高>160)
{
//...
}

這個問題是很基本的程式判斷,建議你把MSDN C # 運算子和運算式 先看完

annajhan iT邦新手 5 級 ‧ 2021-08-22 14:29:56 檢舉

好的~感謝石頭的即時回應^^

0
japhenchen
iT邦超人 1 級 ‧ 2021-08-23 11:23:20

合併 || 跟 && 做單串過濾

if( (學生.性別=="男" && 學生.身高>170) || (學生.性別=="女" && 學生.身高>160))
{
    MessageBox.Show("你入選了");
}
annajhan iT邦新手 5 級 ‧ 2021-08-23 19:17:10 檢舉

感謝大大的幫忙!

我要發表回答

立即登入回答