我需要帮助将单词存储在变量中并从我的指南针程序中输出我的信息。我需要编写一个程序,让用户输入指南针方向,然后打印一条信息。我不知道为什么它不输出方向,我需要将整数转换为字符串/
样本输出:
Input compass direction(Eg.S45E):
S45E
Start facing South.Turn 45 degrees towards East.
代码:
// Initializes the BufferReader for user input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//initiates first do-while loop
String word = null;
String word2 = null;
System.out.println("Compass Directions");
System.out.println("==================");
System.out.println("Input a compass direction "
+ "(E.g. S45E):");
String userInput = br.readLine();
/*gets unicode from users first given letter
to find direction */
int direction = userInput.charAt(0);
//finds unicode at the last character entered
int direction2 = userInput.charAt(3);
//check direction input and prints it
if (direction == 78)
word="North";
else if (direction == 83)
word="South";
else if (direction == 87)
word="West";
else if (direction == 69)
word="East";
//gets degree #1
char degrees1 = userInput.charAt(1);
//gets degree #2
char degrees2 = userInput.charAt(2);
if (direction2 == 78)
word2="North";
else if (direction2 == 83)
word2="South";
else if (direction2 == 87)
word2="West";
else if (direction2 == 69)
word2="East";
System.out.println("Start facing "+word+" turn "+degrees1+degrees2+" degrees to the "+word2);