0
Guys, I have this code, it takes a sentence and counts how long it took to type a whole sentence. since each letter has a value. but I have to do this for 10 sentences. and I’m only able to do it if I repeat code, for example, I put: frase1, frase2 .... up to phrase 10 and I repeat all the rest of code, ie one for each sentence, one Count for each phrase... I want to know if there is how to reuse the code for the 10 sentences, without creating 10 "for" and 10 Count?
package pacote;
import java.util.HashMap;
public class ContagemMelhoria2 {
public static void main(String[] args) {
HashMap<String, Integer> hashLetras=new HashMap<String, Integer>();
hashLetras.put("A", 2); hashLetras.put("E", 3); hashLetras.put("O", 3); hashLetras.put("S", 4); hashLetras.put("R", 4);
hashLetras.put("I", 4); hashLetras.put("N", 5); hashLetras.put("D", 5); hashLetras.put("M", 5); hashLetras.put("U", 5);
hashLetras.put("T", 6); hashLetras.put("C", 6); hashLetras.put("L", 6); hashLetras.put("P", 6); hashLetras.put("V", 7);
hashLetras.put("G", 7); hashLetras.put("H", 7); hashLetras.put("Q", 7); hashLetras.put("B", 8); hashLetras.put("F", 8);
hashLetras.put("Z", 8); hashLetras.put("J", 9); hashLetras.put("X", 9); hashLetras.put("K", 9); hashLetras.put("W", 10);
hashLetras.put("Y", 10);hashLetras.put(" ", 6);//espaco
String frase1 = "TECNOLOGIA ENVOLVE SERVICO RECURSOS ESTRATEGIA";
int count=0;
for(int i=0; i<frase1.length();i++){
if( i<(frase1.length()-1) && frase1.charAt(i)=='Q' && frase1.charAt(i+1)=='U'){
count = count+8;
i++;
}else{
String c = frase1.charAt(i)+ "";
count = count+hashLetras.get(c);
}
}
System.out.println("O tempo foi de: "+count);
}
}
if it helped? you have no idea rsrs. Thank you very much
– Gabriella
Just one question, if I want to add up all these times: I tried it this way, but it didn’t work. sum = sum + Count; System.out.println("Eee"+ sum);
– Gabriella
You change the method contaLetters from String to Integer, return only Count and remove the text. In the main method, you do the account. : ) I edited the answer p/ you check.
– Filipe L. Constante
I’m just on the floor with so much intelligence. Thank you again
– Gabriella