Java的int数组返回全0 [英] Java Int Array returning all 0's

查看:497
本文介绍了Java的int数组返回全0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,感谢您抽出时间来看看我的问题。我的工作我的Java功课(我懂规矩,我不希望你做我的功课,我只是很卡,我非常困惑所以请禁止/吼/捅我)

我有一个名为加密类。我打电话此一等级面板,它被放入一个Frame英寸

我需要用户输入读取和使用数组'加密'的字符串以我自己的系统。

我已经读过我的书,并寻找答案,但我不知道为什么我的int数组是returnign所有O的。我的字符数组返回正确的字符,当我调试它,但我的int数组将返回所有0。

下面是我,任何意见或建议,很多AP preciated。

感谢

 进口java.util.Scanner中; 公共类加密{私人字符串finalEncryption;
INT [] = numArray新INT [25];
的char [] = charArray {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
烧焦电流;//构造
公共加密(){}公共字符串的toString(){    返回finalEncryption;
}公共字符串setEncryption(字符串输入){    串新条目= entry.toUpperCase();    //循环都要经过串中的每个字母
    对(INT CH = 0; CH&下; newEntry.length(); CH ++)
    {
        电流= newEntry.charAt(CH);        //循环要经过字母表中的每个字母
        的for(int i = 0; I< 26;我++)
        {
            如果(当前== charArray [I])
            {
                INT finalEntry = numArray [I]
                的System.out.println(finalEntry);            }
            否则,如果(当前== numArray [I])
            {            }        }        的System.out.println(电流);
    }    返回进入;
} }


解决方案

在初始化后的int数组默认为全零 - 这似乎是你的情况。无处你设置int数组中的任何值,你只初始化,然后阅读它。

Hello all and thanks for taking the time to look at my question. I'm working on my Java homework (I understand the rules and I don't want you to do my homework, I'm just very stuck and am very confused so please to ban/yell at/poke me)

I have a class called Encryption. I'm calling this class in a Panel, which is being put into a Frame.

I need to read in user input and 'encrypt' that string with my own system using an Array.

I have read my book and searched for answers but I don't know why my INT array is returnign all O's. My Char array is returning the correct Char when I debug it, but my Int array is returning all 0's.

Here is what I have, any advice or suggestions is much appreciated.

Thanks

 import java.util.Scanner;

 public class Encryption {

private String finalEncryption;
int [] numArray = new int[25];
char[] charArray = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
char current;

//constructor
public Encryption(){

}

public String toString(){

    return finalEncryption;
}

public String setEncryption(String entry){

    String newEntry = entry.toUpperCase();

    //loop to go through each letter in the string
    for (int ch = 0; ch < newEntry.length(); ch++)
    {
        current = newEntry.charAt(ch);

        //loop to go through each letter in the alphabet
        for (int i=0; i < 26; i++)
        {
            if(current == charArray[i])
            {
                int finalEntry = numArray[i];
                System.out.println(finalEntry);

            }
            else if (current == numArray[i])
            {

            }

        }

        System.out.println(current);
    }

    return entry;
}

 }

解决方案

An int array defaults to all zero's after you initialize it - this appears to be your case. Nowhere are you setting any values in the int array, you are only initializing it, and then reading it.

这篇关于Java的int数组返回全0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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