麻烦传递参数

时间:2014-12-14 14:27:50

标签: java methods parameters

出于某种原因,当我测试我的代码时,我的随机生成的数字不会出现。以下是一个示例结果:

  

您想要生成什么,整数,双精度或字符?

     

整数

     

您要生成的整数的上限和下限是多少?

     

10

     

1

     

您想要生成多少个整数?

     

10

     

建立成功(总时间:9秒)

这是因为我的代码没有正确传递我的参数吗?我也不确定如何解决这个问题。

以下是我的参考代码(目前尚未完成)

import java.util.Scanner;
public class NewNumberCharacter {
    /** Main method
     * @param args */
public static void main(String[] args) {
int return_int;
double return_double;
char return_Uchar;
char return_Lchar;
int Min_int;
int Max_int;
double Min_double;
double Max_double;
    // 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();
    }
    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();
    }
    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();
    }
}
}
// Pass the parameters to a method
public class RandomDataGenerator {
public static int random(int minn2, int maxn1, 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++){
       System.out.println(maxn3 + (int)(Math.random() * minn4));

    }
    return 0;
}
public static void random(int numcharacter){

    for (int i = 0; i < numcharacter;  i++){
        System.out.println();
    }
}
}

代码更新

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);
    }
}
// Pass the parameters to a method
public 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++){
       System.out.println(maxn3 + (int)(Math.random() * minn4)); 
    }
    return 0;
}
public static void random(int numcharacter){
    for (int i = 0; i < numcharacter;  i++){
        System.out.println();
    }
}
}
}

2 个答案:

答案 0 :(得分:0)

您没有调用打印输出的任何random方法(RandomDataGenerator类)。这就是你没有看到它的原因。

例如,调用RandomDataGenerator.random(minn2, maxn1, numinteger);打印随机生成的整数:

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

答案 1 :(得分:0)

您需要调用生成随机数的方法,如下所示:

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

    RandomDataGenerator.random(numcharacter);//Call the methods
}
else 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

}
else 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); //Call the method
}

请注意,您尚未完成制作随机字符的随机方法。因此,当您致电

时,除了新行之外什么都不会打印
 RandomDataGenerator.random(numcharacter);