简体   繁体   English

为什么我在这个程序中收到“/ by zero”错误?

[英]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.

相关问题 除以零时出现 ArithmeticException 错误。 但我不是。 为什么? - I am getting an ArithmeticException Error for dividing by zero. But I am not. Why? 我在以下程序中得到OptionalDataException。 为什么会出现此错误 - I am getting OptionalDataException in the following Program. why this error is coming 以下Java程序的输出是什么? 以及为什么我会出错 - What will be the output of following Java program? And why I am getting error 为什么我的 LWJGL 程序会出现此错误? - Why am I getting this error for my LWJGL program? 为什么我收到此错误“由:java.lang.ArithmeticException:除以零”? - Why I am getting this error “Caused by: java.lang.ArithmeticException: divide by zero”? 为什么我收到错误“线程中的异常”AWT-EventQueue-0“java.lang.ArithmeticException:/ by zero”? - Why am i getting the error “Exception in thread ”AWT-EventQueue-0“ java.lang.ArithmeticException: / by zero”? 为什么在此Java程序中出现ArrayIndexOutOfBoundsException? - Why am I getting ArrayIndexOutOfBoundsException in this Java program? 为什么在此程序中出现nullpointer异常? - Why am I getting nullpointer Exception in this Program? 为什么在下面的程序中出现UnsatisfiedLinkError? - Why am I getting UnsatisfiedLinkError in below program? 为什么会收到{错误? - Why am I getting a { error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM