mercredi 9 septembre 2015

Program not giving the expected output [duplicate]

This question already has an answer here:

The program should ask the user for a name and then outputs to the user whether that name achieved a top score, but for some reason it doesn't. Here's the code:

package names;
import java.util.*;
public class NameSearch {

static String[] names = new String[4];

void populateStringArray(){
    names[0] = "Ben";
    names[1] = "Thor";
    names[2] = "Zoe";
    names[3] = "Kate";
}


public static void main(String[] args){

    String pName;
    int max = names.length;
    int current = 0;
    boolean found = false;
    Scanner scan = new Scanner(System.in);

System.out.println("What player are you looking for?");

pName = scan.next();

scan.close();

while (!false && current < max){

    if (names[current] == pName){
        found = true;
    }else{
        current++;
    }
}
if (found == true){
    System.out.println("Yes, they have a top score");
}else{
    System.out.println("No, they do not have a top score");
}

}
}

When I type 'Thor', the program should output

Yes, they have a top score

but it outputs instead

No, they do not have a top score

I am guessing the problem is with the ifstatement but I don't know what is wrong with it.

Any thoughts?

Aucun commentaire:

Enregistrer un commentaire