C# 程序不工作,控制台输出空白
C# program not working, blank console output
这里是控制台空白的程序-
using System;
namespace here
{
class Program
{
static void Main(string[] args)
{
double width, height, woodLength, glassArea;
string widthString, heightString;
widthString = Console.ReadLine();
width = double.Parse(widthString);
heightString = Console.ReadLine();
height = double.Parse(heightString);
woodLength = 2 * ( width + height ) * 3.25 ;
glassArea = 2 * ( width * height ) ;
Console.WriteLine ( "The length of the wood is " + woodLength + " feet" ) ;
Console.WriteLine( "The area of the glass is " + glassArea + " square metres" ) ;
Console.ReadKey(true);
}
}
}
这是显示输出的程序-
using System;
namespace here
{
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Console.ReadKey(true);
}
}
}
我什至尝试卸载我所有的防病毒软件,正如我在其他 post 中读到的那样。没有任何变化。
行 widthString = Console.ReadLine();
等待,直到在键盘上输入内容并按下 Enter
键。
为程序输入两个值(宽度和高度,用 space 分隔)并按回车键显示输出。
(我假设您是 C# 编程的新手 - 希望这对您有所帮助!)
您的程序正在正确执行。
控制台是空白的,因为您没有在提示的开头打印任何内容。
试试这个
using System;
namespace here {
class Program
{
static void Main(string[] args)
{
double width, height, woodLength, glassArea;
string widthString, heightString;
Console.WriteLine ("Enter width");
widthString = Console.ReadLine();
width = double.Parse(widthString);
Console.WriteLine ("Enter height");
heightString = Console.ReadLine();
height = double.Parse(heightString);
woodLength = 2 * ( width + height ) * 3.25 ;
glassArea = 2 * ( width * height ) ;
Console.WriteLine ( "The length of the wood is " +
woodLength + " feet" ) ;
Console.WriteLine( "The area of the glass is " +
glassArea + " square metres" ) ;
Console.ReadKey(true);
}
}
}
看看这里的输出:
正如 Aniruddha Varma 所说,您正在使用 Console.ReadLine()。这是等待你输入到控制台。如果您输入两个数字,程序就会打印出来。程序暂停,等待您输入内容然后按回车键。然后第二个 Readline 函数做同样的事情。输入两个值后,函数的其余部分将按您预期的方式继续。例如我输入:
2
4
The length of the wood is 39 feet
The area of the glass is 16 square meters
您的程序可能可以运行,它只是在等待输入。试试这个:
static void Main(string[] args)
{
Console.WriteLine("Starting program...");
double width, height, woodLength, glassArea;
string widthString, heightString;
Console.WriteLine("Enter width: ");
widthString = Console.ReadLine();
width = double.Parse(widthString);
Console.WriteLine("Enter height: ");
heightString = Console.ReadLine();
height = double.Parse(heightString);
Console.WriteLine("Calculating...")
woodLength = 2 * ( width + height ) * 3.25 ;
glassArea = 2 * ( width * height ) ;
Console.WriteLine ( "The length of the wood is " + woodLength + " feet" ) ;
Console.WriteLine( "The area of the glass is " + glassArea + " square metres" ) ;
Console.ReadKey();
}
这里是控制台空白的程序-
using System;
namespace here
{
class Program
{
static void Main(string[] args)
{
double width, height, woodLength, glassArea;
string widthString, heightString;
widthString = Console.ReadLine();
width = double.Parse(widthString);
heightString = Console.ReadLine();
height = double.Parse(heightString);
woodLength = 2 * ( width + height ) * 3.25 ;
glassArea = 2 * ( width * height ) ;
Console.WriteLine ( "The length of the wood is " + woodLength + " feet" ) ;
Console.WriteLine( "The area of the glass is " + glassArea + " square metres" ) ;
Console.ReadKey(true);
}
}
}
这是显示输出的程序-
using System;
namespace here
{
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Console.ReadKey(true);
}
}
}
我什至尝试卸载我所有的防病毒软件,正如我在其他 post 中读到的那样。没有任何变化。
行 widthString = Console.ReadLine();
等待,直到在键盘上输入内容并按下 Enter
键。
为程序输入两个值(宽度和高度,用 space 分隔)并按回车键显示输出。
(我假设您是 C# 编程的新手 - 希望这对您有所帮助!)
您的程序正在正确执行。 控制台是空白的,因为您没有在提示的开头打印任何内容。 试试这个
using System;
namespace here {
class Program
{
static void Main(string[] args)
{
double width, height, woodLength, glassArea;
string widthString, heightString;
Console.WriteLine ("Enter width");
widthString = Console.ReadLine();
width = double.Parse(widthString);
Console.WriteLine ("Enter height");
heightString = Console.ReadLine();
height = double.Parse(heightString);
woodLength = 2 * ( width + height ) * 3.25 ;
glassArea = 2 * ( width * height ) ;
Console.WriteLine ( "The length of the wood is " +
woodLength + " feet" ) ;
Console.WriteLine( "The area of the glass is " +
glassArea + " square metres" ) ;
Console.ReadKey(true);
}
}
}
看看这里的输出:
正如 Aniruddha Varma 所说,您正在使用 Console.ReadLine()。这是等待你输入到控制台。如果您输入两个数字,程序就会打印出来。程序暂停,等待您输入内容然后按回车键。然后第二个 Readline 函数做同样的事情。输入两个值后,函数的其余部分将按您预期的方式继续。例如我输入:
2
4
The length of the wood is 39 feet
The area of the glass is 16 square meters
您的程序可能可以运行,它只是在等待输入。试试这个:
static void Main(string[] args)
{
Console.WriteLine("Starting program...");
double width, height, woodLength, glassArea;
string widthString, heightString;
Console.WriteLine("Enter width: ");
widthString = Console.ReadLine();
width = double.Parse(widthString);
Console.WriteLine("Enter height: ");
heightString = Console.ReadLine();
height = double.Parse(heightString);
Console.WriteLine("Calculating...")
woodLength = 2 * ( width + height ) * 3.25 ;
glassArea = 2 * ( width * height ) ;
Console.WriteLine ( "The length of the wood is " + woodLength + " feet" ) ;
Console.WriteLine( "The area of the glass is " + glassArea + " square metres" ) ;
Console.ReadKey();
}