标题:这段代码有什么问题
只看楼主
urick
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2012-12-6
结帖率:75%
已结贴  问题点数:20 回复次数:5 
这段代码有什么问题
问题::在执行了button1_Click,在满足if的条件下,richTextBox4无输出。请大家指点下,谢谢!

using System;
using System.Collections.Generic;
.....
namespace InfoSearch
{
    public partial class Form1 : Form
    {
           public Form1()
        {
            InitializeComponent();
         
        }
    private void button1_Click(object sender, EventArgs e)        
       {
         
            char[] Name = richTextBox1.Text.ToCharArray();
            string a = Name[0].ToString();
            string b = richTextBox2.Text.Trim();
            string c = richTextBox3.Text.Trim();
            string r1 = "";
            string r2 = "";
            richTextBox4.Text = r1.ToString() + r2.ToString();//输出if的结果

            if ((a == "北" || a == "上") && (b == "地铁" || b == "公交"))
            {
                r1 = "拥堵";
            }
            else
                r1 = "";
            if ((a == "成" || a == "西") && (b == "铁路" || b == "飞机"))
            {
                r1 = "少量";
            }
            else
                r1 = "";
           if ((c == "游客" || c == "背包客") && (b == "铁路" || b == "飞机"))
            {
                r2 = "增加中";
            }
            else
                r2 = "";

           }
    }
}
搜索更多相关主题的帖子: void private public 
2012-12-09 13:36
hjywyj
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:3
帖 子:1114
专家分:2611
注 册:2010-4-14
得分:7 
   private void button1_Click(object sender, EventArgs e)        
        {
           
            char[] Name = richTextBox1.Text.ToCharArray();
             string a = Name[0].ToString();
             string b = richTextBox2.Text.Trim();
             string c = richTextBox3.Text.Trim();
             string r1 = "";
             string r2 = "";
            
            if ((a == "北" || a == "上") && (b == "地铁" || b == "公交"))
             {
                 r1 = "拥堵";
             }
             else
                 r1 = "";
             if ((a == "成" || a == "西") && (b == "铁路" || b == "飞机"))
             {
                 r1 = "少量";
             }
             else
                 r1 = "";
            if ((c == "游客" || c == "背包客") && (b == "铁路" || b == "飞机"))
             {
                 r2 = "增加中";
             }
             else
                 r2 = "";
  richTextBox4.Text = r1.ToString() + r2.ToString();//输出if的结果
 
           }
2012-12-09 14:21
urick
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2012-12-6
得分:0 
回复 2楼 hjywyj
将“richTextBox4.Text = ...”放在最后也还是没有输出
2012-12-09 14:47
mmxo
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:13
帖 子:189
专家分:1090
注 册:2012-11-7
得分:7 
你对r1字段进行了两次条件截然不同的处理,这样的话相当于第一个if是不起作用的,由于你没有说清楚你的逻辑是什么,我只能猜测你可能想这样:
程序代码:
private void button1_Click(object sender, EventArgs e)
        {
            var name = richTextBox1.Text.ToCharArray();
            var a    = name[0].ToString(CultureInfo.InvariantCulture);
            var b    = richTextBox2.Text.Trim();
            var c    = richTextBox3.Text.Trim();
            var r1   = "";
            var r2   = "";
            r1 = (a == "" || a == "") && (b == "地铁" || b == "公交")
                     ? "拥堵"
                     : ((a == "" || a == "西") && (b == "铁路" || b == "飞机") ? "少量" : "");
            r2 = (c == "游客" || c == "背包客") && (b == "铁路" || b == "飞机") ? "增加中" : "";
            richTextBox4.Text = r1 + r2;
        }


为提高中华编程水平而奋斗
2012-12-09 16:29
urick
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2012-12-6
得分:0 
回复 4楼 mmxo
是的  真牛!
追问下: 对于 richTextBox4.Text = r1 + " " + r2;//需要在2个值之间添加1个空格。如果r1的值为空,richTextBox4.Text 只显示r2,而不是" "r2。是要怎么来进行判断呢?

2012-12-09 18:07
shmilyflf
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:5
帖 子:356
专家分:1008
注 册:2012-12-9
得分:7 
回复 5楼 urick
你整体Trim()不就行了,既然r1为空,那么加上的空,给去掉就行了
2012-12-09 18:54



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-390276-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.101633 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved