【问题标题】:I am a beginner coder and i'm trying to code a tic-tac-toe game but i'm getting errors i dont understand我是一名初学者编码器,我正在尝试编写井字游戏,但我遇到了我不明白的错误
【发布时间】:2020-02-03 07:13:09
【问题描述】:

我得到的错误是“找不到符号”。在“tic”之后的时期 p1L = tic.nextChar();

,并且“不能取消引用”也在“p1L”之后的句点上 p1L = p1L.equalsIgnoreCase(p1L);

我不确定这意味着什么或如何修复它。

我尝试按照一些同学的建议重新定义循环中的扫描仪,但它似乎不起作用或成为问题

Scanner tic = new Scanner(System.in);
Scanner tac = new Scanner(System.in);

for (int i = 0; wins || i == 9;i++) {
  tic = new Scanner(System.in);
  tac = new Scanner(System.in);

  table = line1+"/n"+A+"/n"+line2+"/n"+B+"/n"+line3+"/n"+C;

  System.out.println(table);//show table

  System.out.println("Enter Line A,B, or C for X (Player1)");
  p1L = tic.nextChar();
  p1L = p1L.equalsIgnoreCase(p1L);
  System.out.println ("Enter Row 1,2, or 3 for X (Player1)");
  p1R = tic.nextInt();

我希望输出以井字游戏表开头,然后是我的输入请求。例如 "为 X (Player1) 输入 A、B 或 C 行"

【问题讨论】:

  • 虽然这不会影响编译,但您不应该创建两个 Scanner 对象。他们会互相干扰。只创建一个 Scanner。
  • 更不用说22个了。

标签: java compiler-errors dereference


【解决方案1】:

例如:

Scanner tic = new Scanner(System.in); // use Standard Input for reading data (when the user writes to console)
Scanner tac = new Scanner(System.in); // delete because you have a scanner

for (int i = 0; wins || i == 9;i++) {
  tic = new Scanner(System.in); // delete because you have a Scanner
  tac = new Scanner(System.in); // delete because you have a Scanner

  table = line1+"/n"+A+"/n"+line2+"/n"+B+"/n"+line3+"/n"+C;

  System.out.println(table);//show table

  System.out.println("Enter Line A,B, or C for X (Player1)");
  p1L = tic.nextChar(); // delete, because method does not exist,
  p1L = p1L.equalsIgnoreCase(p1L); // delete, because method exist only data type String (text)
  p1L = tic.nextLine(); // read text
  p1L = p1L.toUpperCase() // make for "a" -> "A", for "A" -> "A"
  System.out.println ("Enter Row 1,2, or 3 for X (Player1)");
  p1R = tic.nextInt();

【讨论】:

    【解决方案2】:

    Scanner 对象中没有名为 nextChar() 的方法。您是说 nextLine() 吗?

    而且我不知道 p1L 中的对象类型。但我能说的是,equalsIgnoreCase() 不是那种无所不能的方法。

    参考:https://www.w3schools.com/java/java_user_input.asphttps://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

    【讨论】:

      猜你喜欢
      • 2023-02-21
      • 1970-01-01
      • 2023-02-07
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 2021-07-03
      • 2020-08-07
      • 1970-01-01
      相关资源
      最近更新 更多