0
import java.util.Scanner;

public class Separate {

   public static void main(String[] args) {
         Scanner user_input = new Scanner( System.in ); 
    String variable;
    System.out.print("Enter Variable:");
      variable = user_input.next();
          Separate(variable);
   }

   public static void Separate(String str) {
          String number = "";
          String letter = "";
      String symbol = "";
          for (int i = 0; i < str.length(); i++) {
                 char a = str.charAt(i);
                 if (Character.isDigit(a)) {
                       number = number + a;

                 } else {
                       letter = letter + a;


         }
          }
          System.out.println("Alphabets in string:"+letter);
          System.out.println("Numbers in String:"+number);   
   }

}

好的,我已经有了这个代码来分隔我输入的数字和字母。问题是,当我输入符号时,例如 (^,+,-,%,*) 它也表示为字母。

我想要做的是将符号与字母分开,就像我在数字和字母上所做的那样,并为其制作另一个输出。

4

5 回答 5

3
public static void separate(String string) {
        StringBuilder alphabetsBuilder = new StringBuilder();
        StringBuilder numbersBuilder = new StringBuilder();
        StringBuilder symbolsBuilder = new StringBuilder();
        for (int i = 0; i < string.length(); i++) {
            char ch = string.charAt(i);
            if (Character.isAlphabetic(ch)) {
                alphabetsBuilder.append(ch);
            } else if (Character.isDigit(ch)) {
                numbersBuilder.append(ch);
            } else {
                symbolsBuilder.append(ch);
            }
        }
        System.out.println("Alphabets in string: " + alphabetsBuilder.toString());
        System.out.println("Numbers in String: " + numbersBuilder.toString());
        System.out.println("Sysmbols in String: " + symbolsBuilder.toString()); 
    }
于 2013-02-27T16:28:18.850 回答
1

您正在测试 character isDigitelse将其视为字母。好吧,如果它不是数字,则所有其他情况都转到else您的代码中。也为这些符号创建一个else案例。

于 2013-02-27T16:07:18.893 回答
0

由于这是家庭作业,只需查看Character 的文档,它具有很好的功能isDigit

于 2013-02-27T16:10:23.830 回答
0
public static void Separate(String str) 
{
    String number = "";
    String letter = "";
    String symbol = "";

    for (int i = 0; i < str.length(); i++) 
    {
        char a = str.charAt(i);
        if (Character.isDigit(a)) 
        {
            number = number + a;
            continue;
        } 
        if(Character.isLetter(a))
        {
            letter = letter + a;
        }
        else
        {
            symbol = symbol + a;
        }
    }
    System.out.println("Alphabets in string:"+letter);
    System.out.println("Numbers in String:"+number);   
}
于 2013-02-27T16:14:05.180 回答
0
import java.util.Scanner;

public class Separate {

public static void main(String[] args) {

      Scanner user_input = new Scanner( System.in );

      String variable;

      System.out.print("Enter Variable:");

      variable = user_input.next();

      Separate(variable);
   }

         public static void Separate(String str) 
         {
          String number = "";

          String letter = "";

          String symbol = "";

          for (int i = 0; i < str.length(); i++) {

                 char a = str.charAt(i);

                 if (Character.isDigit(a)) {
                       number = number + a;

                 } else if (Character.isLetter(a)) {
                       letter = letter + a;
                 } else {
                       symbol = symbol + a;
         }
       }
     System.out.println("Alphabets in string:"+letter);
     System.out.println("Numbers in String:"+number);
     System.out.println("Symbols in String:"+symbol);   
   }

}
于 2018-12-13T15:16:19.200 回答