gpt4 book ai didi

java - 如何使用 ASCII 值生成字符

转载 作者:行者123 更新时间:2023-12-01 12:07:44 27 4
gpt4 key购买 nike

对于我的程序的一部分,我试图要求用户输入生成多个随机字符的信息。我已经对整数和 double 完成了此操作,那么如何对使用 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;
}
}

更新V2

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

最佳答案

你会用类似的方式来做。首先,你需要了解角色。查找关键字,例如 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() 从函数中移至调用方。
  • 您可能不希望在每次循环迭代中生成新的随机数生成器。一个自己的全局就足够了。

关于java - 如何使用 ASCII 值生成字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27471200/

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