【发布时间】: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