Home > Java > javaTutorial > body text

Introduction to the method of converting letters from upper to lower case in Java

黄舟
Release: 2017-06-04 09:22:20
Original
3687 people have browsed it

This article mainly introduces the method of Java letter case conversion in detail, which has certain reference value. Interested friends can refer to the

case:

It is required to receive a letter input by the user from the console. If the letter is lowercase, convert it to uppercase; if the letter is uppercase, convert it to lowercase; only letters can be entered. If it is other values, it will prompt that the data is incorrect. !

Implementation code:

import java.util.Scanner;

/**
 * 常见字母大小写转换
 * 原理:
 *   字符char采用的是Unicode编码的16位字符类型,其表示范围是0-65536。标准的8位ASCII字符集是Unicode
 *   的子集,其取值范围为0-127。大小写字母之间正好相差32
 * @author Administration
 *
 */
public class WordChange {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("请输入您需要转换的字母:");
    char c = input.next().charAt(0);
    change(c);
  }

  private static void change(char c) {
    //如果输入的是大写,+32即可得到小写
    if(c>=&#39;A&#39; && c<=&#39;Z&#39;){
      c+=32;
      System.out.println("您输入的大写字母"+(char)(c-32)+"被转换成了"+c);
    }else if(c>=&#39;a&#39; && c<=&#39;z&#39;){  //如果输入的是小写,-32即可得大小写
      c-=32;
      System.out.println("您输入的小写字母"+(char)(c+32)+"被转换成了"+c);
    }else{
      System.out.println("输入的字符有误!!");
    }
  }
}
Copy after login

Principle analysis:

The character char uses a 16-bit character type encoded by Unicode. Its representation range is 0-65536. The standard 8-bit ASCII character set is a subset of Unicode, and its value range is 0-127. The difference between uppercase and lowercase letters is exactly 32

The above is the detailed content of Introduction to the method of converting letters from upper to lower case in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template