【问题标题】:How can I store a word into a variable in a compass program如何将单词存储到指南针程序中的变量中
【发布时间】:2013-12-19 01:27:33
【问题描述】:

我需要帮助将一个单词存储在一个变量中并从我的指南针程序中输出我的信息。我需要编写一个程序让用户输入指南针方向,然后打印一条信息。不知道为什么不输出方向,需要把整数转成字符串吗/

样本输出:

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);

【问题讨论】:

  • 请提供一个最低限度的工作示例 (MWE) 来证明您的问题。
  • 请格式化您的代码并使其可编译...
  • 我编译了。感谢您的帮助

标签: java do-while charat


【解决方案1】:

如果你把问题分解成小块,事情就会简单得多,例如 -

// 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);
  }
}

【讨论】:

    【解决方案2】:

    最好的办法可能是一次循环遍历输入字符串一个字符,但使用 switch 块而不是无数的 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 等)进行调整,但这应该提供一个更可行的起点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-02
      • 2021-03-18
      • 2021-11-06
      • 2012-08-20
      • 1970-01-01
      • 2017-05-03
      • 1970-01-01
      • 2012-12-18
      相关资源
      最近更新 更多