如何使用ASCII值生成字符

时间:2014-12-14 16:12:06

标签: java random ascii

对于我的部分程序,我试图询问用户有关生成多个随机字符的输入。我已经用整数和双精度完成了这个,我如何使用ASCII值对字符进行此操作?我是否会使用与生成整数相同的格式(在代码中显示)?

import java.util.Random;
import java.util.Scanner;
public class NewNumberCharacter {
public static void main(String[] args) {
    // Create a Scanner
    Scanner input = new Scanner(System.in);

    // Ask the user to enter in the command: integer, double, or character
    System.out.println("What do you want to generate, integer, double, or character?");

    // Prompt the user to enter a string, or command, then follow the prompts
    String command = input.nextLine();

    if(command.equals("character")){
        System.out.println("How many characters would you like generated?");

    int numcharacter = input.nextInt();

    RandomDataGenerator.random(numcharacter);
    }
    if (command.equals("integer")){
        System.out.println("What is the upper limit and lower limit of the integers you want to generate?");  

    int maxn1 = input.nextInt();
    int minn2 = input.nextInt();

    System.out.println("How many integers do you want to generate?");
    int numinteger = input.nextInt();

      RandomDataGenerator.random(minn2,maxn1,numinteger); //Call the method
    }
    if (command.equals("double")){
        System.out.println("What is the upper limit and lower limit of the doubles you want to generate?");

    double maxn3 = input.nextDouble();
    double minn4 = input.nextDouble();

    System.out.println("How many doubles do you want to generate?");

    int numdouble = input.nextInt();

    RandomDataGenerator.random(maxn3,minn4,numdouble);
    }
}
}class RandomDataGenerator {

public static int random(int maxn1, int minn2, int numinteger){
    for(int i = 0 ; i < numinteger ; i++ ) {  
        System.out.println(maxn1 + (int)(Math.random()* minn2));    
    }    
    return 0;   
}
public static double random(double maxn3, double minn4, int numdouble){

    for (int i = 0; i < numdouble; i++){
       Random r = new Random();
       System.out.println(minn4 + (maxn3- minn4) * r.nextDouble()); 
    }
    return 0;
}
public static String random(int numcharacter){
    for (int i = 0; i < numcharacter;  i++){
        System.out.println();
    }
    return null;
}
}

UpdateV2

import java.util.Random;
import java.util.Scanner;
public class NewNumberCharacter {
public static void main(String[] args) {
    // Create a Scanner
    Scanner input = new Scanner(System.in);

    // Ask the user to enter in the command: integer, double, or character
    System.out.println("What do you want to generate, integer, double, or character?");

    // Prompt the user to enter a string, or command, then follow the prompts
    String command = input.nextLine();

    if(command.equals("character")){
        System.out.println("Enter in an ASCII value for the character" + 
                "\n 33 to 47 for special characters" +
                "\n 65 to 90 for uppercase letters" + 
                "\n 97 to 102 for lowercase letters");

        int ascii1 = input.nextInt();
        int ascii2 = input.nextInt();

        System.out.println("How many characters would you like generated?");

    int numcharacter = input.nextInt();

    RandomDataGenerator.random(ascii1,ascii2,numcharacter);
    }
    if (command.equals("integer")){
        System.out.println("What is the upper limit and lower limit of the integers you want to generate?");  

    int maxn1 = input.nextInt();
    int minn2 = input.nextInt();

    System.out.println("How many integers do you want to generate?");
    int numinteger = input.nextInt();

      RandomDataGenerator.random(minn2,maxn1,numinteger); //Call the method
    }
    if (command.equals("double")){
        System.out.println("What is the upper limit and lower limit of the doubles you want to generate?");

    double maxn3 = input.nextDouble();
    double minn4 = input.nextDouble();

    System.out.println("How many doubles do you want to generate?");

    int numdouble = input.nextInt();

    RandomDataGenerator.random(maxn3,minn4,numdouble);
    }
}
}
class RandomDataGenerator {

public static int random(int maxn1, int minn2, int numinteger){
    for(int i = 0 ; i < numinteger ; i++ ) {  
        System.out.println(maxn1 + (int)(Math.random()* minn2));    
    }    
    return 0;   
}
public static double random(double maxn3, double minn4, int numdouble){

    for (int i = 0; i < numdouble; i++){
       Random r = new Random();
       System.out.println(minn4 + (maxn3- minn4) * r.nextDouble()); 
    }
    return 0;
}
public static char randChar(int ascii1 , int ascii2 , int numcharacter) {
    for (int i = 0; i < numcharacter; i++){
        Random r = new Random();
    }
     return(char)(Random.nextInt(ascii1-ascii2+1) + ascii1);
}
}

2 个答案:

答案 0 :(得分:1)

你会以类似的方式做到这一点。首先,您需要了解角色。查找关键字,如 ASCII表 Unicode 。 然后选择您想要随机字符的字母表。打印随机中文字符与打印随机拉丁字符略有不同。此外,打印随机拉丁字符取决于您是否只想从基本字母表打印它们,或者是否要包含特定脚本中的其他字符,即德语变音符号和德语sz连字符。

以下示例演示如何为大写拉丁字符创建随机字符串。

private static final Random random = new Random();
public static String random(final int numChars) {
    final StringBuilder sb = new StringBuilder();
    for (int i = 0; i < numChars; i++) {
        sb.append((char) (random.nextInt(26) + 'A'));
    }
    return sb.toString();
}

顺便说一句,你可能想重新考虑一下你的程序:

  • 您可能希望返回计算值而不是在函数中打印它们。
  • 您可能希望将System.out.println()从函数中移出调用者。
  • 您可能希望不在每次循环迭代中生成新的随机数生成器。一个全球性的应该就足够了。

答案 1 :(得分:0)

如果您需要随机范围,可以

static final Random rand = new Random();
public static char randChar(char first, char last) {
     return (char)(rand.nextInt(last-first+1) + first);
}