Java 程序将小写字母转换为大写字母

编写一个 Java 程序,用示例将小写字母转换为大写字母。在此从小写到大写的示例中,我们在给定的 lowStr 字符串上使用了内置的 toUpperCase() 函数。

import java.util.Scanner;

public class LowerToUpper1 {
	private static Scanner sc;
	public static void main(String[] args) {
		String lowStr;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter Lowercase String =  ");
		lowStr = sc.nextLine();
	
		String lowStr2 = lowStr.toUpperCase();	
		System.out.println("\nThe Uppercase String  =  " + lowStr2);
	}
}
Java Program to Convert Lowercase to Uppercase Example

在此转换小写字母为大写字母的示例中,我们首先使用 toCharArray() 将 lowStr 字符串转换为 ch 字符数组。 

接下来,我们使用 For 循环从头到尾迭代 ch 字符数组。在循环中,我们检查每个索引位置的字符是否大于或等于 a 且小于或等于 z。如果为真,我们将当前 ASCII 值减去 32。例如,a = 97,减去 32 后变为 65,而 A 的 ASCII 值是 65。

import java.util.Scanner;

public class LowerToUpper2 {
	private static Scanner sc;
	public static void main(String[] args) {
		String lowStr;
		int i;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter Lowercase String =  ");
		lowStr = sc.nextLine();
		
		char[] ch = lowStr.toCharArray();
		
		for(i = 0; i < ch.length; i++)
		{
			if(ch[i] >= 'a' && ch[i] <= 'z') {
				ch[i] = (char) ((int)ch[i] - 32);;
			}
		}
		System.out.print("\nThe Uppercase String  =  ");
		for(i = 0; i < ch.length; i++) {
			System.out.print(ch[i]);
		}
	}
}

将小写文本转换为大写输出

Please Enter Lowercase String =  java programming

The Uppercase String  =  JAVA PROGRAMMING

Java 程序将小写字母转换为大写字母示例 3

在这里,我们没有将 lowStr 字符串更改为 char 数组。接下来,我们使用 lowStr 上的 charAt 函数获取索引位置的字符。然后,我们将每个字符分配给 Java uppStr2。

import java.util.Scanner;

public class LowerToUpper3 {
	private static Scanner sc;
	public static void main(String[] args) {
		String lowStr, uppStr2 = "";
		int i;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter Lowercase String to convert =  ");
		lowStr = sc.nextLine();
		
		char ch;
		
		for(i = 0; i < lowStr.length(); i++)
		{
			if(lowStr.charAt(i) >= 'a' && lowStr.charAt(i) <= 'z') {
				ch = (char) (lowStr.charAt(i) - 32);
			}
			else {
				ch = (char) (lowStr.charAt(i));
			}
			uppStr2 += ch;
		}
		System.out.print("\nThe Uppercase String  =  " + uppStr2);
	}
}

转换小写到大写输出

Please Enter Lowercase String to convert =  tutorial gateway

The Uppercase String  =  TUTORIAL GATEWAY

在此从小写字母到大写字母的示例中,我们没有将其分配给新的 uppStr2 字符串,而是在循环中打印每个字符。

import java.util.Scanner;

public class LowerToUpper4 {
	private static Scanner sc;
	public static void main(String[] args) {
		String lowStr;
		int i;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter Lowercase String to convert =  ");
		lowStr = sc.nextLine();
	
		System.out.print("\nThe Uppercase String =  ");
		for(i = 0; i < lowStr.length(); i++)
		{
			char ch = lowStr.charAt(i);
			if(ch >= 'a' && ch <= 'z') {
				ch = (char) (ch - 32);
			}
			System.out.print(ch); // Str2 += ch;
		}
	}
}
Please Enter Lowercase String to convert =  learn Java

The Uppercase String =  LEARN JAVA

在此 Java 程序中,使用 ASCII 值将小写字母转换为大写字母,我们通过比较 ASCII 值而不是比较字符。

import java.util.Scanner;

public class LowerToUpper5 {
	private static Scanner sc;
	public static void main(String[] args) {
		String lowStr, uppStr2 = "";
		int i;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter Lowercase String =  ");
		lowStr = sc.nextLine();
	
		for(i = 0; i < lowStr.length(); i++)
		{
			char ch = lowStr.charAt(i);
			if(ch >= 97 && ch <= 122) {
				ch = (char) (ch - 32);
			}
			uppStr2 += ch;
		}
		System.out.print("\nThe Uppercase String =  " + uppStr2);
	}
}
Please Enter Lowercase String =  lowercase char

The Uppercase String =  LOWERCASE CHAR

将小写字母转换为大写字母的代码,我们将逻辑分离到函数中。

import java.util.Scanner;

public class LowerToUpper6 {
	private static Scanner sc;
	public static void main(String[] args) {
		String lowStr, uppStr2;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter Lowercase String =  ");
		lowStr = sc.nextLine();
		
		uppStr2 = toUpper(lowStr);
		System.out.print("\nThe Uppercase String =  " + uppStr2);
	}
	
	public static String toUpper(String lowStr) {
		String uppStr2 = "";
		
		for(int i = 0; i < lowStr.length(); i++)
		{
			char ch = lowStr.charAt(i);
			if(ch >= 97 && ch <= 122) {
				ch = (char) (ch - 32);
			}
			uppStr2 += ch;
		}
		return uppStr2;
	}
}
Please Enter Lowercase String =  java lowercase coding

The Uppercase String =  JAVA LOWERCASE CODING