我们应该在我们的程序中使用方法,但是我很难将它们合并到它中。我的程序就像一个数学游戏,询问简单的加法和加法/减法问题。我希望所有的计算都在方法中,但我无法返回整个方程式。
在谈到我需要的方向时,我需要一些帮助,以便在我的程序中有效地使用方法,以及我应该在哪里进行计算!除了它自己的方法应该是什么以及主要方法应该是什么。
import javax.swing.*;
import java.io.*;
public class SummativeTwo {
public static void main (String[] args) throws IOException {
BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in)); {
Object[] options = {"Yes! I'm Excited!","Im kind of nervous..","Now Way! Ewww!"};
int n = JOptionPane.showOptionDialog(null, "Welcome Million Dollar Madness! "+ "Are you ready to play?","Welcome",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE,null,options,options[2]);
if (n == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(null,"Okie Dokie! Lets get started!", "Okie Dokie", JOptionPane.INFORMATION_MESSAGE);
}
if (n == JOptionPane.NO_OPTION) {
JOptionPane.showMessageDialog(null,"Aw thats okay! Im sure you'll have fun!", "It's okay", JOptionPane.INFORMATION_MESSAGE);
}
if (n == JOptionPane.CANCEL_OPTION) {
JOptionPane.showMessageDialog(null,"Well too bad so sad! You're going to have to play anyways!", "Too bad so sad", JOptionPane.INFORMATION_MESSAGE);
}
Object[] optionsA = {"Yes Please", "Nope! I'm good!"};
int x = JOptionPane.showOptionDialog(null,"Would you like to know the instructions for this game?","INSTRUCTIONS?", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,optionsA,optionsA[0]);
if (x == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(null,"There will be 3 levels in this game. As you go farther, the questions get harder and harder and each question \n. Each time you get a question right you will recieve a certain amount of money, which increases each time you pass each question. \n But be careful, because if you get 3 questions wrong in one level \n it's GAME OVER for you! Meaning you lose all your money. \n You may Cash Out at any point if you're not willing to risk losing the amount of money you have. Reach the end of \n the 3 levels and you take home all your money! \n GOOD LUCK!", "Instructions", JOptionPane.INFORMATION_MESSAGE);
}
if (x == JOptionPane.NO_OPTION) {
JOptionPane.showMessageDialog(null,"Suit Yourself! Lets Start!", "Your Choice", JOptionPane.INFORMATION_MESSAGE);
}
JOptionPane.showMessageDialog(null,"Welcome to Level: 1 " ," level 1", JOptionPane.INFORMATION_MESSAGE);
//LEVEL ONE
int wrong = 0;
int run = 1;
int questionnum = 0;
int amount = 0;
while (run <= 6 && wrong != 3)
{
run ++;
questionnum++;
int increment = 150;
amount += 150;
int numOne = additionOne ();
int numTwo = additionTwo ();
int answer = numOne + numTwo;
String useranswerA = JOptionPane.showInputDialog(null,"Question #" + questionnum + " is for: $" + increment + "\n" + numOne + " + " + numTwo + " = ?", "Question", JOptionPane.INFORMATION_MESSAGE);
int useranswer = Integer.parseInt(useranswerA);
if (useranswer != answer)
{
wrong ++;
JOptionPane.showMessageDialog(null,"You got the wrong answer! \n The correct answer is: " + answer + " \n Questions Wrong: " + wrong, "Wrong Answer", JOptionPane.INFORMATION_MESSAGE);
int y = JOptionPane.showOptionDialog(null,"CASH OUT with a total of $" + amount + "?","Cash Out?", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,optionsA,optionsA[0]);
if (y == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(null,"Thanks for Playing!", "Thank You!", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
if (y == JOptionPane.NO_OPTION) {}
}
else if (useranswer == answer)
{
JOptionPane.showMessageDialog(null,"Correct!", "Right Answer", JOptionPane.INFORMATION_MESSAGE);
int y = JOptionPane.showOptionDialog(null,"CASH OUT with a total of $" + amount + "?","Cash Out?", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,optionsA,optionsA[0]);
if (y == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(null,"Thanks for Playing!", "Thank You!", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
if (y == JOptionPane.NO_OPTION) {}
}
}
JOptionPane.showMessageDialog(null,"LEVEL ONE COMPLETE!", "LEVEL 1", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null,"Cash on Hand: $ " + amount, "Cash", JOptionPane.INFORMATION_MESSAGE);
//LEVEL TWO
JOptionPane.showMessageDialog(null,"Welcome to Level: 2 " ," level 2", JOptionPane.INFORMATION_MESSAGE);
int wrongB = 0;
int runB = 1;
int questionnumB = 0;
while (runB <= 8 && wrongB != 3)
{
runB ++;
questionnumB++;
amount += 250;
int increment = 250;
int numOne = additionOne ();
int numTwo = additionTwo ();
int numThree = numThree ();
int answer = numOne + numTwo - numThree;
String useranswerA = JOptionPane.showInputDialog(null,"Question #" + questionnum + " is for: $" + increment + "\n" + numOne + " + " + numTwo + " - " + numThree + " = ?", "Question", JOptionPane.INFORMATION_MESSAGE);
int useranswer = Integer.parseInt(useranswerA);
if (useranswer != answer)
{
wrong ++;
JOptionPane.showMessageDialog(null,"You got the wrong answer! \n The correct answer is: " + answer + " \n Questions Wrong: " + wrongB, "Wrong Answer", JOptionPane.INFORMATION_MESSAGE);
int z = JOptionPane.showOptionDialog(null,"CASH OUT with a total of $" + amount + "?","Cash Out?", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,optionsA,optionsA[0]);
if (z == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(null,"Thanks for Playing!", "Thank You!", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
if (z == JOptionPane.NO_OPTION) {}
}
else if (useranswer == answer)
{
JOptionPane.showMessageDialog(null,"Correct!", "Right Answer", JOptionPane.INFORMATION_MESSAGE);
int z = JOptionPane.showOptionDialog(null,"CASH OUT with a total of $" + amount + "?","Cash Out?", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,optionsA,optionsA[0]);
if (z == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(null,"Thanks for Playing!", "Thank You!", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
if (z == JOptionPane.NO_OPTION) {}
}
}
JOptionPane.showMessageDialog(null,"LEVEL TWO COMPLETE!", "LEVEL 2", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null,"Cash on Hand: $ " + amount, "Cash",JOptionPane.QUESTION_MESSAGE);
}
}
public static int additionOne ()
{
int x = (int)( Math.random () * 30);
return x;
}
public static int additionTwo ()
{
int x = (int)( Math.random () * 30);
return x;
}
public static int numThree ()
{
int x = (int) (Math.random () * 15);
return x;
}
}
答案 0 :(得分:1)
程序设计的一般经验法则:
在你的代码中,你有2个级别,每个级别都有相似之处。这可能是开始考虑将代码放入其自己的方法的好地方。然后,如果你想添加第3级,则不需要额外的代码。
答案 1 :(得分:0)
你的问题太模糊了,但我认为这会对你有所帮助。
您有三个功能:additionOne
,additionTwo
和numThree
非常相似。您可以将它们缩减为如下函数:
public static int myFunction(int multiplier)
{
int x = (int)(Math.random () * multiplier);
return x;
}
并使用它:
int numOne = myFunction(30);
int numTwo = myFunction(30);
int numThree = myFunction(15);
int answer = numOne + numTwo - numThree;
答案 2 :(得分:0)
编写方法的目的之一是减少重复代码。 在您的代码中,您似乎需要多次调用JOptionPange.showMessageDialog。因此,您可以编写一个方法,将所有消息显示代码放入其中,并将要显示的消息作为参数传递。
public void someMethod(String message, String anotherMessage){
JOptionPane.showMessageDialog(null,message , anotherMessage, JOptionPane.INFORMATION_MESSAGE);
// some more code to run
}
写完后,你需要在if条件下调用这个方法。
现在它看起来不会有用,但如果你想在显示消息ID后执行一些代码,那么你可以添加这个方法。
总是尝试尽可能简单地编写主要方法。试着从中调用其他方法。
答案 3 :(得分:0)
首先,拉出任何重复的内容。 所以对于2组块:
if (useranswer != answer)
和
else if (useranswer == answer)
我希望这些块的内容是一种方法,其中任何差异都是方法的参数 - 或者只是有一个布尔右/错参数并调整方法内的哪组字符串被调用。以哪种方式减少代码。如果在第1组和第3组以及第2组和第4组中使用相同的字符串,那么不要将它们作为参数发送4次(将它们声明为4次),只需将它们声明两次 - 这样就意味着在方法内部。
删除重复的代码之后,下一步将从较小的代码块(可能是10-20行)开始,通常在大括号内,最好是使用其他地方不需要的新变量。
删除较小的块后,您可能会发现可以删除新的“小”块。 1级和2级可以采用不同的方法。
理想情况下,只需尝试保持较小的方法,以便读者不必滚动,并且可以轻松查看方法的整个代码,在脑中运行它们,并知道它在做什么。
答案 4 :(得分:0)
1.级别和2.级别几乎完全相同。因此,将第一级提取到单独的方法将破坏下一级的代码。破碎的语法指导从第一级到第二级所需的信息。似乎只需要必要的数量。
遵循这种重构方式还表明原始的错误B和问题B未正确使用。我冒昧地假设两轮都有不同的错误和问号变量。
这种方法的问题在于,如果代码没有得到很好的理解而且没有测试就很脆弱,很可能会破坏它。但是我发现它在较小的重构步骤不明显的情况下是有效的。
一般来说,所有方法都应该清楚阅读。因为main方法是最顶级的方法,所以应该清楚地说明程序中发生的事情。方法应该处于相同的抽象层次并且非常短。
使用State-object传递信息,但在这种情况下,可以从runLevel返回简单数量,并作为参数传递给下一个。
我不知道是否要清理对话框的内容,但我采取了一些措施来减少重复的代码。整件事情可以更加精致。我希望这可以提供一些指导......
清洁代码(http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882)是关于重构和制作可读的方法和代码的非常好的书。
这是我的两分钱:
public class SummativeTwo {
public static void main(String[] args) throws IOException {
BufferedReader myInput = new BufferedReader(new InputStreamReader(System.in));
{
showYesNoCancelDialogWithFollowUp(
"Welcome",
"Welcome Million Dollar Madness! " + "Are you ready to play?",
new DialogOption("Yes! I'm Excited!", new InformationDialog("Okie Dokie! Lets get started!", "Okie Dokie")),
new DialogOption("Im kind of nervous..", new InformationDialog("Aw thats okay! Im sure you'll have fun!", "It's okay")),
new DialogOption("Now Way! Ewww!", new InformationDialog("Well too bad so sad! You're going to have to play anyways!", "Too bad so sad")));
showYesNoDialogWithFollowUp("INSTRUCTIONS?",
"Would you like to know the instructions for this game?",
new InformationDialog("There will be 3 levels in this game. As you go farther, the questions get harder and harder and each question \n. Each time you get a question right you will recieve a certain amount of money, which increases each time you pass each question. \n But be careful, because if you get 3 questions wrong in one level \n it's GAME OVER for you! Meaning you lose all your money. \n You may Cash Out at any point if you're not willing to risk losing the amount of money you have. Reach the end of \n the 3 levels and you take home all your money! \n GOOD LUCK!", "Instructions"),
new InformationDialog("Your Choice", "Suit Yourself! Lets Start!"));
showInformationMessage("Welcome to Level: 1 ", " level 1");
State state = new State();
runLevel1(state);
showInformationMessage("LEVEL ONE COMPLETE!", "LEVEL 1");
showInformationMessage("Cash on Hand: $ " + state.amount, "Cash");
showInformationMessage("Welcome to Level: 2 ", " level 2");
runLevel2(state);
showInformationMessage("LEVEL TWO COMPLETE!", "LEVEL 2");
JOptionPane.showMessageDialog(null, "Cash on Hand: $ " + state.amount, "Cash", JOptionPane.QUESTION_MESSAGE);
}
}
static class State {
int amount;
}
private static void runLevel1(State state) {
int maxRuns = 6;
int maxWrongs = 3;
int increment = 150;
runLevel(increment, maxRuns, maxWrongs, state);
}
private static void runLevel2(State state) {
int maxRuns = 8;
int maxWrongs = 3;
int increment = 150;
runLevel(increment, maxRuns, maxWrongs, state);
}
private static void runLevel(int increment, int maxRuns, int maxWrongs, State state) throws NumberFormatException, HeadlessException {
int run = 1;
int questionnum = 0;
int wrong = 0;
while (run <= maxRuns && wrong != maxWrongs) {
run++;
questionnum++;
state.amount += increment;
int numOne = additionOne();
int numTwo = additionTwo();
int answer = numOne + numTwo;
String useranswerA = JOptionPane.showInputDialog(null, "Question #" + questionnum + " is for: $" + increment + "\n" + numOne + " + " + numTwo + " = ?", "Question", JOptionPane.INFORMATION_MESSAGE);
int useranswer = Integer.parseInt(useranswerA);
if (useranswer != answer) {
wrong++;
showInformationMessage("You got the wrong answer! \n The correct answer is: " + answer + " \n Questions Wrong: " + wrong, "Wrong Answer");
askForCashOut(state.amount);
} else if (useranswer == answer) {
showInformationMessage("Correct!", "Right Answer");
askForCashOut(state.amount);
}
}
}
public static int additionOne() {
int x = (int) (Math.random() * 30);
return x;
}
public static int additionTwo() {
int x = (int) (Math.random() * 30);
return x;
}
public static int numThree() {
int x = (int) (Math.random() * 15);
return x;
}
private static void askForCashOut(int amount) throws HeadlessException {
if (askYesNo("Cash Out?", "CASH OUT with a total of $" + amount + "?")) {
showInformationMessage("Thanks for Playing!", "Thank You!");
System.exit(0);
}
}
private static void showYesNoCancelDialogWithFollowUp(String title, String text, DialogOption yes, DialogOption no, DialogOption cancel) {
DialogOption[] options = new DialogOption[]{yes, no, cancel};
int n = JOptionPane.showOptionDialog(null, text, title, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[2]);
options[n].dialog.show();
}
private static void showYesNoDialogWithFollowUp(String title, String text, InformationDialog afterYes, InformationDialog afterNo) {
if (askYesNo(text, title)) {
afterYes.show();
} else {
afterNo.show();
}
}
private static final String[] YES_NO_OPTIONS = new String[]{"Yes Please", "Nope! I'm good!"};
private static boolean askYesNo(String text, String title) throws HeadlessException {
int n = JOptionPane.showOptionDialog(null, text, title, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, YES_NO_OPTIONS, YES_NO_OPTIONS[0]);
return n == JOptionPane.YES_OPTION;
}
private static void showInformationMessage(String text, String title) throws HeadlessException {
JOptionPane.showMessageDialog(null, text, title, JOptionPane.INFORMATION_MESSAGE);
}
static class InformationDialog {
private String text;
private String title;
public InformationDialog(String text, String title) {
this.text = text;
this.title = title;
}
public void show() {
showInformationMessage(text, title);
}
}
static class DialogOption {
String text;
InformationDialog dialog;
public DialogOption(String text, InformationDialog dialog) {
this.text = text;
this.dialog = dialog;
}
@Override
public String toString() {
return text;
}
}
}