gpt4 book ai didi

java - 如何获取一系列字符? (字母)

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:50:46 25 4
gpt4 key购买 nike

我已经为此工作了几个小时,现在我有点卡住了....请帮助我。我是一个完全的编程障碍。除字母表方法外,所有方法都可以正常工作。

它将接收两个字符(大写或小写)并返回由给定 char 值范围组成的字符串。保持传递给方法的相同大小写(大写或小写)。如果将大写字符和小写字符(各一个)传递给该方法,则将大写字符转换为小写字符并使用小写范围。请注意,该范围将包括起始字符,不包括结束字符。此外,请注意如果给定的起始(第一个)字符大于结束(第二个)字符,例如“m”和“h”,则该方法将返回一个空字符串,因为此范围内没有任何字符。

你能帮我一些关于如何在字母表方法上做上述事情的帮助吗?

import java.util.*;

class CharacterOperations
{
public static void run()
{
int number=1;
Scanner scanner = new Scanner(System.in);
while(number > 0)
{
System.out.println("(1) Insert 1 to change a letter from its lower case value to its upper case value");
System.out.println("(2) Insert 2 to change a letter from its upper case value to its lower case value ");
System.out.println("(3) Insert 3 for the alphabet method (range of two letters) ");
System.out.println("Enter a number (or negative to quit): ");
number = scanner.nextInt();

if (number == 1)
{
System.out.print("Enter a lower case letter: ");
String a= scanner.next();
char letter = (char) a.charAt(0);
toUpper(letter);
}
else if (number == 2)
{
System.out.print("Enter an upper case letter: ");
String a= scanner.next();
char letter = (char) a.charAt(0);
toLower(letter);
}
else if (number == 3)
{
System.out.print("Enter an upper case or lower case letter: ");
System.out.print("Enter an upper case or lower case letter: ");
String a= scanner.next();
char letter1 = (char) a.charAt(0);
String b= scanner.next();
char letter2 = (char) b.charAt(0);
alphabet(letter1, letter2);
}
}
}

public static char toUpper(char letter)
{
int rep = ((int)letter - 32);
char ltr = (char)rep;
System.out.println("The letter "+ ltr + " integer representation is: " + rep);
return (char) ((int) letter -32);
}

public static char toLower(char letter)
{
int rep = (int)(letter + 32);
char ltr = (char)rep;
System.out.println("The letter " + ltr + " integer representation is: " + rep);
return (char) ((int) letter + 32);
}

public static String alphabet( char letter1, char letter2){
int rep1 = (int)letter1;
int rep2 = (int)letter2;
char ltr1 = (char)rep1;
char ltr2 = (char)rep2;
System.out.println("The letter " + ltr1 + " integer representation is: " + rep1);
System.out.println("The letter " + ltr2 + " integer representation is: " + rep2);

}
}

谢谢!

最佳答案

for (char c = 'a'; c <= 'z'; c++) {
System.out.println(c);
}

很简单^_^

关于java - 如何获取一系列字符? (字母),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5099449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com