我正在从旧的MFC GUI迁移到C#.
当我遇到将字符串转换为整数类型的意外异常时,我正在构建基于表单的GUI.我认为它会像将字符串转换为double一样工作.
string str = "1,000";
double dthou = Convert.ToDouble(str); // OK
int ithou = Convert.ToInt32(str); // raises an exception
Run Code Online (Sandbox Code Playgroud)
转换为double会给出正确的值:1000.0.对于int转换,我能够得到一个解决方案:Convert.ToInt32()一个带逗号的字符串.
但我很好奇这背后是否有任何理由.或者,我错过了什么?
我能够找到一个类似的,但不完全是一个重复的问题: 数字解析怪异
[编辑]了解了文化问题.
我是一种文化冲击,因为到目前为止,在韩国,浮点数和整数都用","表示,对于数千组和".".对于小数点(至少在现实世界中,在韩国,我的意思是,我认为......).我想我将不得不接受MS Visual Studio的当前设置并继续.
[EDIT2]在这个问题上睡了之后.
我认为这更多是对格式化字符串的不一致处理.ToDouble接受带有千位分隔符的字符串(在我的文化中,逗号),但ToInt32没有.如果ToDouble是float | allowThousands,那么为什么could'nt ToInt32一直integer | allowThousands就是我问.
当我尝试运行这个简单的OpenGL测试程序时,我遇到了分段错误.只有在使用核心配置文件标志创建上下文时才会发生这种情况.如果我使用兼容性配置文件标志,程序运行没有问题.
编辑:我检查了函数的指针glGenVertexArrays,然后返回NULL.如果glfwCreateWindow没有返回NULL,并glGetString(GL_VERSION)确认上下文是版本4.3并glewInit返回GLEW_OK那么为什么glGenVertexArrays == NULL?
我的操作系统是64位Windows 7,而我的GPU是带有331.82 WHQL驱动程序的Nvidia GTX 760.
码:
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdlib.h>
#include <stdio.h>
#define GLSL(src) "#version 430 core\n" #src
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
GLuint create_program(const char* vertex_source, const char* fragment_source)
{
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, &vertex_source, NULL);
glCompileShader(vs); …Run Code Online (Sandbox Code Playgroud) 我正在为计算机对手构建一个具有不同AI实现的Tic Tac Toe游戏,以便学习不同的算法以及如何实现它们.我尝试的第一个应该是最简单的只是让计算机每次都选择一个随机空间.
这在一定程度上对我有用,问题就变成了运行时间.每次aiRandMove()调用该方法时,需要更长时间才能选择移动到已经在板上进行了5次移动(cpu +用户组合)之后的程序,程序似乎挂起(尽管从技术上来说并非如此).
在我进一步调试时,我意识到这应该是预期的,因为该aiRandMove()方法是随机选择X和Y坐标,然后测试移动以查看它是否合法.随着越来越少的空间被打开,合法移动越来越少,因此随机发生器尝试生成合法移动的失败次数更多.
我的问题是,有什么方法可以修改这个,至少可以减少函数所用的时间吗?据我所知,谷歌搜索和我自己解决问题,我想不出一种方法来优化这个,而不会影响功能的"随机性".我考虑过保留计算机尝试的一系列移动,但这不会解决问题,因为这不会影响rand()生成重复数字的次数.以下是此函数的代码,它与此问题非常相关:
//Function which handles the AI making a random move requires a board
//object to test moves legality and player object to make a move with
//both are passed by reference because changes to board state and the player's
//evaluation arrays must be saved
char aiRandMove(Player &ai, Board &game){
int tryX;
int tryY; //Variables to store computer's attempted moves
bool moveMade = false;
char winner;
while(!moveMade){
srand(time(NULL));//Randomizes the …Run Code Online (Sandbox Code Playgroud) 我有界面:
interface operations{
void sum();
}
Run Code Online (Sandbox Code Playgroud)
我想要上课:
class matrix implements operations {
@override
void sum(matrix m) {
}
}
class vector3D implements operations {
@override
void sum(vecor3D v) {
}
}
Run Code Online (Sandbox Code Playgroud)
这该怎么做?我试过这样的事情:
interface operations < T > {
<T> void sum(T t);
}
class matrix implements operations<matrix>{
@Override
void sum(matrix m){};
}
}
class vector3D implements operations<vector3D>{
@Override
void sum(vector3D v){};
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
我正在使用GLFW和GLEW。但是在我的对象初始化期间glGenBuffers抛出异常
void Character::init2D(glm::vec3 top, glm::vec3 bottom_left, glm::vec3 bottom_right)
{
glm::vec3 Vertices[3];
Vertices[0] = bottom_left;
Vertices[1] = top;
Vertices[2] = bottom_right;
this->top = top;
this->left_front = bottom_left;
this->right_front = bottom_right;
glGenBuffers(1, &VBO); //throws an exception 0xC0000005: Access violation
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
CompileShaders(shaderProgram, "vertex.shader", "fragment.shader");
}
Run Code Online (Sandbox Code Playgroud)
我宣布我的课Character是这样
#include <GL\glew.h>
#include <GLFW\glfw3.h>
#include <glm\glm.hpp>
#include <glm\gtc\type_ptr.hpp>
#pragma comment(lib, "glfw3.lib")
#pragma comment(lib, "glew32.lib")
class Character
{
private:
glm::vec3 top,
left_front,
right_front,
left_back,
right_back; …Run Code Online (Sandbox Code Playgroud)