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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5099449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:26:21  来源:igfitidea点击:

How to get a range of characters? (alphabet)

java

提问by jona

I have been working on this for hours and now Im kinda stuck....please help me. Im a complete programming handicap. All the methods work fine except the alphabet one.

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

It will receive two characters (either upper or lower case) and return a string composed of the range of char values given. Maintain the same case (upper or lower) that was passed in to the method. If an upper case and a lower case char (one of each) was passed to the method, convert the upper case char into lower case and use the lower case range. Note, the range will be inclusive of the starting char and exclusive of the ending char. Also, observe that if the starting (first) char given is greater than the ending (second) char, for example 'm' and 'h', then the method will return an empty string since there are no chars in this range.

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

Can you give me some help on how I can do the above on the alphabet method?

你能给我一些关于如何在字母表方法上执行上述操作的帮助吗?

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);

}
}

Thanks!

谢谢!

回答by AlfaTeK

With a char you can just ++ it to get the next char and so on.

使用字符,您只需 ++ 即可获取下一个字符,依此类推。

char a = 'a';
a++; // now you have b
a++; // now you have c

Just do a while loop to go from start to end char.

只需做一个while循环即可从开始到结束字符。

回答by Mourad El Aomari

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

it's very simple ^_^

很简单^_^

回答by shnkgn

Use this to first to generate a random letter. You first have to generate a random number within a certain range. Then when you get the number back and store it in a char variable, it shows it as a letter.

首先使用它来生成一个随机字母。您首先必须在一定范围内生成一个随机数。然后,当您取回数字并将其存储在 char 变量中时,它会将其显示为一个字母。

char letter = 0;
letter = (char) (65 + (char)(Math.random() * (90 - 65) + 1));

回答by Aaron Gage

This answaer assumes you are just talking about standard keyboard characters from the ASCII set.

这个答案假设您只是在谈论 ASCII 集中的标准键盘字符。

Take the ascii codes for the 2 charaters and create a loop:

取 2 个字符的 ascii 代码并创建一个循环:

StringBuilder buf = new StringBuilder();

for (int i = rep1; i <= rep2; ++i) 
    buf.append((char)i);

return buf.toString();

This will work as they both need to be same case...

这将起作用,因为它们都需要相同的情况......

回答by Ian Turton

public static String alphabet(char letter1, char letter2) { 
    StringBuffer out = new StringBuffer();    
    for (char c = letter1; c < letter2; c++) {
        out.append(c);
    }
    return out.toString();
}

Obviously you should add some error checking and handling

显然你应该添加一些错误检查和处理

回答by Abraham Adam

public static String alphabet( char letter1, char letter2){

公共静态字符串字母表(字符字母1,字符字母2){

expects to return a String because you said public static "String"

期望返回一个字符串,因为您说的是公共静态“字符串”

take out the String and this should work, replace it by void if you just want to print with System.out.print. or return the string that your making

取出字符串,这应该可以工作,如果您只想使用 System.out.print 打印,请将其替换为 void。或返回您制作的字符串

beside that my advise for going to uppercase and lowercase would have been to convert the char to a String and just use the java built in method.

除此之外,我对大写和小写的建议是将 char 转换为 String 并只使用 java 内置方法。