簡體   English   中英

數組未正確記錄JOptionPane的輸入

[英]Array not recording input from JOptionPane correctly

因此,我想做的就是創建一個按鈕,該按鈕允許我的游戲玩家輸入他們想在比賽中競爭的玩家人數。 然后,他們輸入競爭者的名稱,這些競爭者的名稱將被記錄並存儲在數組中,以備后用。 我創建了一個playerList方法來檢查它是否正確記錄了名稱,但是它一直只返回一個名稱。 這使我相信,我的enterPlayers()方法中的for循環只會添加輸入到數組中的名字。 我不確定為什么會這樣,但是。 我打算以后使用這些名稱在玩家之間建立比賽,以提升比賽水平,因此這是我項目的重要組成部分。 如果有人可以幫助,將不勝感激。 謝謝,祝你有美好的一天!

這是其中涉及的代碼:

    public void playerList()
  {
      JFrame frame = new JFrame("The Competitors");
      JPanel panel = new JPanel();
      JButton returnToMenu = new JButton ("Return to menu");
      String competitors = "<html><h1><font size=25 color=red> Your competitors are:<br>";
      for (int i = 0; i<tournament.length; i++)
      {
          competitors+= (i+1) + ". " + tournament[i] + "<br></font></h1></html>";  
      }
      JLabel overText = new JLabel(competitors);
      returnToMenu.addActionListener(new ActionListener()
      {
          public void actionPerformed(ActionEvent event)
          {
                 sound11.play();
                 frame.dispose();
          }
      });
      panel.add(returnToMenu);
      panel.add(overText);
      frame.getContentPane().setBackground(Color.black);
      frame.add(overText, BorderLayout.NORTH);
      frame.add(panel);
      frame.setSize(800, 340);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
      sound13.loop();
  }

  public void enterPlayers()
  {
      String number = JOptionPane.showInputDialog("Enter number of players ");   
      int a = Integer.parseInt(number);
      numPlayers = a;
      JOptionPane.showMessageDialog(null, "Number of players = " + numPlayers);
      while (numPlayers>10 || numPlayers<2 || ((numPlayers>2 && numPlayers<=10) && numPlayers%2!=0))
      {
          if (numPlayers>10)
          {
              JOptionPane.showMessageDialog(null, "Enter no more then 10 players"); 
          }
          else if (numPlayers<2)
          {
              JOptionPane.showMessageDialog(null, "You must have more then 2 players"); 
          }
          else if ((numPlayers>2 && numPlayers<=10) && numPlayers%2!=0)
          {
              JOptionPane.showMessageDialog(null, "You must have an even number of players"); 
          }
          String number1 = JOptionPane.showInputDialog("Enter number of players ");  
          int b = Integer.parseInt(number1);
          numPlayers = b;
          JOptionPane.showMessageDialog(null, "Number of players = " + numPlayers);        
      }

      tournament = new String[numPlayers];

      for (int i = 0; i<numPlayers; i++)
      {
          String name = JOptionPane.showInputDialog("Enter Player " + (i+1) + " name: ");
          String player = "Player " + (i+1) + " is: " + name;
          JOptionPane.showMessageDialog(null, player);
          tournament[i] = name;
      }
  }

問題是您要關閉此行中每個播放器上的html:

for (int i = 0; i<tournament.length; i++)
{
    competitors += (i+1) + ". " + tournament[i] + "<br></font></h1></html>";  
}

您應該這樣做:

for (int i = 0; i<tournament.length; i++)
{
    competitors += (i+1) + ". " + tournament[i] + "<br>";  
}

competitors += "</font></h1></html>";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM