对不起,但是这个节目hacienod java eclipse luna和mars,问题是要有" lunes"等等,但一旦我插入品牌我109次。我需要帮助。
import java.util.Scanner;
public class Dias {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner Scanner= new Scanner(System.in);
int car=0;
int repl=0;
int repm=0;
int repi=0;
int repj=0;
int repv=0;
int reps=0;
int repd=0;
int i;
do {
System.out.println("introduzca caracteres");
car = Scanner.next().charAt(0);
if(car == 'l'){
System.out.println("lunes");
repl+='l';
}else {
if (car=='m'){
System.out.println("martes");
repm+='m';
} else {
if(car == 'i'){
System.out.println("miercoles");
repi+='i';
} else {
if(car=='j'){
System.out.println("jueves");
repj+='j';
}
else {
if(car=='v'){
System.out.println("viernes");
repv+='v';
}
else {
if(car=='s'){
System.out.println("sabado");
reps+='s';
}
else {
if(car =='d'){
System.out.println("domingo");
repd+='d';
}
else {
if(car !='l' || car !='m' || car !='i' || car !='j'|| car !='v' || car !='s' || car !='d'){
System.out.println("ERROR");
}
}
}
}
}
}
}
}
}
while(car !='*');
System.out.println("*");
System.out.println("veces repetidas de lunes:" +repl );
System.out.println("veces repetidas de martes:" +repm );
System.out.println("veces repetidas de miercoles:" +repi);
System.out.println("veces repetidas de jueves:" +repj );
System.out.println("veces repetidas de viernes:" +repv );
System.out.println("veces repetidas de sabado:" +reps );
System.out.println("veces repetidas de domingo" +repd );
}
}
答案 0 :(得分:0)
它将在您的变量声明
上char car;
Scanner.next().charAt(0);
会为您提供char
,并且您正在与if-else中的角色进行比较。
在你的上一个else
if (car != 'l' && car != 'm' && car != 'i' && car != 'j' && car != 'v' && car != 's' && car != 'd' && car != '*') {
System.out.println("ERROR");
}
答案 1 :(得分:0)
我希望我能正确地解释你的问题。我不太清楚。根据我收集的内容,您希望每次将这些特定变量的计数增加1。如果我是正确的,那么代码中的问题就很清楚了。你写道:
repl+='l';
这里它将为角色添加ASCII十进制表示' l'到变量repl。 ' l'的ASCII代码是108.要纠正错误,您只需将所有类似的代码更改为以下内容:
repl ++;
以下是包含更改的完整代码:
import java.util.Scanner;
public class Dias {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner Scanner = new Scanner(System.in);
int car = 0;
int repl = 0;
int repm = 0;
int repi = 0;
int repj = 0;
int repv = 0;
int reps = 0;
int repd = 0;
int i;
do {
System.out.println("introduzca caracteres");
car = Scanner.next().charAt(0);
if (car == 'l') {
System.out.println("lunes");
repl++;
} else {
if (car == 'm') {
System.out.println("martes");
repm++;
} else {
if (car == 'i') {
System.out.println("miercoles");
repi++;
} else {
if (car == 'j') {
System.out.println("jueves");
repj++;
} else {
if (car == 'v') {
System.out.println("viernes");
repv++;
} else {
if (car == 's') {
System.out.println("sabado");
reps++;
} else {
if (car == 'd') {
System.out.println("domingo");
repd++;
} else {
if (car != 'l' || car != 'm'
|| car != 'i' || car != 'j'
|| car != 'v' || car != 's'
|| car != 'd') {
System.out.println("ERROR");
}
}
}
}
}
}
}
}
}
while (car != '*');
System.out.println("*");
System.out.println("veces repetidas de lunes:" + repl);
System.out.println("veces repetidas de martes:" + repm);
System.out.println("veces repetidas de miercoles:" + repi);
System.out.println("veces repetidas de jueves:" + repj);
System.out.println("veces repetidas de viernes:" + repv);
System.out.println("veces repetidas de sabado:" + reps);
System.out.println("veces repetidas de domingo" + repd);
}
}