| |

VerySource

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

The doubt of read ()!

[Copy link]

1

Threads

2

Posts

3

Credits

Newbie

Rank: 1

Credits
3

 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

11

Posts

10

Credits

Newbie

Rank: 1

Credits
10

 China

Post time: 7 day(s) ago
| 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

Credits

Newbie

Rank: 1

Credits
3

 China

 Author| Post time: Yesterday 12:00
| 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-2020|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list