🔒 Closed Java input, please help po

Status
Not open for further replies.
Pa test sir. para madaming kang ma pag aralan.

Code:
import java.util.Scanner;

public class ProjectMo {

  public static void main(String[] args) {
 
  //input data.
  Scanner input = new Scanner (System.in);
 
  //storage for candidates
  String [] names = new String [3];

  //storage for votes
  int [] votes = new int [3];
 
  //storage for total number of votes
  double total = 0;
 
  // the current highest vote.
  double temp = votes[0];
 
  //Act as a signal for draw.
  int count = 0;
 
  // array index  of the winner.
  int WinnerIndex = 0;
 
 
  //set the value of names and votes for each candidates
  for(int i =0; i<names.length;i++){
    
    //Candidate name.
    System.out.print("Enter name: ");
    names[i] = input.nextLine();
    
    //Candidate votes.
    System.out.print("Enter votes: ");
    votes[i] = input.nextInt();
    
    //adding total votes.
    total += votes[i];
    
    //Preventing Scanner nextLine error.
    input.nextLine();
    
    //design purposes.
    System.out.println("------------------------");
  }
 
  //temp value set to the votes of the first candidate.
  temp = votes[0];
 
 
  //for printing candidates and thier votes
 
  for(int i = 0; i<names.length; i++){
    //Percent finder
    double percent = (votes[i]/total)*100;
 
    //print names
    System.out.print(names[i]+" ");
    
    //print votes.
    System.out.print(votes[i]+" ");
 
    //print percent(2 decimals value).
    System.out.printf("%.2f",percent);
    
    //for line/spacing.
    System.out.println();
  }
 
  //finding the winner
  for(int i = 0; i<names.length; i++){
 
    //if current votes is > the current highest vote.
   if(votes[i]>temp){
 
    //update index.
      WinnerIndex = i;
      
    //update for the current highest vote.
      temp = votes[i];
      
    }else if(votes[i] == temp){
     //coinditon for draw
    
      //update count.
      count++;
  
      /*
       kapag ang count ay equals sa number of candidates
       ibig sabihin silang lahat ay mayroong same number
       of votes.
      */
      
      
    }
    
  }
 
  //print total.
  System.out.println("Total: "+(int)total);
 
  //Condition Winner or draw.
  if(count == names.length){
  System.out.println("Winner: It's a draw");
  }else{
  System.out.println("Winner is: "+names[WinnerIndex]);
  }
 
 
  /*
    Sample lang to sir. pag aralan mo nalang 🙂.
    baka may logical error test mo nalang.
    paki double check sa percentage baka my logical error
    dun. Nag copy lang ako sa Google ng formula Hahaha.
  */
 
  }

}
 
Ginawa ko to sa AIDE with Android Tablet. Sira kasi laptop ko. Gawa ka ng class na Candidate. Hindi ko natapos at inayos tinamad kasi hehehe.
 

Attachments

  • Screenshot_20200604-154353.webp
    Screenshot_20200604-154353.webp
    90.2 KB · Views: 12
  • Screenshot_20200604-154334.webp
    Screenshot_20200604-154334.webp
    79.7 KB · Views: 13
  • Screenshot_20200604-154251.webp
    Screenshot_20200604-154251.webp
    56.5 KB · Views: 14
  • Screenshot_20200604-154235.webp
    Screenshot_20200604-154235.webp
    57.3 KB · Views: 13
  • Screenshot_20200604-154219.webp
    Screenshot_20200604-154219.webp
    63.5 KB · Views: 13
Status
Not open for further replies.

About this Thread

  • 31
    Replies
  • 1K
    Views
  • 10
    Participants
Last reply from:
noname18

Online now

Members online
841
Guests online
1,478
Total visitors
2,319

Forum statistics

Threads
2,277,334
Posts
28,975,868
Members
1,229,844
Latest member
branelzzz
Back
Top