C# 投票模拟器如何计算零或未投票的项目/国家

问题描述

我正在使用欧洲电视网投票竞赛模拟器。

26 个国家必须随机投票给其他 10 个国家(没有重复的国家,也没有自己的国家)。

所以我做了一个 for (Countries.Length) 和里面的 for (PossibleVotes.Length) 用于分配投票。

甚至处理最高票数(12 分)的计数器,以显示“最佳”获奖者。

这已经完成了。

代码

//struct array country[26] 
//country[].sName = Spain,Italy,France...
//country.iVotes = Total Votes
//country.iTwelves = Counter for total12 recieved
//country.iZeros = Counter for total 0 recieved
// iPossibleVotes[10] {12,10,8,7,6,5,4,3,2,1 }

    for (int i = 0 ; i < country.Length ; i++){
      
      Console.WriteLine("\n___Country " + (i+1) + " " + country[i].sName + "___");
      Console.WriteLine("\nVotes:");
      
      int[] iVotedCountry = Utils.RandomArrayWitoutDuplicates(i);
      //Function to make an array of 10 random integers without duplicates nor itself (i value)
      

      //Executes 10 times
      for (int j = 0 ; j < iVotedCountry.Length ; j++){

        Console.WriteLine("-" + country[iVotedCountry[j]].sName + " with " + iPossibleVotes[j] + " points. ");
        
//If j = 0 equals to iPossibleVotes[] =12,sum iTwelves counter
        if (j == 0){
          country[iVotedCountry[j]].iTwelves++;
        }


   

      } // END FOR (iVotedCountry.Length) Countries that are being Voted.

      for (int k = 0 ; k < country.Length ; k++){

          if (k != iVotedCountry[j]) {

            country[k].iZeros++;  

          }

        }


 } //END COUNTRY.Length ARRAY

//Expected output
Console.WriteLine("___TheLooser___\nThe country or countries with more zeros recieved are: " );

//Then sort them in an array
//BUT I can't handle to count the times a country hasn't been Voted or Voted '0'

教师还要求显示“TheLooser”获胜者,即投票较少或投票“0”的国家(或国家,并列)。

我的想法是在我将真正的可能投票数分配给 10 个随机国家后,再给其他 16 个国家分配“0”票。

我也对伪代码抽象的想法或评论感兴趣,可以考虑一下。

谢谢

解决方法

不要保留每个国家(例如 iVotes、iZeroes、iTwelves)的投票总和和零的数量,请考虑使用这种方法。

对于每个国家,保留所有选票。例如,如果有 10 轮投票,那么您可以有一个包含 10 个元素的数组,每个元素保存该轮的分数,例如 12、10、8 等,默认值为 0。

那么你的程序分为 3 个部分。

  1. 记录所有投票

  2. 汇总每个国家的投票数 并找到最高的那个,考虑到领带

  3. 把每个国家的票数相加,找出最低的那个, 再次,考虑到关系

如果您愿意,第 2 部分和第 3 部分可以在 1 个循环中,但不要更简洁。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...