Java 如何将单词存储到compass程序中的变量中
我需要帮助在变量中存储一个单词,并从指南针程序输出消息。我需要编写一个程序,让用户输入指南针方向,然后打印消息。我不知道为什么它不输出方向,我需要把整数转换成字符串吗/ 样本输出:Java 如何将单词存储到compass程序中的变量中,java,do-while,charat,Java,Do While,Charat,我需要帮助在变量中存储一个单词,并从指南针程序输出消息。我需要编写一个程序,让用户输入指南针方向,然后打印消息。我不知道为什么它不输出方向,我需要把整数转换成字符串吗/ 样本输出: Input compass direction(Eg.S45E): S45E Start facing South.Turn 45 degrees towards East. 代码: 最好的方法可能是一次循环输入字符串一个字符,但使用开关块而不是无数的if语句: String direction = ""; Str
Input compass direction(Eg.S45E):
S45E
Start facing South.Turn 45 degrees towards East.
代码:
最好的方法可能是一次循环输入字符串一个字符,但使用开关块而不是无数的if语句:
String direction = "";
String word2 = br.ReadLine();
foreach (char c in word2.toCharArray();
{
switch (c)
{
case 'N':
direction = "North";
break;
case 'S':
direction = "South";
break;
... // Do the rest here
}
}
该示例需要针对多个方向(NE、NW-SW、NNE等)进行调整,但这应该提供一个更可行的起点。如果将问题分解为多个部分,事情会简单得多,例如-
// Convert the character representing a direction, into a cardinal direction.
private static String getDirection(char direction) {
if (direction == 'N' || direction == 'n') {
return "North";
} else if (direction == 'S' || direction == 's') {
return "South";
} else if (direction == 'E' || direction == 'e') {
return "East";
} else if (direction == 'W' || direction == 'w') {
return "West";
}
return "Unknown";
}
public static void main(String[] args)
throws InterruptedException {
// Construct a scanner.
Scanner scanner = new Scanner(System.in);
// The output message format.
String fmt = "Start facing %s.Turn %s degrees towards %s.";
// Loop forever.
for (;;) {
// Print directions.
System.out.println("Compass Directions");
System.out.println("==================");
System.out.println("Input a compass direction "
+ "(E.g. S45E):");
// Check that System.in hasn't been closed.
if (!scanner.hasNextLine()) {
break;
}
// Get input.
String line = scanner.nextLine();
String start = getDirection(line.charAt(0));
String end = getDirection(line.charAt(line
.length() - 1));
String msg = String.format(fmt, start,
line.substring(1, line.length() - 1), end);
System.out.println(msg);
}
}
请提供一个最小工作示例(MWE)来演示您的问题。请格式化您的代码并使其可编译…我编译了它。感谢您的帮助
// Convert the character representing a direction, into a cardinal direction.
private static String getDirection(char direction) {
if (direction == 'N' || direction == 'n') {
return "North";
} else if (direction == 'S' || direction == 's') {
return "South";
} else if (direction == 'E' || direction == 'e') {
return "East";
} else if (direction == 'W' || direction == 'w') {
return "West";
}
return "Unknown";
}
public static void main(String[] args)
throws InterruptedException {
// Construct a scanner.
Scanner scanner = new Scanner(System.in);
// The output message format.
String fmt = "Start facing %s.Turn %s degrees towards %s.";
// Loop forever.
for (;;) {
// Print directions.
System.out.println("Compass Directions");
System.out.println("==================");
System.out.println("Input a compass direction "
+ "(E.g. S45E):");
// Check that System.in hasn't been closed.
if (!scanner.hasNextLine()) {
break;
}
// Get input.
String line = scanner.nextLine();
String start = getDirection(line.charAt(0));
String end = getDirection(line.charAt(line
.length() - 1));
String msg = String.format(fmt, start,
line.substring(1, line.length() - 1), end);
System.out.println(msg);
}
}