调用仅显示输出模式的方法

时间:2017-11-25 19:17:03

标签: java int static-methods

我的第二种方法很难,方法声明是:     public static void displayOutput(int loopCount)main()调用该方法,并传递确定重复的有效输入值。该方法仅显示输出模式,不返回任何内容。每隔3行显示一个空格和3个星号

我知道我没有在main()中调用每个方法,我知道displayOutput(int loopCout)是错误的。

有人可以向我解释这个或者使用一个有助于编写程序的例子吗?

public static void main(String[] args) {
   int repeat;
   Scanner goGet = new Scanner(System.in); 
   repeat = getValidValue(goGet); //Uncompilable source code -Erroneous sym type

   displayOutput(repeat);
}

public static int getValidValue() {
    int input;

    do {
        Scanner getInfo = new Scanner(System.in);
        System.out.print("Enter an integer Greater than zero: --> ");
        input = getInfo.nextInt();

    } while (input <= 0);

    return input;
}

public static int displayOutput(int loopCount) {
    int i; 
    for (i = 0; i < loopCount; i++) {
        System.out.print("The semester is ending soon. ");
        System.out.print("The semester is ending soon. ");
        System.out.print("The semester is ending soon.*** ");            
    }

    return loopCount;
}

2 个答案:

答案 0 :(得分:1)

您将值传递给方法 getValidValue ,该值不带任何值。

同样 displayOutput 正在返回loopcount,但你没有把它抓到任何地方,所以在星号后它没有显示任何内容。

答案 1 :(得分:0)

getValidValue()此方法不带参数 但在调用它时你已经传递了一个参数。这就是造成错误的原因。

更改此方法的原型,以便它接受参数

public static int getValidValue(Scanner obj)

或 只需从方法调用中删除参数

repeat = getValidValue();