What's new

Closed Java input, please help po

Status
Not open for further replies.

DarkResurrection

Eternal Poster
Established
Joined
May 28, 2016
Posts
780
Solutions
1
Reaction
128
Points
308
Good day ka-PHC!, Hingi po sana ako ng tulong kung pano ko maso solve itong pinapagawa samin mga sir.
101573015_602435927033449_2903792195863576576_n.jpg

Ito po yung java code ko po, kaso kapag nagenter na po ako kung ilang entries ang gusto ko, for example 3, nage exceed po sya sa 3, tuloy tuloy na po sya.
JavaScript:
import java.util.*;
public class Activity_1{
   public static void main(String args[]){
      Scanner dataIn = new Scanner(System.in);
      
      System.out.print("Enter number of entries: ");
        int noOfEntry = Integer.parseInt(dataIn.nextLine());
      
      String lastname[] = new String[noOfEntry];
      int votes[] = new int[100];
        for (int i = 0; i < lastname.length; i++) {
         for (int j = 0; j < votes.length; j++) {
         System.out.println("--------------------------");
            System.out.print("Enter candidate's name: ");
                lastname[i] = dataIn.nextLine();   
         System.out.print("Enter number of votes received: ");
                votes[j] = Integer.parseInt(dataIn.nextLine());   
           }
        }
      System.out.println("Candidate\t Votes Received");
      for (int i = 0; i < lastname.length; i++) {
            System.out.print(lastname[i] + "\n");              
        }
 
   }
}

Pa help naman po ako mga sir.😢
 

Attachments

maling mali mga codes hahaha

taking too much memory as well.

dapat yung votes mo is hindi naka set sa 100. dapat sa no of entries - 1,

set mo String[] lastname = new String[noOfEntry - 1]

and yung for loop sobrang mali. kada input mo ng candidate at votes. na ooverride ang votes ng 1st camdidate.

napaka easy ng program na yan pwede kita matulungan
 
Last edited:
isang for loop lng hindi nested. same lng yung value sa votes at lastname
pag nag input ka 3 entries
600 times ka mag eenter ng data
 
Last edited:
Kailangan mo ng tatlong container na array para sa Names, No of Votes at Percentage.
Madali lang to ts kayang kaya mo yan
 
int votes[] = new int[noOfEntry];
for (int j = 0; j < noOfEntry; j++) {
System.out.println("--------------------------");
System.out.print("Enter candidate's name: ");
lastname = dataIn.nextLine();
System.out.print("Enter number of votes received: ");
votes[j] = Integer.parseInt(dataIn.nextLine());
}
 
Gawin mong Object Oriented yang program mo, wag mong gawing procedural mamamatay ka ng maaga pag ganyan pa din ginawa mo
 
Suggestion ko is mag while loop ka wag ka gagamit ng for loop hindi akma sa problem mo
 
pa try na lang ts, sana pag aralan mo to ts :)
Java:
import java.util.Scanner;
public class Main
{
    static void printSeperator(){
        System.out.println("_____________________________________");
    }
    static int getHighestVotes(int[] noVote){
        int highest = 0;
        for(int i = 0; i < noVote.length; i++){
            if(highest < noVote[i])
                highest = noVote[i];
        }
       
        int highestIndex = 0;
        for(int i = 0 ; i < noVote.length; i++){
            if(noVote[i] == highest)
            return i;
        }
        return -1;
    }
   
    static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) {
    int[] noVote;
    String[] listOfNames;
    float[] Percentage;
    System.out.print("Enter Number of Entries: ");
    int noEntry = sc.nextInt();
    noVote = new int[noEntry];
    listOfNames = new String[noEntry];
    Percentage = new float[noEntry];
    printSeperator();
   
    //Ask the user to input the data.
    for(int i = 0 ; i < noEntry; i++){
        System.out.print("Enter candidate's name: ");
        listOfNames[i] = sc.next();
        System.out.print("Enter number of Votes recieved: ");
        noVote[i] = sc.nextInt();
        printSeperator();
        }
    //Get the total Votes
    int totalVotes = 0;
    for(int i = 0; i < noEntry; i++)
        totalVotes += noVote[i];
    //Calculate the Percentage of every Candidate.
    for(int i = 0; i < noEntry; i++){
        Percentage[i] = ((float)noVote[i] / totalVotes) * 100;
    }
    //Finally Print the data.
    System.out.println("Candidate\tVotes Received\t%of Total Votes");
    for(int i = 0; i < noEntry; i++)
        System.out.printf(listOfNames[i] + "\t\t" + noVote[i] + "\t\t%.2f%n",Percentage[i]);
        printSeperator();
    //Now get the Winner
        String winner = listOfNames[getHighestVotes(noVote)];
        System.out.println("Total: "+totalVotes);
        System.out.println("Winner: "+winner);
    }
}
 
Last edited:
pa try na lang ts, sana pag aralan mo to ts :)
Java:
import java.util.Scanner;
public class Main
{
    static void printSeperator(){
        System.out.println("_____________________________________");
    }
    static int getHighestVotes(int[] noVote){
        int highest = 0;
        for(int i = 0; i < noVote.length; i++){
            if(highest < noVote[i])
                highest = noVote[i];
        }
       
        int highestIndex = 0;
        for(int i = 0 ; i < noVote.length; i++){
            if(noVote[i] == highest)
            return i;
        }
        return -1;
    }
   
    static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) {
    int[] noVote;
    String[] listOfNames;
    float[] Percentage;
    System.out.print("Enter Number of Entries: ");
    int noEntry = sc.nextInt();
    noVote = new int[noEntry];
    listOfNames = new String[noEntry];
    Percentage = new float[noEntry];
    printSeperator();
   
    //Ask the user to input the data.
    for(int i = 0 ; i < noEntry; i++){
        System.out.print("Enter candidate's name: ");
        listOfNames[i] = sc.next();
        System.out.print("Enter number of Votes recieved: ");
        noVote[i] = sc.nextInt();
        printSeperator();
        }
    //Get the total Votes
    int totalVotes = 0;
    for(int i = 0; i < noEntry; i++)
        totalVotes += noVote[i];
    //Calculate the Percentage of every Candidate.
    for(int i = 0; i < noEntry; i++){
        Percentage[i] = ((float)noVote[i] / totalVotes) * 100;
    }
    //Finally Print the data.
    System.out.println("Candidate\tVotes Received\t%of Total Votes");
    for(int i = 0; i < noEntry; i++)
        System.out.printf(listOfNames[i] + "\t\t" + noVote[i] + "\t\t%.2f%n",Percentage[i]);
        printSeperator();
    //Now get the Winner
        String winner = listOfNames[getHighestVotes(noVote)];
        System.out.println("Total: "+totalVotes);
        System.out.println("Winner: "+winner);
    }
}
ikaw na talaga kuya Arcturus ang master hehehehe.
 
Status
Not open for further replies.

Similar threads

Back
Top