随机数据生成器整数和字符

时间:2014-12-22 01:56:48

标签: java netbeans

我必须创建一个RandomDataGenerator。代码我有点工作,但我的部分内容有问题。我遇到的第一个问题是整数。这是我运行时得到的: 运行:

您想要产生什么?
i =整数,d = double,c = char

你想要多少个整数?

10

为整数选择低值:

3

为整数选择高值:

7

整数:

4 3 6 6 6 五 2 3 五 6

它给了我10个数字,但它不应该低于3.

我遇到的另一个问题是我得到的字符

运行: 你想生成什么?
i =整数,d = double,c = char

C

你想要多少个角色?

5

为角色选择下限:

C

为角色选择上限:

取值

点 假

我该如何解决这个问题?

package javaapplication5;

import java.util.Random;
import java.util.Scanner;



public class JavaApplication5 {


private static int a;


public static void main(String[] args) {


    System.out.println("What do you want to generate?  ");
    System.out.println("i = integer, d = double, c = char" );
    char i, d, c; 
    int low_int = 0,high_int = 0, quant_int = 0; 
    double low_double = 0, high_double = 0, quant_double = 0; 
    char low_char = 0, high_char = 0, quant_char = 0; 
    int result = 0; 
    String input; 

    Scanner in = new Scanner (System.in);
String A= in.next();

int largo = A.length();
char select []= new char [largo];


   select[a]=A.charAt(a);

 for(int q=0; q<largo;q++){
 if (select[q]=='i') {
    System.out.println( " How many integers do you want? ");

    Scanner Integerswanted = new Scanner(System.in);
String B= in.next();

        quant_int = Integer.parseInt(B); 

        System.out.println( " Choose low value for your integer: "); 
       Scanner LoVa = new Scanner(System.in);
String LV= in.next();
        low_int = Integer.parseInt(LV);


       System.out.println( " Choose high value for your integer: ");
        Scanner HiVa = new Scanner(System.in);
String hV= in.next(); 
       high_int = Integer.parseInt(hV); 
       System.out.println( " Integers: ");
       int random_generator = JavaApplication5.random_generator(quant_int, low_int, high_int); 
       int s = random_generator;
       if(s>low_int && s<high_int){


       }


 } 

 else if (select[q]=='d'){
     System.out.println( " How many doubles do you want? "); 

    Scanner DoublesWanted = new Scanner(System.in);
String R= in.next();

        quant_double = Double.parseDouble(R); 

        System.out.println(" Choose low value for your double: "); 
        Scanner LoDo = new Scanner(System.in);
String LD= in.next();
low_double = Double.parseDouble(LD); 


System.out.println(" Choose high value for your double: "); 
Scanner HiDo = new Scanner(System.in);
String HD= in.next();
        high_double = Double.parseDouble(HD); 
        System.out.println( " Doubles: ");
        double random_generator = JavaApplication5.random_generator(quant_double, low_double,         high_double); 
        double k = random_generator; 


     }
 else if (select[q]=='c'){
      System.out.println( " How many characters do you want? ");
      Scanner CharWanted = new Scanner(System.in);
String Cw= in.next();
        quant_char = A.charAt(0); 

       System.out.println( " Choose low limit for your character: "); 
       Scanner LoCh = new Scanner(System.in);
String LC= in.next();
        low_char = A.charAt(0); 

        System.out.println( " Choose high limit for your character: ");
          Scanner HiCh = new Scanner(System.in);
String HC= in.next();
        high_char = A.charAt(0); 

        char random_generator = JavaApplication5.random_generator(quant_char, low_char, high_char); 
        char e = random_generator; 

 } }}
public static int random_generator(int quant, int low, int high){ 



    Random rand_num = new Random(); 
    int return_int = 0; 
    for(int z = low; z <= quant + 2 ; z++) 

    System.out.println(rand_num.nextInt(high) + 1 ); 
    return return_int; 
} 

public static double random_generator(double quant, double low, double high){ 
    Random rand_num = new Random(); 
    double return_double = 0; 
    for(double z = low; z <quant + 2; z++) {
    System.out.println(rand_num.nextDouble() + low ); 
    }
    return return_double; 

} 

public static char random_generator(char quant, char low, char high){ 
    String [] abecedario = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", 
"K", "L", "M","N","O","P","Q","R","S","T","U","V","W", "X","Y","Z" };

    int Rand_num = (int) Math.round(Math.random() * 26 ) ;

System.out.println( abecedario[Rand_num] );Random 
    rand_num = new Random(); 
    char return_char = 0; 
    for(double z = low; z < quant + 1; z++) 
    System.out.println(abecedario[Rand_num].equals(high));  
    return return_char; 
} 
}

1 个答案:

答案 0 :(得分:0)

对于整数,您应该使用:

rand_num.nextInt(high - low) + high;

对于Character,您应该使用相同的东西,除非您在找到随机整数后需要将其强制转换为字符。

(char)rand_num.nextInt(high - low) + high;