| |

VerySource

 Forgot password?
 Register
Search
View: 875|Reply: 2

The doubt of read ()!

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-3-22 08:30:01
| Show all posts |Read mode
using System;

class Test_Add
{
    static void Main ()
    {
        int x, y;
        Console.Write ("Please enter x:");
        x = Console.Read ();
        Console.Write ("Please enter y:");
        y = Console.Read ();

        x = x + y;

        Console.WriteLine ("x + y = {0}", x);
        Console.WriteLine ("Please press Enter to end!");
        Console.Read ();

    }
}
// Question: When I execute the program, I am prompted for x, but when I enter x and press Enter, the result is displayed directly! Why is that?
Reply

Use magic Report

0

Threads

21

Posts

16.00

Credits

Newbie

Rank: 1

Credits
16.00

 China

Post time: 2020-7-3 16:15:01
| Show all posts
using System;

class Test_Add
{
    static void Main()
    {
        int x, y;
        Console.Write("Please enter x:");
        x = Convert.ToInt32(Console.ReadLine());
        Console.Write("Please enter y:");
        y = Convert.ToInt32(Console.ReadLine());

        x = x + y;

        Console.WriteLine("x+y={0}", x);
        Console.WriteLine("Please press Enter to end!");
        Console.ReadLine();

    }
}
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-7-9 12:00:01
| Show all posts
So why doesn't the following program work? Are they all int type? Why is there a format error?
using System;

class Test_Add
{
    static void Main()
    {
        int x, y;
        Console.Write("Please enter x:");
        x = Convert.ToInt32(Console.Read());
        Console.Write("Please enter y:");
        y = Convert.ToInt32(Console.ReadLine());

        x = x + y;

        Console.WriteLine("x+y={0}", x);
        Console.WriteLine("Please press Enter to end!");
        Console.ReadLine();

    }
}
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list