简体   繁体   English

当字符位于数组的第二行时,为什么我的代码不起作用?

[英]Why does my code not work when a character is in the second row of the array?

I have some code with a method that is trying to return the opposing character label of a 2d array's position.我有一些带有方法的代码,该方法试图返回二维数组位置的相反字符标签。

Let's suppose the array and it's labels for each position are like below:让我们假设数组及其每个位置的标签如下:

{{3,3,3,3}, {3,3,3,3}}
  a,b,c,d    e,f,g,h

If b is entered, f is returned.如果输入 b,则返回 f。 I have it working right if a, b, c, or d is entered, but it doesn't work right if e, f, g, or h is entered and I can't figure out why.如果输入 a、b、c 或 d,我可以正常工作,但如果输入 e、f、g 或 h,我就无法正常工作,我不知道为什么。

public class ByteTest {

    public static void main(String[] args) {
        int[][] testArray = {{3,3,3,3}, {3,3,3,3}};
        //                    a b c d    e f g h
        char slectdPit = 'e';
        int total = (int)slectdPit;
        int total97 = total - 97;
        System.out.println(myGetOpposingPit(slectdPit, testArray));
    }

    public static char myGetOpposingPit(char b, int[][] ints) {
        char retChar = 'z';
        int retVal = 0;
        int charPosi = 0;
        int total97 = 0;
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < ints[0].length; j++) {
                charPosi = (int)b;
                total97 = charPosi - 97;
                if (total97 <= ints[0].length) {
                    if (total97 == j) {
                        retVal = (charPosi + ints[0].length);
                        retChar = (char)retVal;
                    }
                }

                else if (total97 > ints[0].length) {
                    if (total97 == (j + (ints[0].length))) {
                        retVal = (charPosi - ints[0].length);
                        retChar = (char)retVal;
                    }
                }
            }
        }
        return retChar;
    }

}

I've made an if statement我做了一个 if 语句

 else if (total97 > ints[0].length) {
                if (total97 == (j + (ints[0].length))) {
                    retVal = (charPosi - ints[0].length);
                    retChar = (char)retVal;
                }

That's supposed to get find labels that are in the second row of the array, and assign returnedChar with characterPosition - array row length, so if the character ran through myGetOpposingPit is g, it'd be charPosi (103) minus ints[0].length (4), equaling 99 , which gets ascii converted into 'c'.这应该找到数组第二行中的标签,并用characterPosition - 数组行长度分配返回的myGetOpposingPit ,所以如果通过myGetOpposingPit的字符是 g,它会是charPosi (103) minus ints[0].length (4), equaling 99 ,将ascii转换为 'c'。

Doesn't work though, z gets returned.但是不起作用, z被返回。 So one of my if statements isn't running or something.所以我的 if 语句之一没有运行或什么的。

Your logic seems to be incorrect for when total97 == ints[0].length .total97 == ints[0].length时,您的逻辑似乎不正确。 I changed the branch that gets executed when they are equal:我更改了当它们相等时执行的分支:

public static char myGetOpposingPit(char b, int[][] ints) {
    char retChar = 'z';
    int retVal = 0;
    int charPosi = 0;
    int total97 = 0;

    charPosi = (int)b;
    total97 = charPosi - 97;

    for (int i = 0; i < 2; i++) {

        for (int j = 0; j < ints[0].length; j++) {

            if (total97 < ints[0].length) {
                if (total97 == j) {
                    retVal = (charPosi + ints[0].length);
                    retChar = (char)retVal;
                }
            } else if (total97 >= ints[0].length) {
                if (total97 == (j + (ints[0].length))) {
                    retVal = (charPosi - ints[0].length);
                    retChar = (char)retVal;
                }
            }
        }
    }

    return retChar;
}

I also moved the total97 assignment out of the loop.我还将total97分配移出循环。 It doesn't change so it doesn't need to be calculated each iteration.它不会改变,所以不需要每次迭代都计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么同步在第二个代码中不起作用? - Why does synchronization not work in the second code? 当我的输入包含字符时,为什么我的代码不起作用? 我希望我的代码忽略字符 - why my code doesn't work when my input contain character? I want my code to ignore character 为什么我的代码不起作用(日记代码)? - Why does my code not work (diary code)? 试图将两个数组组合成一个排序的数组; 第一个数组小于第二个数组时,代码不起作用 - Trying to combine two arrays into one sorted array; code does not work when first array is smaller than second array 为什么第二个矩形碰撞在第一个矩形碰撞时不起作用? - Why does the second rectangle collision not work when the first does? 为什么我的排序代码无法正常工作? - Why my sorting code does not work properly? 为什么 my.isEmpty 在此代码中不起作用? - Why my .isEmpty does not work in this code? 为什么我的代码中的指纹相似性不起作用? - why fingerprintsimilarty in my code does not work? 当我连续输入2个以上的整数时,为什么我的前缀操作程序不起作用? - Why does my prefix operation program not work when I enter more than 2 integers in a row? 检查数组是否有某个字符,为什么我的代码不起作用? - Checking if an array has a certain character, why is my code not working?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM