0

我正在承担统一设计游戏的任务。我不是编程专家,但我在 Ansi C 方面有很多知识。如果我的代码类似于 C#,请原谅我。

该方法工作得很好,但由于某些奇怪的原因,Debug.Log(string.Format("",)显示:

pop_class index value 0 value = 0,
pop_class index value 1 value = 1,   

如果我在方法之外简单地键入带有相同信息的 debug.log ,它会显示 i 变量和 pop_class[i] 一切都正确显示。知道为什么会这样吗?该程序足够复杂,我不想在调试时与日志文件搏斗。

public int pop_generation() //debugger is currently not displaying vars correctly
{   
    int pop_increase = 1000000;
    int pop_counter = 0;//counts populations assigned by the 1000s
    int high_class_per = 10;
    int mid_class_per = 50;
    int low_class_per = 40;
    int lpoptotal = (faction_pop/100) * low_class_per;
    int lpop_count = 0;
    int mpoptotal = (faction_pop/100) * mid_class_per;
    int hpoptotal = (faction_pop/100) * high_class_per;
    //calucates certain percentage of low class
    int mid_class_pop = (faction_pop/100) * mid_class_per;
    int high_class_pop = (faction_pop/100) * high_class_per;
    System.Random random_now = new System.Random();
    for(int i = 0; i < replimit_check && pop_counter < faction_pop;i++)
    {
        //keeps total of low class rep population by 1,000s
        if(lpop_count > lpoptotal)
        {
            Debug.Log("Low class generation complete");
            pop_class[i] = 0;
            display_high_class();
            i--;//brings incrementer back once
            break;
        }
        pop_class[i] = random_now.Next(20);
        pop_counter = pop_increase + pop_counter;
        lpop_count = pop_increase + lpop_count;
        if(debug_on)
            Debug.Log(string.Format("Pop_class index value {0} value = {0} ",i,pop_class[i])); //debug.log is bugged

    }
    return 0;
}
4

1 回答 1

1

更改此行:

Debug.Log(string.Format("Pop_class index value {0} value = {0} ",i,pop_class[i])); //debug.log is bugged

对此:

Debug.Log(string.Format("Pop_class index value {0} value = {1} ",i,pop_class[i])); //debug.log is bugged

您将第一个参数打印两次,因此pop_class[i]永远不会被打印。您需要更改{0}{1}.

String.Format() 函数接受一个对象数组作为它的第二个参数(至少在这种情况下)。这允许您为函数指定任意数量的参数。{0}告诉 Format() 将第二个参数输出到函数,{1}第三个等。第一个参数是格式字符串。

于 2013-07-01T20:23:05.990 回答