【问题标题】:How to use If statements with Boolean and to read user input如何使用带有布尔值的 If 语句并读取用户输入
【发布时间】:2018-11-05 10:33:11
【问题描述】:

我昨天写了一篇关于这个问题的帖子,但我的写作方式完全错误,所以我花了一夜和今天早上再次尝试,但仍然无法理解如何去做。

我想要做的是通过让用户输入两个坐标来测试我的IsInRoom 方法,我无法弄清楚如何让 IF 语句读取用户输入并且每次都使用r.IsInRoom错误提示“类型室中的方法 IsInRoom(Point) 不适用于参数 double。

我尝试了很多不同的方法来做到这一点,但每次我总是回到同一个地方。如果有人有任何很棒的想法,我对 java 来说真的很陌生,那么如果您能给我一些关于如何正确使用这些功能的指示,那就太好了?

我已对 If else 语句进行了更改,但出现此错误

   Exception in thread "main" java.lang.NullPointerException
    at CurtisBaldwin.buildingconsole.Room.isInRoom(Room.java:85)
   at aCurtisBaldwin.buildingconsole.Room.main(Room.java:115)

我认为这是因为我没有将用户输入分配给 if 语句?但我不确定你是怎么做到的

import java.awt.Point;
import java.io.Reader;
import javax.swing.JOptionPane;
import java.util.Random;

public class Room {
    private static int X; 
    private static int Y; 
    private static Point P; 
    private int[] coords;
    Random ranGen;

    public Room(String rstr) {
        StringSplitter S = new StringSplitter(rstr, " ");
        coords = S.getIntegers();
    }

    public Point getRandomPoint (Random ranGen) {
        return new Point(coords[0] +1 + ranGen.nextInt(coords[2] - coords[0] -2), (coords[1] +1 + ranGen.nextInt(coords[3] - coords[1] -2)));                                               
    }

    public String toString() {
        return "Room " + coords[0] + ", " + coords[1] + ", " + coords[2] + ", " + coords[3] + " Door " + coords[4] + ", " + coords[5];
    }

    public boolean IsInRoom(Point xy) {
        P = new Point (X,Y);

        return (xy.getX() > coords[0] && xy.getX() < coords[2] && xy.getY() > coords[1] && xy.getY() < coords[3]);
    }

    public static void main(String[] args) {
        Room r = new Room("0 0 5 5 0 2"); // create room
        System.out.println(r.toString()); // and print it

        String userIn = JOptionPane.showInputDialog
            (null, "please enter two coordinates one X and one Y separated by a space");
        JOptionPane.showMessageDialog(null, r.IsInRoom(P) + userIn);

        if (r.IsInRoom(P.getX() ) ) {
            System.out.println( (int) P.getX() + "," + (int) P.getY() + "Is in room");
        }
        else if (r.IsInRoom(P.getY() ) ){
            System.out.println((int) P.getX() + "," + (int) P.getY() + "Is in room");
        else (r.IsInRoom(P.getXY() ) ) { 
            System.out.println ((int) P.getX() + "," + (int) P.getY() + "Is not in room");
        }
    }
 }

【问题讨论】:

  • 虽然错误很明显,但您设计课程的方式存在很大问题。例如,您将状态保留在类成员中,但仍然在方法中发送。

标签: java if-statement boolean


【解决方案1】:

错误说明出了什么问题。您的 isInRoom 方法需要一个 Point 参数,但在您的 if(r.isInRoom(P.getX()))if(r.isInRoom(P.getY())) 中,getX()getY()double 类型,这就是您得到错误的原因..

只需将您的 if-else 结构更改为:

if(r.isInRoom(P)){
  System.out.println((int)P.getX() + "," + (int)P.getY() + "Is in room");
} else{
  System.out.println((int)P.getX() + "," + (int)P.getY() + "Is not in room");
}

足以解决它。

PS:方法和变量一般都是驼峰式,所以IsInRoom应该是isInRoom。这不是必需的,但它是 Java 编程的最佳实践。

【讨论】:

  • 嗨,感谢您帮助修复我的 If else 语句,我该如何链接用户输入,因为我目前在线程“main”java.lang.NullPointerException 中遇到错误异常,第 113 行和 85 是 isInRoom 方法和 joptionpane.showmessagedialog 等的返回语句
  • 我也经历过,将 IsInRoom 的名称改为 isInRoom
【解决方案2】:

你犯了这个错误

P.getX()P.getY() 返回 double 但在 IsInRoom 参数中,您期望 Point :)

另一点是,直到 java 11 我们不支持 else 块中的参数,例如:

else (r.IsInRoom(P.getXY() ) )

【讨论】:

  • 感谢您的帮助我不胜感激
猜你喜欢
  • 2017-05-12
  • 1970-01-01
  • 2020-08-20
  • 1970-01-01
  • 1970-01-01
  • 2014-02-02
  • 1970-01-01
  • 2016-11-20
  • 2018-04-17
相关资源
最近更新 更多