2011-07-19 52 views
-2

我開始學習編程,我在java中做了一個簡單的代碼,添加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

你的問題是? – bezmax

+0

Max是正確的 - 您需要向我們提供更多關於您想知道的信息。你認爲什麼是代碼錯誤,你認爲我們可以提供什麼幫助? – Bringer128

+0

我想首先猜測:你的意思是你必須將代碼分解成方法,而不是一切都在主要? – LeleDumbo

回答

1

你的意思是:

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

好吧...我知道...但對我來說不簡單//用真正的代碼 – user779848

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

    } 
} 
+0

重新佈置// blah // blah非常感謝我欣賞它 – user779848