如何在同一个for循环中排除字符和整数?

时间:2017-06-25 03:36:14

标签: java

我一直试图弄清楚这几个小时但没有用。基本上我需要将用户输入打印为初始值,ASCII(字符),ASCII(整数)和十六进制。我可以让一切工作但是如果它是一个数字我不应该打印char值,如果它是一个char则打印int值。我可以让一切工作,除了我无法弄清楚如何在同一个for循环中排除字母和数字。必须打印的方式非常严格。我下面的代码将打印Initial,ASCII(char)和Hex值。我有另一个if语句可以自己打印ASCII(int) - 但很明显,如果我包含if语句它将排除所有内容..任何帮助都会很棒谢谢你..

“输出应该是什么样子”
http://imgur.com/v0s40K5

import java.util.*;

/****************************************************************************************
HNUnit3Ch12.java
xx

Write a program that gets input for a string, then output the initial character, ascii 
values of the character and number, and hex values of each character. Repeat until quit.
Get input for a string containing both numbers and letters of any length
Use a for loop with postfix notation to increment through the string, printing the
following:
The initial digit/character
The ASCII value of each character
The ASCII value of each integer converted to a string
Only output the ASCII(char) for letters and ASCII(int) for integers
Spaces should output ASCII(char) and hex only
The Hex value of each integer/character output in upper case
Output “Thank you for playing!”
****************************************************************************************/

public class HNUnit3Ch12
{
    public static void main (String[] args)
    {
        Scanner input = new Scanner(System.in);
        final String HEADER = "%-10s%-15s%-15s%s";
        //final String OUTPUT = "%s\n %-15s\n %-15s\n %s";
        String userString;

        System.out.print("Please enter a string of any length: ");
        userString = input.nextLine();
        System.out.printf(HEADER, "\nInitial", "ASCII(char)", "ASCII(int)", "Hex\n");

        for (int i = 0; i < userString.length(); i ++)
        {
            char ch = userString.charAt(i);

            char initialCase = userString.charAt(i);

            if (!Character.isDigit(ch))
                {
                int letters = (int) ch;

            //if (!Character.isLetter(ch))
            //  {
            //  int numbers = (int) ch;

            String hex = Integer.toString(ch, 16).toUpperCase();

            System.out.printf("%-9s%-9s%-9s%n",initialCase,letters,hex);
                }
            //  }
        }

    } // End main
} // End public class HNUnit3Ch12

1 个答案:

答案 0 :(得分:0)

您的代码存在多个问题。

  • 示例输出包含8,16和16宽的列。您的printf格式应该反映出来。

  • 由于第二个printf使用%n,您已经知道它是如何工作的,那么为什么第一个printf没有使用它?

    System.out.printf("%n%-8s%-16s%-16s%s%n", "Initial", "ASCII(char)", "ASCII(int)", "Hex");
    
  • 要打印十进制数字,请使用%d,要以十六进制打印数字,请使用%x。要始终将十六进制打印为2位数,请使用%02x

    // for char
    System.out.printf("%-24s%-16d%02x%n", ch, (int)ch, (int)ch);
    
    // for int
    System.out.printf("%-8s%-32d%02x%n", ch, (int)ch, (int)ch);
    

由于printf为您完成所有工作,您的代码很简单:

System.out.printf("%n%-8s%-16s%-16s%s%n", "Initial", "ASCII(char)", "ASCII(int)", "Hex");
for (int i = 0; i < userString.length(); i ++) {
    char ch = userString.charAt(i);
    if (Character.isDigit(ch)) {
        System.out.printf("%-24s%-16d%02x%n", ch, (int)ch, (int)ch);
    } else {
        System.out.printf("%-8s%-32d%02x%n", ch, (int)ch, (int)ch);
    }
}