不是确切的程序,但基本上如果我有类似
的东西public class WereWolfenstein2D {
public static void main(String[] args){
{Scanner sc = new Scanner(System.in);
Random number = new Random();
int office;
int hitmenlocation;
office = 1+number.nextInt(16);
hitmenlocation = 1+number.nextInt(16);
然后我想做一个if声明说如果办公室比hitmenlocation更大或更小,那么应该发生一些事情,比如打印一条消息。如何输出那种比较语句?
答案 0 :(得分:0)
如果陈述说如果办公室比一个更大或一个小于hitmenlocation
直接翻译为......
if( (office - hitmenlocation) == 1 || (office - hitmenlocation) == -1) {
/* do stuff */
}
更清洁的方法是......
if(Math.abs(office - hitmenlocation) == 1) {
/* do stuff */
}