该代码的真正含义是什么? [英] What does this code really mean?

查看:60
本文介绍了该代码的真正含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个用于int的静态方法数组搜索器,目的是为了澄清自己的算法.我做了静态方法:

 公共类ArraySearcher公共静态整数integerSearcher(int [] arr,int val){整数匹配= -1;for(int i = 0; i< arr.length; i ++){if(arr [i] == val){匹配=我;休息;}}返回比赛;}} 

在main方法中,我创建了一个int数组,并使用我的静态方法在新的int数组中查找已定义的int

  int [] anIntArray = {20,30,40,60};int searchingAnArray = ArraySearcher.integerSearcher(anIntArray,60);如果(searchingAnArray == -1){System.out.println(找不到匹配项");} 别的 {System.out.println(找到的结果:" + anIntArray [searchingAnArray]);} 

问题是我不理解 System.out.println(结果发现:" + anIntArray [searchingAnArray]);",这实际上意味着什么.

解决方案

  System.out.println(结果已找到:" + anIntArray [searchingAnArray]); 

将在 searchingAnArray 位置上查找 anIntArray .IE.将在位置 3 (3来自您的方法)中查找数组 anIntArray 的值,然后将打印:

找到

 结果:60 

如果您是说 + 是什么意思,就像@HotLicks在他的

另一个例子:

 字符串名称="Chuck Norris";System.out.println("H​​ello" +名称); 

它将打印

 你好,你不是Chuck Norris 

不,只是开个玩笑,它会显示:

  Hello Chuck Norris 

另一个例子:

  int年= 2014,天= 12,月= 8;//这可以用日期类型完成,但出于演示目的,我将这样做.System.out.println("Today is:" + day +"/" + month +"/" + year); 

将打印:

 今天是:2014/12/8 

如您所见,它将连接一个 String 和一个 int ,依此类推,然后进行打印.

它可以看作是:

 字符串stringToBePrinted =今天是:" +天+"/" +月+"/" +年;System.out.println(stringToBePrinted); 

如果您在 System.out.println(...)内部调用串联,它将执行上述操作(以隐式方式),例如执行创建局部变量",然后打印它.(不完全是这样,但是您可以像这样理解它).

希望这就是您要寻找的,尽我所能最好地解释.

I made a static method array searcher for int for some pratise to clear myself for making algorithm. I made the static method:

public class ArraySearcher 

    public static int integerSearcher(int[] arr,int val){
        int match = -1;
        for(int i = 0; i < arr.length; i++){
            if(arr[i]==val){
                match = i;
                break;
            }

        }
        return match;
    }
}

And in the main method I created an array of int and used my static method for finding a defined int in the new int array

int[] anIntArray = { 20, 30, 40, 60 };
int searchingAnArray = ArraySearcher.integerSearcher(anIntArray, 60);
if (searchingAnArray == -1){
    System.out.println("match not found");
} else {
    System.out.println("result found: "+ anIntArray[searchingAnArray]);
}

The question is that I did'n't understand the System.out.println("result found: "+ anIntArray[searchingAnArray]);" and what does this really means.

System.out.println("result found: "+ anIntArray[searchingAnArray]);

Will look for anIntArray on searchingAnArray position. I.e. will look for the value of the array anIntArray at position 3 (3 comes from your method), then it will print:

result found: 60

If you mean what does + means, is, as @HotLicks said on his comment, a concatenation. It will convert 60 into a String and add it to the same String as "result found: "

Another example:

String name = "Chuck Norris";
System.out.println("Hello " + name);

It will print

Hello you're not Chuck Norris

nah, just joking, it will print:

Hello Chuck Norris

Another example:

int year = 2014, day = 12, month = 8; //This could be done with a date type but for demonstration purposes I'll do it like this.
System.out.println("Today is: " + day + "/" + month + "/" + year);

And will print:

Today is: 12/8/2014

As you can see it will concatenate a String and then an int and so on, then print it.

It could be seen as:

String stringToBePrinted = "Today is: " + day + "/" + month + "/" + year;
System.out.println(stringToBePrinted);

If you call concatenation inside System.out.println(...) it will do something like the above (in an implicit way), something like "creating a local variable" and then print it. (Not exactly that way but you can see it like that to understand it).

Hopefully that is what you're looking for, the best explained as I could.

这篇关于该代码的真正含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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