不要胡乱滥用try...catch。你就按照“大白话”的逻辑,写成if( ... "男" ....)
 ....
else if(..."女"....)
  ....
else
  xxxx这就行了,不要滥用什么try...catch。

解决方案 »

  1.   

    不要胡乱滥用try...catch。你就按照“大白话”的逻辑,写成if( ... "男" ....)
     ....
    else if(..."女"....)
      ....
    else
      xxxx这就行了,不要滥用什么try...catch。这是我们老师出的实验题目啊,把try catch取消就行了么?
      

  2.   

     string gender = Convert.ToString(gender.Text);
     string name = Convert.ToString(Name.Text);
    变量名与控制名一样,出现冲突;上面2次好像没有,注释了不要。
    你的文本框名一个是:gender,一个是name;上面一句中则是大写的了(Name),现是变量也没有用到,所以上面2句就不要了
      try
                {
                    if (name.Text != "")
                    {
                        if (gender.Text == "男")
                        {
                            MessageBox.Show("你好," + name.Text + "先生,欢迎你");
                        }
                        else if (gender.Text == "女")
                        {
                            MessageBox.Show("你好," + name.Text + "女士,欢迎你");
                        }
                        else
                            throw new InvalidOperationException("性别输入有误!");
                    }
                    else
                        throw new InvalidOperationException("姓名输入不能为空!");
                }
                catch (InvalidOperationException ioEx)
                {
                    MessageBox.Show(ioEx.Message);
                }
      

  3.   

    还有 Convert.ToString(gender.Text);gender.Text本来就是字符串,不必再用Convert.ToString.
                 if (name.Text != "")
                    {
                        if (gender.Text == "男")
                        {
                            MessageBox.Show("你好," + name.Text + "先生,欢迎你");
                        }
                        else if (gender.Text == "女")
                        {
                            MessageBox.Show("你好," + name.Text + "女士,欢迎你");
                        }
                        else
                            throw new InvalidOperationException("性别输入有误!");
                    }
                    else
                    {
                        throw new InvalidOperationException("姓名输入不能为空!");
                    }
      

  4.   

    下面是这样一些错误:错误 1 “string”不包含“Text”的定义,并且找不到可接受类型为“string”的第一个参数的扩展方法“Text”(是否缺少 using 指令或程序集引用?) C:\Users\Administrator\Documents\Visual Studio 2008\Projects\WpfApplication2\WpfApplication2\Window1.xaml.cs 33 26 WpfApplication2错误 2 “string”不包含“Text”的定义,并且找不到可接受类型为“string”的第一个参数的扩展方法“Text”(是否缺少 using 指令或程序集引用?) C:\Users\Administrator\Documents\Visual Studio 2008\Proj错误 3 “string”不包含“Text”的定义,并且找不到可接受类型为“string”的第一个参数的扩展方法“Text”(是否缺少 using 指令或程序集引用?) C:\Users\Administrator\Documents\Visual Studio 2008\Projects\WpfApplication2\WpfApplication2\Window1.xaml.cs 37 54 WpfApplication2
    ects\WpfApplication2\WpfApplication2\Window1.xaml.cs 35 32 WpfApplication2
      

  5.   

    不要胡乱滥用try...catch。你就按照“大白话”的逻辑,写成if( ... "男" ....)
     ....
    else if(..."女"....)
      ....
    else
      xxxx这就行了,不要滥用什么try...catch。这是我们老师出的实验题目啊,把try catch取消就行了么?那你说说看,程序里面引用了控件不是还得添加Forms引用么?为什么我添加还不行,该空间代码添加在源代码中还是一样的反应
      

  6.   

    不要胡乱滥用try...catch。你就按照“大白话”的逻辑,写成if( ... "男" ....)
     ....
    else if(..."女"....)
      ....
    else
      xxxx这就行了,不要滥用什么try...catch。这是我们老师出的实验题目啊,把try catch取消就行了么?那你说说看,程序里面引用了控件不是还得添加Forms引用么?为什么我添加还不行,该空间代码添加在源代码中还是一样的反应不要胡乱滥用try...catch。你就按照“大白话”的逻辑,写成if( ... "男" ....)
     ....
    else if(..."女"....)
      ....
    else
      xxxx这就行了,不要滥用什么try...catch。这是我们老师出的实验题目啊,把try catch取消就行了么?那你说说看,程序里面引用了控件不是还得添加Forms引用么?为什么我添加还不行,该空间代码添加在源代码中还是一样的反应那为什么我这不行呢?修改过的代码是这样的啊:using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;namespace WpfApplication2
    {
        /// <summary>
        /// Window1.xaml 的交互逻辑
        /// </summary>
        public partial class Window1 : Window
        {
            private string gender;
            private string name;        public Window1()
            {
                InitializeComponent();
            }
            private void btnOK_Click(object sender, RoutedEventArgs e)
            {
              
                try
                {
                    if (name!= "")
                    {
                        if (gender== "男")
                        {
                            MessageBox.Show("你好," + name + "先生,欢迎你");
                        }
                        else if (gender == "女")
                        {
                            MessageBox.Show("你好," + name + "女士,欢迎你");
                        }
                        else
                            throw new InvalidOperationException("性别输入有误!");
                    }
                    else
                        throw new InvalidOperationException("姓名输入不能为空!");
                }
                catch (InvalidOperationException ioEx)
                {
                    MessageBox.Show(ioEx.Message);
                }
            }
        }
    }
      

  7.   

    这些代码是建个WPF应用程序后直接复制进去就可以运行么,还是要设置属性什么的,我运行了看到下面有这样一个错误:错误 3 “C:\Users\Administrator\Documents\Visual Studio 2008\Projects\WpfApplication2\WpfApplication2\Properties\Resources.resx”不是有效的 Win32 资源文件 运行着程序需要设置这个吗
      

  8.   

    不要复制所有的代码,重新建立,放两个文本框,再放一个btnOK,双击后放入这个就可,  try
                {
                    if (name!= "")
                    {
                        if (gender== "男")
                        {
                            MessageBox.Show("你好," + name + "先生,欢迎你");
                        }
                        else if (gender == "女")
                        {
                            MessageBox.Show("你好," + name + "女士,欢迎你");
                        }
                        else
                            throw new InvalidOperationException("性别输入有误!");
                    }
                    else
                        throw new InvalidOperationException("姓名输入不能为空!");
                }
                catch (InvalidOperationException ioEx)
                {
                    MessageBox.Show(ioEx.Message);
                }
      

  9.   


    前台:
    <Window x:Class="WpfApplication3.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="286" Width="433">
        <Grid>
            <Button Content="Button" Height="70" HorizontalAlignment="Left" Margin="248,103,0,0" Name="button1" VerticalAlignment="Top" Width="77" Click="button1_Click" />
            <TextBox Height="45" HorizontalAlignment="Left" Margin="84,90,0,0" Name="gender" VerticalAlignment="Top" Width="130" />
            <TextBox Height="45" HorizontalAlignment="Left" Margin="84,156,0,0" Name="name" VerticalAlignment="Top" Width="130" />
        </Grid>
    </Window>
    后台:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;namespace WpfApplication3
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, RoutedEventArgs e)
            {
                         
                    if (name.Text != "")
                    {
                        if (gender.Text == "男")
                        {
                            MessageBox.Show("你好," + name.Text + "先生,欢迎你");
                        }
                        else if (gender.Text == "女")
                        {
                            MessageBox.Show("你好," + name.Text + "女士,欢迎你");
                        }
                        else
                            throw new InvalidOperationException("性别输入有误!");
                    }
                    else
                    {
                        throw new InvalidOperationException("姓名输入不能为空!");
                    }
                   }
        }
    }
      

  10.   

    Label里有值只是显示吧,你加上就行,不影响判断阿,判断的内容是来自文本框里的
      

  11.   


    楼主,我也强例建议你退钱,换个正规的
    还有以后你记得看到这个代码就直接让他们退钱。光这两句就太雷人了
     string gender = Convert.ToString(gender.Text);
      string name = Convert.ToString(Name.Text);
    本来就是string了,你还再用Convert.ToString
      

  12.   

    错误 1 当前上下文中不存在名称“name” C:\Users\Administrator\Documents\Visual Studio 2008\Projects\Test1_4\Test1_4\Window1.xaml.cs 31 21 Test1_4错误 2 当前上下文中不存在名称“gender” C:\Users\Administrator\Documents\Visual Studio 2008\Projects\Test1_4\Test1_4\Window1.xaml.cs 33 25 Test1_4
      

  13.   


    那控件Label呢我加上了,但还是出现上面的错误了啊----错误 1 当前上下文中不存在名称“name” 
      

  14.   

    是文本框吧,用这个(我复制你的,你的变成了变量了,我没看清楚)
    看#16代码
                    if (name.Text != "")
                    {
                        if (gender.Text == "男")
                        {
                            MessageBox.Show("你好," + name.Text + "先生,欢迎你");
                        }
                        else if (gender.Text == "女")
                        {
                            MessageBox.Show("你好," + name.Text + "女士,欢迎你");
                        }
                        else
                            throw new InvalidOperationException("性别输入有误!");
                    }
                    else
                    {
                        throw new InvalidOperationException("姓名输入不能为空!");
                    }
      

  15.   

    怎么会这样?,几行简单的代码,你在窗体上建立name为gender及name的文本框,再加一个button1,双击button1,加入以下代码:
     if (name.Text != "")
                    {
                        if (gender.Text == "男")
                        {
                            MessageBox.Show("你好," + name.Text + "先生,欢迎你");
                        }
                        else if (gender.Text == "女")
                        {
                            MessageBox.Show("你好," + name.Text + "女士,欢迎你");
                        }
                        else
                            throw new InvalidOperationException("性别输入有误!");
                    }
                    else
                    {
                        throw new InvalidOperationException("姓名输入不能为空!");
                    }
    这样就OK阿,
      

  16.   

    gender,name都不初始化,怎么取到数据。
      

  17.   

    直接复制代码必然是不行啊.窗体上的文本框你建立了么?拖2个文本框上来,修改文本框的name,一个起名叫name,另一个起名叫gender,这样才行啊.否则必然是找不到变量了.
      

  18.   

     string gender = Convert.ToString(gender.Text);
    string name = Convert.ToString(Name.Text);这两句把人雷的外焦里嫩,打个比方说,你已经是男的,还得再做个变性手术变成男的
      

  19.   

    哈哈,搞定了,是gender和name文本框里的属性没设置好,多谢啦。
      

  20.   

    怪不得有时候看代码到处都是try-catch