Altin Mullaidrizi:
Quiero contar las entradas (cadenas) escritos en la consola que terminan en "im", así que ¿cómo puedo hacerlo como titular?
import java.lang.String;
import java.util.Scanner;
public class WordCount {
public static void main (String[]args){
final String SENTINEL = "END";
Scanner sc = new Scanner(System.in);
String text = "xy";
do {
System.out.print("Type a text or type "+SENTINEL+" when you are done. ");
text = sc.nextLine();
} while(!text.equalsIgnoreCase(SENTINEL));
boolean IMcheck = text.endsWith("im");
int count = 0;
if(IMcheck == true){
count++;
}
System.out.println("You have typed "+ count +" texts that end in \"im\" ");
}
}
LuminousNutria:
A medida que los comentaristas han dicho, tiene que mover su sentencia if dentro del bucle do-while. Tener la variable "IMcheck" es innecesaria aquí.
Para resolver el problema, he puesto el código que utiliza para asignar la variable "IMcheck" dentro de su estado de cuenta si-y me he movido en su do-while.
public class WordCount {
public static void main (String[]args) {
final String SENTINEL = "END";
int count = 0;
Scanner sc = new Scanner(System.in);
String text = "xy";
do {
if(text.endsWith("im"))
count++;
System.out.print("Type a text or type "+SENTINEL+" when you are done. ");
text = sc.nextLine();
} while(!text.equalsIgnoreCase(SENTINEL));
System.out.println("You have typed "+ count +" texts that end in \"im\" ");
}
}