C#控制流程

时间:2011-01-06 21:37:19

标签: c#

我做了一个号码,用C#请求2个号码,然后用相应的消息回复。怎么会对第二个号码不起作用? 无论我为第二个号码输入什么,我都会收到消息“你的号码在0-10范围内”。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string myInput;  // declaring the type of the variables
            int myInt;

            string number1;
            int number;


            Console.WriteLine("enter a number");
            myInput = Console.ReadLine(); //muyInput is a string  which is entry input
            myInt = Int32.Parse(myInput); // myInt converts the string into an Integer

            if (myInt > 0)
                Console.WriteLine("Your number {0} is greater than zero.", myInt);
            else if (myInt < 0)
                Console.WriteLine("Your number {0} is  less  than zero.", myInt);
            else
                Console.WriteLine("Your number {0} is equal zero.", myInt);

            Console.WriteLine("enter another number");
            number1 = Console.ReadLine(); 
            number = Int32.Parse(myInput); 

            if (number < 0 || number == 0)
                Console.WriteLine("Your number {0} is  less  than zero or equal zero.", number);
            else if (number > 0 && number <= 10)
                Console.WriteLine("Your number {0} is  in the range from 0 to 10.", number);
            else
                Console.WriteLine("Your number {0} is greater than 10.", number);

            Console.WriteLine("enter another number");

        }
    }    
}

5 个答案:

答案 0 :(得分:7)

number = Int32.Parse(myInput);应该阅读number = Int32.Parse(number1);

答案 1 :(得分:2)

number = Int32.Parse(myInput);

第二个数字应为

number = Int32.Parse(number1);

答案 2 :(得分:2)

因为您要解析相同的字符串两次

number1 = Console.ReadLine(); 
number = Int32.Parse(myInput); 

应该是

number1 = Console.ReadLine(); 
number = Int32.Parse(number1); 

答案 3 :(得分:1)

你输入的是第一个号码?在“输入另一个号码”后,您将number1分配给控制台中的值,但您从未使用过,实际上是使用myInput

中的值

答案 4 :(得分:0)

number1 = Console.ReadLine(); 
number = Int32.Parse(myInput); 

在第二次测试中,您评估第一个输入,它应该是

Int32.Parse(number1);