Java 如何接收索引并显示其ASCII字符?

Java 如何接收索引并显示其ASCII字符?,java,ascii,Java,Ascii,我输入了一个数字(0-127),并希望显示带有该索引的ASCII字符。到目前为止,我只使用自己的变量实现了这一点。有人能解释一下如何用输入来代替吗 我的代码: import java.util.Scanner; public class CharCode { public static void main (String [] args) { Scanner input = new Scanner(System.in); // obtain index

我输入了一个数字(0-127),并希望显示带有该索引的ASCII字符。到目前为止,我只使用自己的变量实现了这一点。有人能解释一下如何用输入来代替吗

我的代码:

import java.util.Scanner;

public class CharCode {
    public static void main (String [] args) {
        Scanner input = new Scanner(System.in);
        // obtain index
        System.out.print("Enter an ASCII code: ");
        String entry = input.next();
        // How do I get a char with the inputted ASCII index here?

        // I can do it with my own variable, but not with input:
        int i = 97;
        char c = (char) i; // c is now equal to 'a'
        System.out.println(c);
    }
}

我认为您需要输入用户任何ASCII值,它将显示在char值中。为此,您需要更改,因为您必须对用户输入执行这些操作

int entry = input.nextInt(); // get the inputted number as integer
// and
char c = (char) entry; 

对于您当前的实现

//entry has to be String of numbers otherwise it will throw NumberFormatException.
char c = (char) (Integer.parseInt(entry)); // c will contain 'a'

您当前的代码有什么问题?还有,为什么所有代码都是左对齐的?这就是你所有代码的格式吗?既然您要求志愿者提供免费帮助,请理解我们非常感谢您为帮助您而付出的努力,比如发布格式良好、易于阅读的代码。您遇到了什么问题?这个链接有几个例子。如果我正确地解释了OP,我想她可能会说,到目前为止,她已经用“I”变量尝试了这个方法,但是在尝试使用用户输入时遇到了问题。@AlishaMcDonald很高兴听到这个消息。如果smit的答案是您想要的,请务必单击他答案左侧的复选图标。非常感谢。现在它完全有道理了