static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("1. SHC ");
int UserInput1 = Console.Read();
if (UserInput1 == 1)
{
Console.WriteLine("Mass (kg): ");
int shcmass = Console.Read();
Console.WriteLine("Specific Heat Capactiy (J/Kg/°C): ");
int shcshc = Console.Read();
Console.WriteLine("Temperature Difference (△Ø): ");
int shctemp = Console.Read();
int shcfinal = shcmass * shcshc * shctemp;
Console.WriteLine("Energy: " + shcfinal);
}
这是我在小型控制台应用程序中使用的代码。我不知道我是否遗漏了一些东西,但每次运行它时,第一位都会在“1. SHC”的位置工作,并为用户输入提供时间。但一旦进入控制台应用程序死亡,我无法弄清楚原因。
答案 0 :(得分:0)
没有什么可以阻止控制台应用程序关闭,在主循环的末尾添加一个Console.ReadLine()。我建议使用ReadLine()来读取其他输入,但这取决于您的需求。如果您从"备注" Console.Read documentation的部分最好使用ReadLine()
e.g。
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("1. SHC ");
int UserInput1 = int.Parse(Console.ReadLine());
if (UserInput1 == 1)
{
Console.WriteLine("Mass (kg): ");
int shcmass = int.Parse(Console.ReadLine());
Console.WriteLine("Specific Heat Capactiy (J/Kg/°C): ");
int shcshc = int.Parse(Console.ReadLine());
Console.WriteLine("Temperature Difference (△Ø): ");
int shctemp = int.Parse(Console.ReadLine());
int shcfinal = shcmass * shcshc * shctemp;
Console.WriteLine("Energy: " + shcfinal);
}
Console.ReadLine();
}
答案 1 :(得分:0)
ch = Convert.ToChar(x);
返回
输入流中的下一个字符,如果是,则为负一(-1) 目前没有更多字符可供阅读。
https://stackoverflow.com/a/9890107/1664675
你需要
class Sample
{
public static void Main()
{
string m1 = "\nType a string of text then press Enter. " +
"Type '+' anywhere in the text to quit:\n";
string m2 = "Character '{0}' is hexadecimal 0x{1:x4}.";
string m3 = "Character is hexadecimal 0x{0:x4}.";
char ch;
int x;
//
Console.WriteLine(m1);
do
{
x = Console.Read();
try
{
ch = Convert.ToChar(x);
if (Char.IsWhiteSpace(ch))
{
Console.WriteLine(m3, x);
if (ch == 0x0a)
Console.WriteLine(m1);
}
else
Console.WriteLine(m2, ch, x);
}
catch (OverflowException e)
{
Console.WriteLine("{0} Value read = {1}.", e.Message, x);
ch = Char.MinValue;
Console.WriteLine(m1);
}
} while (ch != '+');
}
}
摘录:
import codecs
# script.txt contains the sample text you posted
with codecs.open('script.txt', 'r', 'utf8') as f:
# read the file content
f = f.read()
# store all the clean text that's accumulated
spoken_text = ''
# split the file into a list of strings, with each line a member in the list
for line in f.split('\n'):
# split the line into a list of words in the line
words = line.split()
# if there are no words, do nothing
if not words:
continue
# if this line is a person identifier, do nothing
if len(words[0]) > 1 and all([i.isupper() for i in words[0]]):
continue
# if there's a good amount of whitespace to the left, this is a spoken line
if len(line) - len(line.lstrip()) > 4:
spoken_text += line.strip() + ' '
print(spoken_text)
答案 2 :(得分:-1)
之前我遇到过类似的问题。在Console.Read()
之后使用Console.WriteLine()
时,您最终会阅读'\ r',这是导致崩溃的原因。请尝试使用Console.ReadKey()
。