如何在Java中将大写字符转换为ASCII值?

cgyqldqp  于 2023-03-21  发布在  Java
关注(0)|答案(3)|浏览(141)

抱歉,这方面很新。
我的任务是创建一个ASCII转换器,其中用户提供一个大写字符,并将其转换为ASCII值。它还返回小写等效值。我仍然在第一步,因为我不知道如何做到这一点,甚至从观看几个视频我也不太确定。每次我运行代码并输入大写字符,它总是返回65。任何帮助都将不胜感激。

import java.util.Scanner;
public class CharConvertor
{
    public static void main (String[] args)
    {
      Scanner input = new Scanner(System.in);

      char character = 'A';
      int ascii = (int) character; 
      
      System.out.println("Please enter an uppercase character: ");
      character = input.next().charAt(0); // Allows user to choose an uppercase character
      System.out.println(character+" in ASCII is "+ascii+".");    
      
    }

}

我以为'A'就像是任何字母的占位符,但这不起作用,每次都返回65。* ,但我刚刚收到一条错误消息,说它没有初始化。

u0njafvf

u0njafvf1#

您忘记计算和打印输入(字符)的ASCII值。

import java.util.Scanner;
public class CharConvertor
{
    public static void main (String[] args)
    {
      Scanner input = new Scanner(System.in);

      char character = 'A';
      
      System.out.println("Please enter an uppercase character: ");
      character = input.next().charAt(0); // Allows user to choose an uppercase character
      System.out.println(character+" in ASCII is "+((int) character )+".");    
      
    }

}
jq6vz3qz

jq6vz3qz2#

我会使用Character类的一个方法。这也强制了输入要求。

Scanner input = new Scanner(System.in);
String str;
System.out.print("Please enter an uppercase character: ");

//Allows user to choose an uppercase character
while((str = input.next()).length() != 1 || Character.isLowerCase(str.charAt(0))) {
     System.out.print("Please enter a single uppercase character: ");
}   
System.out.println(str.charAt(0)+" in ASCII is "+ Character.codePointAt(str, 0));
zqdjd7g9

zqdjd7g93#

TL;医生

String input = scanner.nextLine();  // Gather input from user.
int codePoint = input.codePointAt( 0 );  // Get code point (ASCII code) for first character of input.
String upper = Character.toString( codePoint );  // Convert back to a `String` textual object.
int codePointOfLowercase = Character.toLowerCase( codePoint );  // Get the code point (ASCII code) of the partner lowercase character.
String lower = Character.toString( codePointOfLowercase );  // Generate a `String` textual object containing that lowercase letter.
System.out.println( "You entered uppercase letter: " + upper + " which has a code point of: " + codePoint + "." );  // Dump to console.
System.out.println( "The matching lowercase character is: " + lower + ", code point: " + codePointOfLowercase + "." );

编写超简单代码

您正在将函数组合在一行代码中。如果将每行代码限制为一个操作,通常会使调试应用更容易。
此外,超级简单的代码恰好是JVM在运行时最容易优化的。
另一个好处是,简短的代码行提供了一个机会,可以用解释性的名称来命名结果。这使您的代码更加self-documenting
因此,请注意下面的示例代码,我们是如何一次完成一个步骤,一次完成一行代码的。

代码点

其他答案是正确的。但是,这些答案和您的问题使用旧类型char
作为一个16位值,char在物理上不能表示大多数字符。char类型和它的 Package 类Character从Java 2开始就被破坏了,从Java 5开始就被继承了。
相反,养成使用code point整数的习惯。

String input = "A" ; 
int codePoint = input.codePointAt( 0 );

Unicode是US-ASCII的超集。因此前128个码位(0-127)确实是您请求的ASCII码。
测试输入是否为字母:

if( Character.isLetter( codePoint ) ) {
    … 
} else { … reject user’s input }

测试输入是否为大写:

if( Character.isUpperCase( codePoint ) ) { … } else { … reject user’s input }

将代码点转换回文本。

String upper = Character.toString( codePoint ) ;

获取大写字母的小写搭档。

int codePointOfLowercase = Character.toLowerCase( codePoint ) ;
String lower = Character.toString( codePointOfLowercase ) ;

请注意,并非所有语言都有大写和小写的概念。

示例代码

这是一个完整的示例应用程序。

package work.basil.example.text;

import java.util.Scanner;

public class UpperLower
{
    public static void main ( String[] args )
    {
        Scanner scanner = new Scanner( System.in );

        System.out.println( "Enter an uppercase letter A-Z, from basic English/Latin alphabet:" );

        String input = scanner.nextLine();  // Read user input
        int codePoint = input.codePointAt( 0 );
        if ( Character.isLetter( codePoint ) )
        {
            if ( Character.isUpperCase( codePoint ) )
            {
                String upper = Character.toString( codePoint );
                int codePointOfLowercase = Character.toLowerCase( codePoint );
                String lower = Character.toString( codePointOfLowercase );
                System.out.println( "You entered uppercase letter: " + upper + " which has a code point of: " + codePoint + "." );
                System.out.println( "The matching lowercase character is: " + lower + ", code point: " + codePointOfLowercase + "." );
            }
            else
            {
                System.out.println( "Oops, the letter you entered was not uppercase. Program ending. " );
            }
        }
        else
        {
            System.out.println( "Oops, you entered something other than a letter, such as a digit or punctuation. Program ending. " );
        }
    }
}

运行时:

A
You entered uppercase letter: A which has a code point of: 65.
The matching lowercase character is: a, code point: 97.

相关问题