0
I am making the problem 1168 - LED and would like to know if there is a way to make my in.nextLine()
do not read the enter key,I managed creating a variable to read this enter,but I’m sure it is not the most correct way to do this.
https://www.urionlinejudge.com.br/judge/pt/problems/view/1168 (Uri problem for anyone who wants to see, I accept suggestions on code improvement as well).
import java.util.Scanner;
public class Uri1168 {
public static void main(String[]args){
int i,n;
int total = 0;
String enter;
Scanner in = new Scanner(System.in);
n = in.nextInt();
enter = in.nextLine();
for(i = 0;n>i;n--){
total = 0;
String v;
v = in.nextLine();
char[] num = v.toCharArray();
for (char c : num) { // divide o numero em cada numero separado
int f = Integer.parseInt(c+"");
if (f==1){
total += 2;
}else if (f==2){
total += 5;
}else if (f==3){
total += 5;
}else if (f==4){
total += 4;
}else if (f==5){
total += 5;
}else if (f==6){
total += 6;
}else if (f==7){
total += 3;
}else if (f==8){
total += 7;
}else if (f==9){
total += 6;
}else if (f==0){
total += 6;
}
}
System.out.println(total+" leds\n");
}
in.close();
}
}
I’m sorry to close your question as a duplicate, but there in the question Linkei (along with their answers) he explains why you are having this problem.
– Victor Stafusa
On the improvement of the code, I suggest taking a look at the structure
switch
instead ofif
– Tuxpilgrim
Or better yet, instead of
switch
orif
, use a table in the form of an array to find the numbers to be summed.int[] tabela = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6}; total += tabela[f];
- CC @Tuxpilgrim– Victor Stafusa
Beauty Victor,I will take a look. Thank you,had not found. Thanks also Tuxpilgrim ,the switch really is better
– Mark4