简体   繁体   English

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

[英]How to get a range of characters? (alphabet)

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.它将接收两个字符(大写或小写)并返回一个由给定的 char 值范围组成的字符串。 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.另外,请注意,如果给定的起始(第一个)字符大于结束(第二个)字符,例如“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!谢谢!

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

it's very simple ^_^很简单^_^

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循环即可从开始到结束字符。

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

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...这将起作用,因为它们都需要相同的情况......

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显然你应该添加一些错误检查和处理

Simple solution:简单的解决方案:

    String str = "A"
    char cr = str.charAt(0);
    
    System.out.println((cr += 1)); //got B

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.取出字符串,这应该可以工作,如果您只想使用 System.out.print 打印,请将其替换为 void。 or return the string that your making或返回您制作的字符串

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 内置方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM