Java - for循环被跳过 [英] Java - for loops being skipped

查看:208
本文介绍了Java - for循环被跳过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  import java.lang.Integer; 
import java.util.Arrays;

public class Decimal {

//初始化intance变量
private int decimal;
私有字符串十六进制;

public static void toHex(String s){
int decimal = Integer.parseInt(s); //将字符串转换为二进制转换的整数
字符串十六进制= null;
int [] binNum = new int [16];
int [] binNumNibble = new int [4]; //半字节是四位。
int nibbleTot = 0;
char hexDig ='\0';
char [] cvtdHex = new char [4];
StringBuffer result = new StringBuffer();

for(int a = 32768; a == 1; a / = 2){// 32768是最大位的值。
int b = 0; //在for循环结束时,将在15处位置上方。 15引用binNum数组中的最后一个点。
if(decimal> a){
decimal - = a;
binNum [b ++] = 1; //数组对所有元素都有一个默认值0。这提供了一个解析的二进制数字。



(int a = 0; a == 15; a + = 3){
//将二进制数的部分复制到binNumNibble数组。 .arraycopy在java 1.5及更低版本中使用。
//Arrays.copyOfRange用于java 1.5及更高版本。
System.arraycopy(binNum,a,binNumNibble,0,4);

for(int b = 8; b == 1; a + = 3){
int c = 0;
nibbleTot + = binNumNibble [c ++];

//将单个十六进制值转换为十六进制数字。
if(nibbleTot> = 1& nibbleTot< = 9){
hexDig + = nibbleTot;
} else if(nibbleTot == 10){
hexDig ='A';
} else if(nibbleTot == 11){
hexDig ='B';
} else if(nibbleTot == 12){
hexDig ='C';
} else if(nibbleTot == 13){
hexDig ='D';
} else if(nibbleTot == 14){
hexDig ='E';
} else if(nibbleTot == 15){
hexDig ='F';
}

cvtdHex [c ++] = hexDig;
}
}
// return hex = new String(cvtdHex);
十六进制=新字符串(cvtdHex);
System.out.print(Hex:+ hex);




$ b我似乎无法弄清楚为什么变量十六进制作为空白变量返回。我一直在使用System.out.print();在每个for循环中,没有一个被使用,给我的印象是for循环被完全跳过,但我不明白为什么,我在时间限制。

非常感谢任何帮助,但请不要粘贴代码。我需要了解这个为我的计算机科学类!

解决方案

是的,您的循环被跳过,因为第二部分for语句不是中断条件,而是必须满足循环才能运行的条件。



所以它不是
$ (a = 0; a == 15; a + = 3)


 对于(a = 0; a <= 15; a + = b),b 
$ b

但是

  3)

等等...


import java.lang.Integer;
import java.util.Arrays;

public class Decimal {

    //initialize intance variables
    private int decimal;
    private String hex;

    public static void toHex(String s) {
        int decimal = Integer.parseInt(s); //converts the s string into an int for binary conversion. 
        String hex = null;
        int[] binNum = new int[16];
        int[] binNumNibble = new int[4]; //A nibble is four bits.
        int nibbleTot = 0;
        char hexDig = '\0';
        char[] cvtdHex = new char[4];
        StringBuffer result = new StringBuffer();

        for(int a = 32768; a == 1; a /= 2) { //32768 is the value of the largest bit.
            int b = 0;//Will top at 15 at the end of the for loop. 15 references the last  spot in the binNum array. 
            if(decimal > a) {
                decimal -= a;
                binNum[b++] = 1;//Arrays have a default value of zero to all elements.     This provides a parsed binary number.
            }
        }

        for(int a = 0; a == 15; a += 3) {
            //Copies pieces of the binary number to the binNumNibble array. .arraycopy is used in java 1.5 and lower.
            //Arrays.copyOfRange is used in java 1.5 and higher.
            System.arraycopy(binNum, a, binNumNibble, 0, 4);

            for(int b = 8; b == 1; a += 3) {
                int c = 0;
                nibbleTot += binNumNibble[c++];

                //Converts the single hex value into a hex digit.
                if(nibbleTot >= 1 && nibbleTot <= 9) {
                    hexDig += nibbleTot;
                } else if(nibbleTot == 10) {
                    hexDig = 'A';
                } else if(nibbleTot == 11) {
                    hexDig = 'B';
                } else if(nibbleTot == 12) {
                    hexDig = 'C';
                } else if(nibbleTot == 13) {
                    hexDig = 'D';
                } else if(nibbleTot == 14) {
                    hexDig = 'E';
                } else if(nibbleTot == 15) {
                    hexDig = 'F';
                }

                cvtdHex[c++] = hexDig;
            }
        }
        //return hex = new String(cvtdHex);
        hex = new String(cvtdHex);
        System.out.print("Hex: " + hex);
    }
}

I can't seem to figure out why variable hex is returned as a blank variable. I've been using System.out.print(); in each for loop and none of them are used, giving me the impression that the for loops are being skipped entirely, but I don't understand why and I'm on a time limit.

Any help is much appreciated, but please don't just paste code. I need to understand this for my computer science class!

解决方案

yes, your for loops ARE being skipped, since the second part of the for statement is not the break condition but the condition that has to be fullfilled for the loop to run.

So it is NOT

for(a = 0; a == 15; a += 3)

but

for(a = 0; a <= 15; a += 3)

and so on...

这篇关于Java - for循环被跳过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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