标签: console

使用cout在重复调用的函数中只打印一次语句

是否可以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++ console cout

-1
推荐指数
1
解决办法
3177
查看次数

代码中的 C# 命令行

我试图在我的 C# 代码中使用此命令“winsatformal”运行隐藏控制台,但它在“Process.Start()”中给出错误。这是我的代码:

string command = "winsat formal";

Process process = new Process();
process.StartInfo.FileName = command;
process.Start();
Run Code Online (Sandbox Code Playgroud)

c# console command-line

-1
推荐指数
1
解决办法
299
查看次数

C#中的简单计算器就像这个6 + 4

这些是我用来保存数据的变量

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# asp.net console calculator

-1
推荐指数
1
解决办法
359
查看次数

C#无法将类型'string'隐式转换为'bool'错误

我是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)

c# string console boolean

-1
推荐指数
1
解决办法
2万
查看次数

Simon说C#控制台,收到运行时错误

我试图在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# console

-1
推荐指数
1
解决办法
468
查看次数

控制台未正确读取输入字符串

我试图通过执行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++样式代码后的错误消息框:

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.
Run Code Online (Sandbox Code Playgroud)

执行C风格代码后出错:

Exception triggered at 0x00007FF95C9398E9 (ucrtbased.dll) in Test.exe: 0xC0000005: Access violation when …
Run Code Online (Sandbox Code Playgroud)

c++ console winapi c++17

-1
推荐指数
1
解决办法
92
查看次数

如何打印两次相同的随机数?

我尝试制作一个代码,在文本中打印两个相等的随机值。

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)

python windows random math console

-1
推荐指数
1
解决办法
73
查看次数

文本游戏角色碰撞检测问题

我正在制作一个游戏,当你向下移动屏幕时,你可以避开由'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)

c++ console

-2
推荐指数
1
解决办法
611
查看次数

"只有赋值,调用,递增,递减和新对象表达式才能用作语句"

我在我的ac#console应用程序代码中收到此错误

 case 5:
    Console.WriteLine("User selected to Quit, option " + response);
    Environment.Exit;
    break;
Run Code Online (Sandbox Code Playgroud)

//错误只有赋值,调用,递增,递减和新对象表达式才能用作语句

c# environment console quit

-2
推荐指数
1
解决办法
1万
查看次数

C#执行控制台命令

好吧,所以我希望我的C#程序执行以下两个控制台命令.

takeown /f "c:\windows\system32\Utilman.exe"  
icacls "c:\windows\system32\Utilman.exe" /grant administrators:F  
Run Code Online (Sandbox Code Playgroud)

我的问题是C#无法处理额外的路径.(也尝试使用转义序列没有运气)

c# console

-2
推荐指数
1
解决办法
1549
查看次数

标签 统计

console ×10

c# ×6

c++ ×3

asp.net ×1

boolean ×1

c++17 ×1

calculator ×1

command-line ×1

cout ×1

environment ×1

math ×1

python ×1

quit ×1

random ×1

string ×1

winapi ×1

windows ×1