C ++到C#:cin到Console.Read

时间:2010-11-04 21:34:09

标签: c# c++ console iostream

有没有办法在C#中读取同一行的多个输入,就像在C ++中一样?

我添加了一个例子:

#include <iostream>
#include <string>
using namespace std;

int main ()
{
  cout << "Format: name age"<< endl;
  int age;
  string name;
  cin >> name >> age;
  return 0;
}

4 个答案:

答案 0 :(得分:5)

String.Split是明显的解决方案:

string input = Console.ReadLine();
string [] split = input.Split(` `);

然后使用结果数组。

您丢失了“漂亮”的变量名称,必须从string转换为int - 但无论如何您都必须这样做。

您可以指定一组拆分字符:

string [] split = words.Split(new Char [] {' ', ',', '.', ':', '\t' });

答案 1 :(得分:2)

不。您必须使用Console.Read或Console.ReadLine自行实现。

答案 2 :(得分:0)

您可以使用C# std::cin class编写的Svetlin Nakov,其行为类似于C ++中的std::cinjava.util.Scanner。它可以从控制台读取数字,整数,双精度,小数和字符串标记,就像C ++中的cin >> a >> b一样。

答案 3 :(得分:0)

试试这样:

import pygame

def main():
    while True:
        pygame.init()
        
        screen = pygame.display.set_mode([1280, 720])
        pygame.display.set_caption("PYGAME DOES NOT RECEIVE SCROLL EVENT AFTER RE-INIT?")
        
        frame = pygame.time.Clock()
        
        running = True
        
        while running:
            for event in pygame.event.get():
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if event.button == 1:
                        print("left click")
                    elif event.button == 4:
                        print("scroll up")
                    elif event.button == 5:
                        print("scroll down")
                if event.type == pygame.QUIT:
                    running = False
            frame.tick(30)
        pygame.quit()

if __name__ == "__main__":
    main()