如何使用其他方法打印数组?

时间:2019-11-25 07:18:01

标签: java arrays loops joptionpane

我目前在编程领域还很陌生,想知道您是否能帮助我。我目前正在制作一个只希望使用joptionpane成为百万富翁游戏的人,感谢这个论坛,我已经解决了很多问题。现在,我的问题是我想选择救生索,想知道是否可以帮助我。

我想使用生命线,例如打电话给朋友并问听众,我想在使用生命线时显示当前的问题。这是我到目前为止的代码。

打包game4;

import java.util.Random;
import javax.swing.JOptionPane;

公共类Game4 {

public static void main(String[] args) 
{
    startGame();
}
public static void startGame()
{
// declare the variables first

boolean rep1 = true, rep2 = true, rep3 = true, rep4 = true, rep5 = true;
        cont = true;
//easy round
String  q1 = ". "
            + "\n[A] a"//answer
            + "\n[B] "
            + "\n[C] "
            + "\n[D] ",
        q2 = ".. "
            + "\n[A] "
            + "\n[B] "
            + "\n[C] "
            + "\n[D] d",//answer
        q3 = "..."
            + "\n[A] a"//answer
            + "\n[B] "
            + "\n[C] "
            + "\n[D] ",
        q4 = ".... "
            + "\n[A] "
            + "\n[B] b"//answer
            + "\n[C] "
            + "\n[D] ",
        q5 = "..... "
            + "\n[A] "
            + "\n[B] "
            + "\n[C] c"//answer
            + "\n[D] ";

String questionsEasy[] = new String[] { q1, q2, q3, q4, q5};

String input, ready;

int iChoice = JOptionPane.YES_NO_OPTION;
int c = 0;

shuffle1(questionsEasy); // here we do the shuffle

for (String easy : questionsEasy) 
{
    c++;

    if(easy.equals(q1))
    {
        while(rep1)
        {
            input = JOptionPane.showInputDialog("Question No. "+c+"\n\n"+easy
                                                +"\n[L] Lifeline"
                                                +"\n\nChoose from (A/B/C/D/L)");
            switch(input.toLowerCase())
            {
                case "a":
                {
                    iChoice = JOptionPane.showConfirmDialog (null,"is this your final answer?","continue",iChoice);

                    if(iChoice == JOptionPane.YES_OPTION) 
                    {
                        rep1 = false;
                        JOptionPane.showMessageDialog(null,"You got the right answer!");
                    }
                    else
                    {
                        rep1 = true;
                    }
                }
                break;
                case "b":
                case "c":
                case "d":
                {
                    wrongAnswer();
                }
                break;
                default:
                {
                    JOptionPane.showMessageDialog(null,"Invalid Input!!!");
                    rep1 = true;
                }
                case "l":
                {
                    lifeline();
                }
                break;
            }
        }
    }
    if(easy.equals(q2))
    {
        while(rep2)
        {
            input = JOptionPane.showInputDialog("Question No. "+c+"\n\n"+easy
                                                +"\n[L] Lifeline"
                                                +"\n\nChoose from (A/B/C/D/L)");
            switch(input.toLowerCase())
            {
                case "d":
                {
                    iChoice = JOptionPane.showConfirmDialog (null,"is this your final answer?","continue",iChoice);

                    if(iChoice == JOptionPane.YES_OPTION) 
                    {
                        rep2 = false;
                        JOptionPane.showMessageDialog(null,"You got the right answer!");
                    }
                    else
                    {
                        rep2 = true;
                    }
                }
                break;
                case "b":
                case "c":
                case "a":
                {
                    wrongAnswer();
                }
                break;
                default:
                {
                    JOptionPane.showMessageDialog(null,"Invalid Input!!!");
                    rep2 = true;
                }
                break;
            }
        }
    }
    if(easy.equals(q3))
    {
        while(rep3)
        {
            input = JOptionPane.showInputDialog("Question No. "+c+"\n\n"+easy
                                                +"\n[L] Lifeline"
                                                +"\n\nChoose from (A/B/C/D/L)");
            switch(input.toLowerCase())
            {
                case "a":
                {
                    iChoice = JOptionPane.showConfirmDialog (null,"is this your final answer?","continue",iChoice);

                    if(iChoice == JOptionPane.YES_OPTION) 
                    {
                        rep3 = false;
                        JOptionPane.showMessageDialog(null,"You got the right answer!");
                    }
                    else
                    {
                        rep3 = true;
                    }
                }
                break;
                case "b":
                case "c":
                case "d":
                {
                    wrongAnswer();
                }
                break;
                default:
                {
                    JOptionPane.showMessageDialog(null,"Invalid Input!!!");
                    rep3 = true;
                }
                break;
            }
        }
    }
    if(easy.equals(q4))
    {
        while(rep4)
        {
            input = JOptionPane.showInputDialog("Question No. "+c+"\n\n"+easy
                                                +"\n[L] Lifeline"
                                                +"\n\nChoose from (A/B/C/D/L)");
            switch(input.toLowerCase())
            {
                case "b":
                {
                    iChoice = JOptionPane.showConfirmDialog (null,"is this your final answer?","continue",iChoice);

                    if(iChoice == JOptionPane.YES_OPTION) 
                    {
                        rep4 = false;
                        JOptionPane.showMessageDialog(null,"You got the right answer!");
                    }
                    else
                    {
                        rep4 = true;
                    }
                }
                break;
                case "a":
                case "c":
                case "d":
                {
                    wrongAnswer();
                }
                break;
                default:
                {
                    JOptionPane.showMessageDialog(null,"Invalid Input!!!");
                    rep4 = true;
                }
                break;
            }
        }
    }
    if(easy.equals(q5))
    {
        while(rep5)
        {
            input = JOptionPane.showInputDialog("Question No. "+c+"\n\n"+easy
                                                +"\n[L] Lifeline"
                                                +"\n\nChoose from (A/B/C/D/L)");
            switch(input.toLowerCase())
            {
                case "c":
                {
                    iChoice = JOptionPane.showConfirmDialog (null,"is this your final answer?","continue",iChoice);

                    if(iChoice == JOptionPane.YES_OPTION) 
                    {
                        rep5 = false;
                        JOptionPane.showMessageDialog(null,"You got the right answer!");
                    }
                    else
                    {
                        rep5 = true;
                    }
                }
                break;
                case "b":
                case "a":
                case "d":
                {
                    wrongAnswer();
                }
                break;
                default:
                {
                    JOptionPane.showMessageDialog(null,"Invalid Input!!!");
                    rep5 = true;
                }
                break;
            }
        }
    }
    if (c == 1)
    {
        JOptionPane.showMessageDialog(null,"You have won Php 100.00!!"
                        +"\n\nClick \"OK\" to proceed to the next question");
    }
    if (c == 2)
    {
        JOptionPane.showMessageDialog(null,"You have won Php 500.00!!"
                        +"\n\nClick \"OK\" to proceed to the next question");
    }
    if (c == 3)
    {
        JOptionPane.showMessageDialog(null,"You have won Php 1,000.00!!"
                        +"\n\nClick \"OK\" to proceed to the next question");
    }
    if (c == 4)
    {
        break;
    }
}

iChoice = JOptionPane.showConfirmDialog (null,"You now have a guaranteed Php 5,000.00."
                                        + "\nDo you still want to play?","continue",iChoice);
if(iChoice == JOptionPane.YES_OPTION)
{
    JOptionPane.showMessageDialog(null,"        LET'S PLAY,"
                                        + "\nWHO WANTS TO BE A MILLIONAIRE!!!");
}
else
{
    System.exit(0);
}

JOptionPane.showMessageDialog(null,"        YOU JUST WON "
                                + "\n\"ONE MILLION PESOS\"!!!!!"
                                + "");
}

这是一个错误的答案

public static void wrongAnswer()
{
    int iChoice;

    iChoice = JOptionPane.YES_NO_OPTION;
    iChoice = JOptionPane.showConfirmDialog (null,"is this your final answer?","continue",iChoice);

    if (iChoice == JOptionPane.YES_OPTION)
    {
        JOptionPane.showMessageDialog(null,"you got the wrong answer!");

        iChoice = JOptionPane.YES_NO_OPTION;
        iChoice = JOptionPane.showConfirmDialog (null,"Do you want to play again?","continue",iChoice);

        if(iChoice == JOptionPane.NO_OPTION)
        {
            JOptionPane.showMessageDialog(null,"Thank you for playing this game!");
            System.exit(0);
        }
        else
        {
            startGame();
        }
    }       
}

这是改组发生的地方。我在这个论坛的某个地方复制了它。

public static void shuffle1(String[] questionsEasy) 
{
    Random random = new Random();

    for (int i = 0; i < questionsEasy.length; i++) {
        int newIndex = random.nextInt(questionsEasy.length - 1);
        swap1(questionsEasy, i, newIndex);
    }
}
public static void swap1(String[] questionsEasy, int oldIndex, int newIndex) 
{
    String temp = questionsEasy[oldIndex];
    questionsEasy[oldIndex] = questionsEasy[newIndex];
    questionsEasy[newIndex] = temp;
}

这就是生命线

public static void lifeline()
{
    String input = JOptionPane.showInputDialog("You have chosen to use a Lifeline, "
                                                                    + "\nPlease select which Lifeline you would like to use\n"
                                                                    + "\n[F] Call a Friend"
                                                                    + "\n[A] Ask the Audience\n"
                                                                    + "\n[D] Do not use a lifeline\n"
                                                                    + "\nChoose from (F/A/D)");
    switch(input.toLowerCase())
    {
        case "f":
        {
            JOptionPane.showMessageDialog(null,"You have chosen Call a Friend");
            JOptionPane.showMessageDialog(null,"blablabla"/*the current question will print right in here*/);
        }
        break;
    }

}

如果您可以帮助我,对我的项目将是一个巨大的帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

看起来像变量easy是这里的问题:

shuffle1(questionsEasy); // here we do the shuffle

for (String easy : questionsEasy) 
{
    c++;

因此,只需在此处轻松传递函数的生命线即可:

case "l":
{
    lifeline(easy);
}

并更改函数def以包含输入,即:

public static void lifeline(String easy)
{
    String input = JOptionPane.showInputDialog("You have chosen to use a Lifeline, "
                                                                    + "\nPlease select which Lifeline you would like to use\n"
                                                                    + "\n[F] Call a Friend"
                                                                    + "\n[A] Ask the Audience\n"
                                                                    + "\n[D] Do not use a lifeline\n"
                                                                    + "\nChoose from (F/A/D)");
    switch(input.toLowerCase())
    {
        case "f":
        {
            JOptionPane.showMessageDialog(null,"You have chosen Call a Friend");
            JOptionPane.showMessageDialog(null,easy/*the current question will print right in here*/);
        }
        break;
    }

}

然后您可以使用简单的问题,并在此功能中将其打印出所需的位置