c#代码中的问题(Noob)

时间:2015-05-22 09:13:06

标签: c# .net

我的计划在给出结果之前关闭,年龄差异是错误的。

我已经到处检查了,他们说使用Console.Read()Console.ReadLine()Console.ReadKey(),我们在他们说出来之前就已经做过了,但它仍然没有用。其他人说使用System("PAUSE"),但这只是给我一个错误。

当我21岁进入21岁时,它也说21岁= 38岁

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

namespace ConsoleApplication1
{
    class Program
    {

        static String name;
        static int age;

        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to the application!");
            Console.Write("What's your name? ");

            // Setting the name string to the line read from the console.
            name = Console.ReadLine();

            Console.Write("\n So how old are you " + name + "? ");

            // Doing the same thing we did last time with the age.
            age = Console.Read();

            compareAges();
            Console.Read();
        }

        static void compareAges()
        {
            if(age < 12)
            {
                Console.WriteLine("I'm older than you! I'm 12.");
                Console.ReadLine();
            }
            else if(age == 12)
            {
                Console.WriteLine("We're the same age!!!");
                Console.ReadLine();
            }
            else
            {
                int ageDifference = age - 12;
                Console.WriteLine("You're " + ageDifference + " years older than me!");
                Console.ReadLine();
            }
        }
    }
}

P.S。很抱歉,如果我在这里发现了一些缩进错误,但实际代码的缩进是正确的。

3 个答案:

答案 0 :(得分:4)

问题在于

age = Console.Read();

好吧,Console.Read()只读取一个符号 - 你的案例2 int中的字符50,你有

  '2' - 12 == 38 // '2' == 50

补救措施:在您的案例中阅读整个字符串 "21"

  String line = Console.ReadLine(); 

然后解析它为整数:

  // Parse() is the simplest; TryParse() is a better: 
  // what if a user entered "bla-bla-bla"?
  age = int.Parse(line); 

答案 1 :(得分:1)

你的问题在这里:

age = Console.Read();

这里你只需要将年龄设置为输入字符的ASCII码,因为Read从控制台读取一个符号并返回它的ASCII码。

应该是

age = Convert.ToInt32(Console.ReadLine());

答案 2 :(得分:0)

这是我的看法。也许不是最好的,但它确实有效:

using System;
using System.IO;

namespace ConsoleApplication1{
    class Program{
        public static int Main(){
            Program.determineAgeDifference dta = new Program.determineAgeDifference();

            string[] input = new string[2];

            Console.Write("So, what's your name?\n\n>> ");
            input[0] = Console.ReadLine();

            Console.Clear();

            Console.Write("How old are you?\n\n>> ");
            input[1] = Console.ReadLine();

            Console.WriteLine(dta(int.Parse(input[1].TrimStart(' ').TrimEnd(' ')));
            Console.ReadLine();

            return 0;
        }
        private string determineAgeDifference(int age){
             string returnValue = "";

            if (age < 12){
                returnValue = "I'm older than you. I'm 12!";
            }
            else if (age == 12){
                returnValue = "We are the same age!!!";
            }
            else{
                returnValue = ("You're " + (age - 12).ToString() + "Years older than me!");
            }

            return returnValue;
        }
    }
}