이전에 게시했지만 아직 문제가 있습니다. 사용자가 제공 한 메시지를 디코딩해야합니다.코드 해독 프로그램 작성
메시지는 일련의 숫자로 구성되어 있으며 이러한 치환을 이용하여 디코딩되어야한다
예컨대1 = D
2 = W
3 = E
4 = L
5 = H
6 = O
7 = R
, 5344626741
HELLOWORLD
가있다.
나는 많은 것들을 시도하고 가깝지만 출력이 정확하지 않습니다. 문자열에 특정 문자가 있는지 검색하는 데 문제가 있다고 생각합니다.
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Please enter 10 numbers, after each number you put in, press enter. The numbers can only be from 1 - 7.");
int numInputs = 0;
String code = "", deciphered = "";
int input = 0, charNumber = 7;
//5344626741
do{
System.out.println("Please enter a number: ");
input = in.nextInt();
if(input>=8 || input<=0){
System.out.println("Please enter a different number: ");
input = in.nextInt();
code+=input;
numInputs++;
}
else{
code+=input;
numInputs++;
}
}while(numInputs < 10);
System.out.println("Your code is " + code);
for(int add = 0; add < 10; add++){
switch(code.charAt(charNumber)){
case '1': deciphered+="D";
case '2': deciphered+="W";
case '3': deciphered+="E";
case '4': deciphered+="L";
case '5': deciphered+="H";
case '6': deciphered+="O";
case '7': deciphered+="R";
break;
default: System.out.println("Something went wrong! Try again with numbers only 1 - 7.");
charNumber++;
}
}
System.out.println("The output is: "+deciphered);
in.close();
}
각'case'에 대해'break'가 필요합니다. 그렇지 않으면 다음'case'로 넘어갑니다. –
숫자를 문자에 매핑하는 배열을 생각 했습니까? –
사용자가 잘못된 입력을 두 번 입력하면이를 무시해야 할 때 유효한 번호로 사용한다는 점도 유의해야합니다. –