将java方法添加到代码中

时间:2011-07-19 06:21:49

标签: java function methods programming-languages

我开始学习编程,我在java中编写了一个简单的代码,

是一场比赛,每个参赛者都会咬一口苹果,所以被咬的参赛者更有重量!

但!!我需要用java方法,函数添加所有代码......你知道

请运行代码,让您了解更多

任何帮助?非常感谢!

import java.io.*;

   class reality_show_methods{

public static void main(String[] args)throws java.io.IOException{

    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    PrintStream out = System.out;

    // VARIABLES
    int     counterParticipants = 1, numPart, numBoc;

    double  weightBoc, weightBocTotalMayor = 0;

    String  namePart, nameParticipantWinner = "";                                                                                                                     

    // SETUP
    out.print("Number of Participants ......................... ");
    numPart = Integer.parseInt(in.readLine());

    out.print("Number of Participants Bites: ....... ");
    numBoc = Integer.parseInt(in.readLine());

    // START
    while (counterParticipants <= numPart) {

        out.print("\nParticipant Name #" + counterParticipants + " ...................... ");
        namePart = in.readLine();

        int countBoc = 1;                                                     
        double weightBocTotal = 0;                                               

        while (countBoc <= numBoc) {

            out.print("Bite weight #" + countBoc + " of the Participant " + namePart + ": ");
            weightBoc = Double.parseDouble(in.readLine());

            weightBocTotal = weightBocTotal + weightBoc;

            countBoc++;
        }

        if (weightBocTotalMayor < weightBocTotal) {                                
            weightBocTotalMayor = weightBocTotal;
            nameParticipantWinner = namePart;
    }

    counterParticipants++;
}

// SHOW WINNER
out.println("\nParticipant Winner: ................... " + nameParticipantWinner + " with Total Weight: " + weightBocTotalMayor);

} 

     }

2 个答案:

答案 0 :(得分:1)

你的意思是:

public static void myFunction()
{
    // blah blah
}

答案 1 :(得分:0)

我想这会对你有帮助。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class BiteContest {

    private class Participant{
        private String name;
        private double biteWeight;

        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public double getBiteWeight() {
            return biteWeight;
        }
        public void setBiteWeight(double biteWeight) {
            this.biteWeight = biteWeight;
        }
    }

    ArrayList<Participant> participants = new ArrayList<Participant>();

    int numBoc;

    BufferedReader in;
    PrintStream out;

    BiteContest(){
        in = new BufferedReader(new InputStreamReader(System.in));
        out = System.out;
    }
    void addParticepents() throws IOException
    {
        boolean noMoreParticipants = false;

        while( !noMoreParticipants )
        {
            out.print("Participant Name : ");
            String name = in.readLine();

            Participant participant = new Participant();
            participant.setName(name);
            participant.setBiteWeight(0);

            participants.add( participant );

            out.print("Want to add more participants [Y/N]: ");
            String input = in.readLine();
            //Get first character.
            input = input.substring(0,1);

            noMoreParticipants = "y".equalsIgnoreCase(input)?false:true;
        }
    }
    void takeBiteCount() throws NumberFormatException, IOException {
        out.print("\nNumber of Participants Bites : ");
        numBoc = Integer.parseInt(in.readLine());
    }
    void contest() throws NumberFormatException, IOException
    {
        out.print( "\n\n------CONTEST STARTS------" );
        for( Participant participant : participants )
        {
            out.print( "\n\n------------" );
            out.print( "\nTaking Bites for " + participant.getName() + "\n" );
            for( int i = 0; i < numBoc; i++ )
            {
                out.print("Bite weight #" + (i+1) + " of the Participant " + participant.getName() + " : ");
                double weightBoc = Double.parseDouble(in.readLine());
                participant.setBiteWeight( participant.getBiteWeight() + weightBoc );
            }
        }
    }
    String getTheWinner(){
        Collections.sort( participants, new Comparator<Participant>() {
            public int compare(Participant o1, Participant o2) {
                return (int)(o2.getBiteWeight() - o1.getBiteWeight());
            }
        } );
        // Client at the top will be the winner. 
        return participants.get(0).getName();
    }

    public static void main(String[] args) {
        BiteContest contest = new BiteContest();

        try {
            contest.addParticepents();
            contest.takeBiteCount();
            contest.contest();
            String winner = contest.getTheWinner();

            System.out.println( "\n\n-------------------------\n" +
                    "The winner is : " + winner );

        } catch (IOException e) {
            e.printStackTrace();
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

    }
}