Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我的条件从未达到?_Java_Arrays_For Loop_Linear Search - Fatal编程技术网

Java 为什么我的条件从未达到?

Java 为什么我的条件从未达到?,java,arrays,for-loop,linear-search,Java,Arrays,For Loop,Linear Search,在我的for循环的底部获得一个战利品。这可能是一个简单的逻辑错误,但由于某些原因,“如果”条件从未满足。对不起,我问了一些基本的问题,但我已经搜索了又搜索,似乎找不到答案。谢谢你帮了个忙 Scanner scan = new Scanner(System.in); System.out.println("How large would you like the array to be? (number)"); int arraySize = scan.nextInt();

在我的for循环的底部获得一个战利品。这可能是一个简单的逻辑错误,但由于某些原因,“如果”条件从未满足。对不起,我问了一些基本的问题,但我已经搜索了又搜索,似乎找不到答案。谢谢你帮了个忙

Scanner scan = new Scanner(System.in);

    System.out.println("How large would you like the array to be? (number)");
    int arraySize = scan.nextInt();
    scan.nextLine();
    String [] myArray = new String [arraySize];
    int i = 0;

    if (arraySize <= 0 ) {
        System.out.println("Please enter a positive integer for the array size. Rerun program when ready.");
    } else {
        while (i < myArray.length) {
            System.out.println("Please type a string to be entered in the array");
            myArray[i] = scan.nextLine();
            i++;
        }
    System.out.println("Array contents: " + Arrays.toString(myArray));
    }
    System.out.println("What element would you like would you like to find in the array by performing a linear search?");
    String search = scan.nextLine();

    for (int j = 0; j < myArray.length; j++) {
        if (myArray[j] == search){
            int location = j + 1;
            System.out.println("The element, " + search + " was found in the array, in which the linear search looped " + location + " times to find it." );
            j = myArray.length;
        }
    }
Scanner scan=新的扫描仪(System.in);
System.out.println(“您希望数组有多大?(数字)”;
int arraySize=scan.nextInt();
scan.nextLine();
String[]myArray=新字符串[arraySize];
int i=0;

if(arraySize您应该始终使用
.equals()
而不是
=
运算符进行字符串比较。
=
运算符仅当两个引用都指向同一个字符串实例时才会计算为true。要检查字符串内容是否相等,可以使用
.equals()
equalsIgnoreCase())

因此,将搜索条件从

if (myArray[j] == search)


您应该始终使用
.equals()
而不是
=
运算符进行字符串比较。
=
运算符仅当两个引用都指向同一字符串实例时才会计算为true。要检查字符串内容是否相等,可以使用
.equals()
equalsIgnoreCase()

因此,将搜索条件从

if (myArray[j] == search)


您使用的是
=
而不是
.equals
,它不检查字符串是否相等。
.equals
将检查值是否相等,而不仅仅是像这样的参考号:

if( myArray[j].equals(search)){

您使用的是
=
而不是
.equals
,它不检查字符串是否相等。
.equals
将检查值是否相等,而不仅仅是像这样的参考号:

if( myArray[j].equals(search)){

我的猜测是:字符串相等再次出现。永远不要将字符串与
=
进行比较。在比较两个字符串时,请始终使用
等于(…)
等于(…)
方法。
if(myArray[j]==search){
使用.equals()。在比较两个字符串时必须使用
.equals
。例如:
if(myArray[j].equals(search)){
我的猜测:字符串相等再次出现。永远不要将字符串与
==
进行比较。请始终使用
等于(…)
等于信号例(…)
方法。如果(myArray[j]==search){
使用.equals()在比较两个字符串时必须使用
.equals
。例如:
如果(myArray[j].equals(search)){
我希望你的答案没有前9个单词(“除非两个引用都指向同一个字符串实例”)。通常认为字符串引用相同是个坏主意(而且没有必要),这会让你看起来好像不知道自己在做什么。我希望你的答案没有前9个单词(“除非两个引用都指向同一个字符串实例”)。指望字符串引用相同通常是个坏主意(也没有必要),这会让你看起来好像不知道自己在做什么。