Saturday, 15 February 2014

java - my voting system is already fine but -


I am starting in Java and I have these code for my voting system:

  Public Zero Result_Election () {int vote1 = Vote_President (); String pres1 = "thio"; String pres2 = "William"; String pres3 = "maxon"; String pres4 = "douglas"; Int n1 = 0, n2 = 0, n3 = 0, n4 = 0; {PrintWriter i = New PrintWriter (Try New FileWriter ("voting_score.txt", true)); If (vote1 == 1) {int [] addVotes = {1}; For (int add: addVotes) {Results [add -1] + = 1; N1 = result [ed-1]; } I.println (pres1 + "+ n1);} and if (vote1 == 2) {int [] for addVotes = {2}; (int add: addVotes) {result [add -1] + = 1 ; N2 = result [ed-1];} I.println (pres2 + "+ 2 + 2"); } And if (vote1 == 3) {int [] addVotes = {3}; For (int add: addVotes) {Results [add -1] + = 1; N3 = result [ad-1]; } I.println (pres3 + "+ n3);} and if (vote1 == 4) {int [] for addVotes = {4}; (int add: addVotes) {result [add -1] + = 1 ; N4 = result [ed-1];} I.println (pres4 + "+ n4); } I closed (); } Hold (exception e) {}}  

My problem is output Every time I add a vote to a candidate, then add another name with its increased votes. But I hope every candidate is just one name and every time I add a vote to a candidate, he does not add another name. Just the number of votes please help

To avoid all those variables and if-else block, do we Just do something like this - / P>

  Maps & lt; String, integer & gt; Candidates = new Hashmap & lt; String, integer & gt; (); Candidates.put ("Theo", 0); Candidates.put ("William", 0); Candidates.put ("Maxx", 0); Candidates ("Douglas", 0); Switch (Vote1) {Case 1: Candidates ("Theo", receive candidates ("Theo") + 1); break; Case 2: Candidates ("William", receive candidates ("William") + 1); break; Case 3: Candidates ("Maxson", get the candidates. ("Maxon") + 1); break; Case 4: Candidates ("Douglas", Get the Candidates ("Douglas") + 1); break; }  

Understanding this and debugging it will be easy. This is just an example. You can use it the way you want it.

me "int [] addVotes = {1};" No use of this is seen; And walk on it again because it's always going to catch the same value? What is your intention here? Besides how did you start "results"? [Update] To make it your way and reduce unnecessary details:

  Public Zero Result_Election () {int vote1 = Vote_President (); String [] Candidate Array = {"Theo", "William", "Maxon", "Douglas"}; String filebusolutepoint = "c: /voting_score.txt"; Try {int [] results = getStorredResult (file assault path, candidate Array); Print witter pw = new print referer (new flumerwriter (file asset footpath)); Results [Votes1-1] = Result [Votes1-1] +1; For (int i = 0; i & lt; candidateArray.length; i ++) {pw.println (candidate array [i] + "" + result [i]); } Pw.flush (); Pw.close (); } Hold (exception e) {e.printStackTrace (); // better log in}} private int [] GetStorredResult (string filename, string [] candidate) throws NumberFormatException, IOException {String currentLine = null; Int [] Results = new int [candidateArray.length]; File file = new file (filename); If (file.exists ()) {BufferedReader br = new BufferedReader (new FileReader (file)); While ((currentLine = br.readline ()) = False {for (int i = 0; i & lt; candidateArray.length; i ++) {if (currentLine.startsWith (candidateArray [i])) {Results [ I] = integer PRIIST (current lines. Split ("") [1]); break; }}} Br.close (); } Return results; }  

No comments:

Post a Comment