【问题标题】:add java methods to code [closed]将java方法添加到代码中[关闭]
【发布时间】:2011-07-19 06:21:49
【问题描述】:

我开始学习编程,我用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);

} 

     }

【问题讨论】:

  • Max 是对的 - 您需要向我们提供有关您想知道的更多信息。您认为代码有什么问题,您认为我们可以提供哪些帮助?
  • 我想先猜一下:你的意思是你必须把代码分成方法而不是把所有东西都放在 main 里吗?
  • @LeleDumbo,是的,我需要在方法中分离我的代码,我的意思是做同样的!我这样做但使用方法,我认为这对专家来说很容易,但我有一个月的时间开始编程
  • 将一些代码块隔离到方法中并不能清楚地说明代码。我想您可以想象所有评论部分都可以被隔离到它们自己的方法(InitalizeVariables()、SetupGame()...),但是添加一个简单的 Participant 对象会更好。那时没有提到对象?
  • 这不是一个“为我做作业”的网站。并在发布之前将您的代码减少到最低限度!

标签: java function methods programming-languages


【解决方案1】:

你的意思是:

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

【讨论】:

  • 嗯...我知道...但对我来说并不简单用真实代码重新替换 // blah // blah
【解决方案2】:

我想这会对你有所帮助。

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();
        }

    }
}

【讨论】:

  • 非常感谢我很感激
猜你喜欢
  • 2015-03-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-02
  • 1970-01-01
  • 1970-01-01
  • 2021-10-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多