官术网_书友最值得收藏!

The Conversion class

The Conversion class applies when there is conversion from numbers to letters (int to char). The class has a single String class variable and a constructor method that is passed to the user's input. The class has one additional method, the convertToCharacters() method. That method converts the user's input into a character array and then processes that array, one character at a time. The processing is implemented with a simple switch statement. Each numerical value, from 0 through 9, has a corresponding character. The characters are printed one at a time until the entire character array has been processed.

The first section of our Conversion class follows:

public class Conversion {

// class variable
public String userInput;

// constructor
public Conversion(String userInput) {
this.userInput = userInput;
}

public void convertToCharacters(String userInput) {

this.userInput = userInput;

System.out.print("Decrypted Message: ");
char answer[] = userInput.toCharArray();

The remaining code from the Conversion class is provided here:

        for (int i=0; i < answer.length; i++) {
switch (answer[i]) {
case '0':
System.out.print("A");
break;
case '1':
System.out.print("E");
break;
case '2':
System.out.print("I");
break;
case '3':
System.out.print("Y");
break;
case '4':
System.out.print("O");
break;
case '5':
System.out.print("L");
break;
case '6':
System.out.print("R");
break;
case '7':
System.out.print("T");
break;
case '8':
System.out.print("C");
break;
case '9':
System.out.print("S");
break;
}
}
}
}

The preceding code implements a 10-character mapping of integers. A complete implementation would account for the entire alphabet or, depending on the language, alphabets.

主站蜘蛛池模板: 时尚| 百色市| 丹东市| 呼和浩特市| 吉木萨尔县| 灌阳县| 金寨县| 攀枝花市| 吉水县| 姚安县| 安泽县| 莱芜市| 台江县| 临汾市| 康平县| 永康市| 巴中市| 从江县| 定安县| 涟源市| 平利县| 新晃| 江川县| 韶山市| 三亚市| 崇义县| 建始县| 正镶白旗| 东城区| 白水县| 辽宁省| 黄大仙区| 健康| 通海县| 高邮市| 江安县| 缙云县| 百色市| 抚远县| 楚雄市| 宜兴市|