是否可以cout在窗口中打印一行文本,在重复调用或更新的函数内只能在控制台上打印一次?
为了给出一些范围,我有一个键盘输入功能被调用来检查按键,当我按下"C"我的相机时,值会更新,我打印出确认到控制台,如下所示:cout << "\nView switched to 'Default View..."<< endl;`但它在无限循环中永久打印它.
这可能听起来像一个简单的问题,但这是我第一次遇到这样的问题.
void keyboard()
{
if (CAM_DEF) //switch to default view
{
cout << "\nView switched to 'Default View`..." << endl;
Q_PRESSED = false;
E_PRESSED = false;
}
... //more key presses
}
Run Code Online (Sandbox Code Playgroud) 我试图在我的 C# 代码中使用此命令“winsatformal”运行隐藏控制台,但它在“Process.Start()”中给出错误。这是我的代码:
string command = "winsat formal";
Process process = new Process();
process.StartInfo.FileName = command;
process.Start();
Run Code Online (Sandbox Code Playgroud) 这些是我用来保存数据的变量
int num1 = int.Parse(Console.ReadLine());
string sign = Console.ReadLine();
int num2 = int.Parse(Console.ReadLine());
Run Code Online (Sandbox Code Playgroud)
检查输入的if语句
如何使它变得简单
if (sign == "+")
{
Console.WriteLine(num1 + num2);
}
else if (sign == "-")
{
Console.WriteLine(num1 - num2);
}
else if (sign == "*")
{
Console.WriteLine(num1 * num2);
}
else if (sign == "/")
{
Console.WriteLine(num1 / num2);
}
else
{
Console.WriteLine("Wrong operation sign ...");
}
Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)
如何在窗体中显示输出,num1 sign num2 = num3例如"6 + 4 = 10"在控制台窗口中?
我是C#的新手,我正在使用微软的Visual Studio Express 2013 Windows桌面版本,我正在尝试进行一个测验,我问这个问题,用户必须回答它,这里是代码,我得到的错误是"无法将类型'string'隐式转换为'bool'",这发生在2 if语句中,我知道bool的值为true或false但是它是一个字符串,为什么它会给我这个错误?任何帮助应该被赞赏. PS:我只包含了我遇到问题的代码部分,这是主类中唯一的代码
下面是代码:
Start:
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Question 1: Test? type yes or no: ");
String answer1 = Console.ReadLine();
if (answer1 = "yes") {
Console.WriteLine();
Console.WriteLine("Question 2: Test? type Yes or no");
}
else if (answer1 = "no")
{
Console.WriteLine();
Console.WriteLine("Wrong, restarting program");
goto Start;
}
else {
Console.WriteLine();
Console.WriteLine("Error");
goto Start;
}
Run Code Online (Sandbox Code Playgroud) 我试图在open.kattis.com上解决这个问题https://open.kattis.com/problems/simon,我有这段代码,如果字符串以simon开头,它就完全符合预期然后它的字符串的其余部分被输出,如果它不以simon开始说,则输出一个空行.
using System;
namespace Tester
{
internal class Program
{
private static void Main(string[] args)
{
int lines = int.Parse(Console.ReadLine());
string[] inputs = new string[lines];
for (int i = 0; i < lines; i++)
{
inputs[i] = Console.ReadLine().ToLower();
if (inputs[i].StartsWith("simon says"))
inputs[i] = inputs[i].Substring(10);
else
inputs[i] = "";
}
for (int i = 0; i < lines; i++)
Console.WriteLine(inputs[i]);
}
}
}
Run Code Online (Sandbox Code Playgroud)
输入:
4
simon says write a program
print some output
simon whispers do not stress
simon …Run Code Online (Sandbox Code Playgroud) 我试图通过执行C++风格来读取控制台输入
int main()
{
std::string str;
std::getline(std::cin, str);
// also tested: std::cin >> str;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
和C风格
int main()
{
char* str = new char[20];
scanf_s("%s", str);
delete[] str;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是如果我输入一个字符串然后按回车键,控制台中的光标将不会跳转到下一行,它会跳转到输入命令所在行的第一列.输入第二个键会导致错误:
执行C++样式代码后的错误消息框:
Run Code Online (Sandbox Code Playgroud)Debug Assertion Failed! Program: D:\_extern\Test\Test.exe File: minkernel\crts\ucrt\src\appcrt\lowio\read.cpp Line: 259 Expression: static_cast<void const*>(source_buffer) == static_cast<void const*>(result_buffer) For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts.
执行C风格代码后出错:
Run Code Online (Sandbox Code Playgroud)Exception triggered at 0x00007FF95C9398E9 (ucrtbased.dll) in Test.exe: 0xC0000005: Access violation when …
我尝试制作一个代码,在文本中打印两个相等的随机值。
import random
archivo=open('Ejercicio3.txt', 'w')
up=0
insert=int(input('Valor: '))
for i in range(insert):
up+=1
n1=random.randint(10, 100)
n2=random.randint(10, 100)
archivo.write(f'{up}.- {n1} - {n2} = {n1-n2}\n\n')
print(f'{up}.- {n1} - {n2} = {n1-n2}')
archivo.write(f'{n1-n2}\n')#This is the problem, because the code write numbers different random
archivo.close()
Run Code Online (Sandbox Code Playgroud)
我想要的是它做类似的事情:
import random
archivo=open('Ejercicio1.1.txt', 'w')
up=0
insert=int(input('Valor: '))
for i in range(insert):
up+=1
n1=random.randint(10, 100)
n2=random.randint(10, 100)
archivo.write(f'{n1} - {n2} =''\n\n')
up=0
archivo.write('---------------------------------------------\n')
for i in range(insert):
up+=1
n1=random.randint(10, 100)
n2=random.randint(10, 100)
archivo.write(f'{n1-n2}''\n\n')#Except this part, I want it to …Run Code Online (Sandbox Code Playgroud) 我正在制作一个游戏,当你向下移动屏幕时,你可以避开由'X'组成的墙壁,如果你击中X,你将失去一个盾牌并继续掠过X而不会再失去任何盾牌,直到你弹出一个角色空间哪里没有X.
但是,当运行下面的代码以及其他一些不必要的代码时,如果我在Visual Studio中停止运行,它会完美地运行,但是当我在代码运行时开始撞到'X'墙并且墙壁移动得非常快时它不会工作正确,你最终做了耕作,但你失去了过程中的所有盾牌,并且当你"犁"穿过它们时,X不会消失.
这是代码:
char coll; // the buffer for the collided char
if (theScreen.check_collision(player, xMove, yMove, coll) != true) {
// no collision
// get rid of old player placing
theScreen.Insert(' ', player.get_location()[0], player.get_location()[1]);
// put in new charater placing
player.move(xMove, yMove);
theScreen.Insert(player);
numb_coll = 0;
} else {
// collision
if (coll == 'X' && player.get_shield() > 0) {
for (int I = 0; I < numb_coll && numb_coll < player.get_location()[1]; I++) {
theScreen.Insert(' ', player.get_location()[0],
player.get_location()[1] - …Run Code Online (Sandbox Code Playgroud) 我在我的ac#console应用程序代码中收到此错误
case 5:
Console.WriteLine("User selected to Quit, option " + response);
Environment.Exit;
break;
Run Code Online (Sandbox Code Playgroud)
//错误只有赋值,调用,递增,递减和新对象表达式才能用作语句
好吧,所以我希望我的C#程序执行以下两个控制台命令.
takeown /f "c:\windows\system32\Utilman.exe"
icacls "c:\windows\system32\Utilman.exe" /grant administrators:F
Run Code Online (Sandbox Code Playgroud)
我的问题是C#无法处理额外的路径.(也尝试使用转义序列没有运气)