简体   繁体   English

While&for循环和JOptionPane数据输入连接到for循环

[英]While & for loop and JOptionPane data input connected to the for loop

After reading everything in my books that I thought was relevant and none of it helped I'm writing here. 在阅读了我认为与之相关的所有书籍后,没有任何内容对我在这里的写作有所帮助。 I'm a complete novice in Java and I have to solve the following problem: I have to make a DialogProgram based on Tennis. 我是Java的完全新手,我必须解决以下问题:我必须制作一个基于Tennis的DialogProgram The program has to ask the user "who got the point" and the user has to type in "A" or "B" and that has to be executed until the game is won by either A or B. In the code I have put my questions as notes. 该程序必须询问用户“谁知道了这一点”,并且用户必须键入“ A”或“ B”,并且必须执行该操作,直到游戏被A或B获胜为止。在我输入的代码中我的问题作为笔记。 I hope that way they will make the most sense. 我希望这样他们将最有意义。

import java.awt.Color;
import acm.program.*;
import acm.graphics.*;
import javax.swing.JOptionPane;

public class A3 extends DialogProgram
{
  public void run ()
  { 
    JOptionPane.showMessageDialog(null, "Playing: Who got the point?");

    int A = 0;
    int B = 0; 
    int C = 0;

    /*
     * How do I make a loop to execute the JOptionPane until the if or else statement
     * in the for loop is achieved?
     * I think it should be with a while loop but how do I tell it to ask for input 
     * until the for loop is achieved? 
     */
    while ()
    {
      /*
       * I need the JOptionPane to take in only A or B as an answer each time it pops out.
       * How do I tell the JOptionPane to do that?
       * How to connect the input of A or B in the JOptionPane to the for loop so that it
       * counts the times A is inputed and the times B is inputed
       */
      String input = JOptionPane.showInputDialog("Enter A or B");

      for (C = 0; A >= 4 && B <= A-2; B++) 
      {
        if (A >= 4 && B <= A-2)
        {
          // In this case A wins
          JOptionPane.showMessageDialog(null,"Player A Wins!");
        }
        else if (B >= 4 && A <= B-2)
        {
          // In this case B wins
          JOptionPane.showMessageDialog(null,"Player B wins!");
        }
      }
    }
  }
}

First off, some general Java things (I completely understand that you're just learning): 首先,介绍一些Java常规知识(我完全理解您只是在学习):

  • "Notes" are called "comments" “注释”称为“注释”
  • variables (like A, B, and C) should start with a lowercase letter (like a, b, and c). 变量(例如A,B和C)应以小写字母(例如a,b和c)开头。

As for your questions, I don't exactly understand what int C is doing. 至于您的问题,我不太了解int C在做什么。 For your while loop, I would put two conditions in it. 对于您的while循环,我将添加两个条件。 Check if a == desiredScore and check if b == desiredScore . 检查a == desiredScore并检查b == desiredScore desiredScore should probably be its own variable and looks like 4 currently. desiredScore可能应该是它自己的变量,目前看起来像4。 After getting input (stored in your input String), you should do a couple checks on it. 获取输入(存储在input字符串中)后,您应该对其进行一些检查。 String has a bunch of member functions that you will find very useful. 字符串有很多成员函数,您会发现它们非常有用。 Especially check out .equalsIgnoreCase . 尤其要检查.equalsIgnoreCase If the input doesn't match what you want, print a message saying so and don't update any variables (the loop will just continue). 如果输入与您想要的不匹配,请打印一条消息,说明您不要更新任何变量(循环将继续进行)。

After the loop exits, I would check if a == desiredScore and print a message accordingly. 循环退出后,我将检查是否a == desiredScore并相应地打印一条消息。 And then check if b == desiredScore and print a message accordindly. 然后检查b == desiredScore并打印一条消息。

Basically what supersam654 said with some additions. 基本上, supersam654说了些什么。 You'll actually have to increase your integer variables, when the user picks either player, Java doesn't magically keep score for you. 实际上,您必须增加整数变量,当用户选择任一玩家时,Java不会神奇地为您保留得分。 Also, your for loop makes no sense, since in it B always get a point. 另外,您的for循环没有意义,因为在其中B总是得到一个点。

Here's an example: 这是一个例子:

public void run(int minimumScoreToWin) {
  int a = 0;
  int b = 0;

  JOptionPane.showMessageDialog(null, "Playing a set of tennis.");

  while (true) { // This loop never ends on its own.
    JOptionPane.showMessageDialog(null, "Score is A: " + a + ", B: " + b +
                                        ". Who got the next point?");
    String input = JOptionPane.showInputDialog("Enter A or B");

    if (input.equalsIgnoreCase("A") {
      a++;
    } else if (input.equalsIgnoreCase("B") {
      b++;
    } else {
      JOptionPane.showMessageDialog(null, "Cannot compute, answer A or B.");
      continue; // Skip to next iteration of loop.
    }

    if (a >= minimumScoreToWin && b <= a-2) {
      JOptionPane.showMessageDialog(null,"Player A Wins!");
      break; // End loop.
    } else if (b >= minimumScoreToWin && a <= b-2) {
      JOptionPane.showMessageDialog(null,"Player B Wins!");
      break; // End loop.
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM