[英]Why am I getting a “/ by zero” error in this program?
/**
* Write a description of class TicTacToe here.
*
* @author (your name)
* @version (a version number or a date)
*/
import java.util.Scanner;
public class TicTacToe
{
public static void main(){
Scanner s = new Scanner(System.in);
int state = 0, turn = 1, choice, drawcheck=0;
for(int end=0; end==0; end=end){
System.out.println("\u000c");
for(int i =1; i<10; i++){
if(state%(3^i)==0){
System.out.print(" ");
}
else if(state%(3^i)==1){
System.out.print("X");
}
else{
System.out.print("O");
}
if(i%(3^i)==0){
System.out.println("");
}
else{
System.out.print("|");
}
}
System.out.println("Enter State (max 59048)");
state=s.nextInt();
}
}
}
I was trying to make a Tic Tac Toe game, but for some reason this gets a Divide by Zero error?我试图制作一个井字游戏,但由于某种原因,这会出现除以零错误? I am an absolute coding noob, can someone help?
我是一个绝对的编码菜鸟,有人可以帮忙吗? (I know this isn't the game, I was trying to isolate the parts, this one gets the error)
(我知道这不是游戏,我试图隔离部分,这个错误)
I have a feeling you are trying to use modulo 3 to the power of i.我有一种感觉,您正在尝试将模 3 用于 i 的幂。
However ^
sign means XOR, not power, so it happens, that you get 3 % 0 - which is basically zero division.然而
^
符号意味着 XOR,而不是幂,所以它发生了,你得到 3 % 0 - 这基本上是零除法。
If you want to the power i th power of 3 you need Math.pow(3, i)
如果你想得到 3 的 i 次方,你需要
Math.pow(3, i)
Hi seems you have mistaken the ^ operator it is XOR嗨,您似乎误会了 ^ 运算符,它是 XOR
so when i = 3 in binary 11所以当二进制 11 中的 i = 3 时
3^i = 11 XOR 11 => 0
3^i = 11 异或 11 => 0
XOR is binary operator and compares bits at i-th index and returns 1 if only one bit at i-th index is 1. This means when you xor same numbers you get 0 XOR 是二元运算符,比较第 i 个索引处的位,如果第 i 个索引处只有一位为 1,则返回 1。这意味着当您异或相同的数字时,您会得到 0
[Update] Fixed decimal representation of 3 :p [更新] 3 :p 的固定十进制表示
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.