如果存在更多的1,为什么1s在整行中打印 [英] Why does 1s print in the entire row if more that one is present

查看:52
本文介绍了如果存在更多的1,为什么1s在整行中打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个程序,给出ax和ay值java将在相应的坐标位置打印到屏幕a 1。但是当存在超过11时,它会打印整行8为1而不是有些为0



I am making a program that given a x and a y value java will print to the screen a 1 in that corresponding coordinate position.But when more than 1 "1" is present it prints the entire row of 8 as 1 instead of some as 0

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication12;

import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author Coles Laptop
 */
public class JavaApplication12 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //getting input      
        Scanner input = new Scanner(System.in);
           
              
              //creating the 2d array to store the x and y values in int array queen
              int [][] queen= new int [8][2]; 
             
              
              //getting values for the queen array in a for loop
              for (int i=0; i<8; i++){
                   System.out.println("What Do You Want for the Queens X Cordinate");
                   int x1=input.nextInt();
                   System.out.println("What Do You Want For The Queens Y Cordinate?");
                   int y1=input.nextInt();
                   queen[i][0]=x1;
                   queen[i][1]=y1;}
      

   
      for (int i=0; i<8; i++){
                for(int j=0; j<8; j++){
                    if(queen[i][0]==j & queen[i][1]==i){
                        System.out.print("1");
                    }else{
                                        System.out.print("0");
                                        }
                    }
                    System.out.println();
                    
                    
                    
                    
                    
                    
                }
              }
    }





我尝试了什么:



我试过更换&到|并改变一些循环值,但这些都没有效果。



What I have tried:

I have tried changing the & to a | and changing some of the loop values and neither of those have worked.

推荐答案

尝试

Try
if(queen[i][0]==j && queen[i][1]==i){



请参阅bitwise和logican运算符之间的区别:

< a href =https://www.tutorialspoint.com/java/java_basic_operators.htm> Java Basic Operators [ ^ ]


这篇关于如果存在更多的1,为什么1s在整行中打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆