我的程序有问题,我希望我的程序的用户编写他想要使用的文件,然后使用循环,根据他输入的单词,将使用一个文件。但我的程序从来没有进入一个循环,我不知道问题出在哪里。
这是我的代码:
System.out.println("Nom du test case à lancer ? : ");
Scanner saisieUtilisateur = new Scanner(System.in);
//on rentre l'adresse du fichier texte :
String str = saisieUtilisateur.next();
System.out.println(str);
//Integer val = saisieUtilisateur.nextInt();
//System.out.println(val);
String chaine = "";
String File="";
int i=1;
//Choix du fichier a prendre en compte suivant le choix de l'utilisateur
if (str == "hello"){
File = "C:\\exempleANT\\helloWordTexte.txt";
System.out.println("dans la boucle 1");
}
else if(str == "bye"){
System.out.println("dans la boucle 2");
File = "C:\\exempleANT\\FichiersTestExempleHelloWord\\bye.txt";
}
else if(str == "fake"){
System.out.println("dans la boucle 3");
File = "C:\\exempleANT\\FichiersTestExempleHelloWord\\helloWordTexteFake.txt";
}
else {
System.out.println("ErreurTexte!");
System.out.println("dans la boucle 4");
}
这是我运行程序并输入 hello 时控制台中的结果。
hello
hello
ErreurTexte!
dans la boucle 4
最佳答案
使用 .equals 来表示字符串相等,而不是使用 '=='。
if (str.equals("hello")){
...
}
关于java - 新扫描仪出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29298187/