简体   繁体   English

如何将null返回给int方法

[英]how to return null to int method

iam new to programming.iam wrking on a quiz game software. iam是programming.iam上的新手,它使用问答游戏软件。 here i want a count down to be printed as "3,2,1,go...go...go" 在这里,我希望倒数打印为“ 3,2,1,go ... go ... go”

  package t2;

import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;

public class Stopwatch {
static int interval;
static Timer timer;

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int delay = 1000;
    int period = 1000;
    timer = new Timer();
    interval = 3;
    System.out.println("3");
    timer.scheduleAtFixedRate(new TimerTask() {

        public void run() {
            System.out.println(setInterval());

        }
    }, delay, period);
}



 private static final int setInterval() {
    String go="go...go...go";
    if (interval == 2)
    {

        timer.cancel();
        return go;
    }

      return --interval;

}
}

this says that setInterval() has int as return value.if i place System.out.print("go"); 这表示setInterval()具有int作为返回值。如果我放置System.out.print("go"); in the place of return go; return go;的地方return go; it prints go and prints 1 which is out of requirement. 它打印go并打印超出要求的1。 please any buddy can tell me how to do this. 请任何伙伴告诉我该怎么做。

One option would be to change your setInterval() method to return a string like such: 一种选择是更改setInterval()方法以返回如下字符串:

private static final String setInterval() {
    String go="go...go...go";
    if (interval == 2)
    {

        timer.cancel();
        return go;
    }

    --interval;
    return String.valueOf(interval);
}

Change the return type of setInterval to object to take advantage of the overloaded println method and the conditional should check for 1 instead of 2. Also use print instead of println to have the output on the same line. setInterval的返回类型更改为object,以利用重载的println方法,并且该条件应检查1而不是2。还可以使用print代替println来使输出位于同一行。

private static final Object setInterval() {
    String go="go...go...go";
    if (interval == 1)
    {

        timer.cancel();
        return go;
    }
    return --interval;
}

Full Example 完整的例子

 package t2;

    import java.util.Scanner;
    import java.util.Timer;
    import java.util.TimerTask;

    public class Stopwatch {
        static int interval;
        static Timer timer;

        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int delay = 1000;
            int period = 1000;
            timer = new Timer();
            interval = 3;
            System.out.print("3,");
            timer.scheduleAtFixedRate(new TimerTask() {

                public void run() {
                    System.out.print(setInterval() + ",");

                }
            }, delay, period);
        }

        private static final Object setInterval() {
            String go = "go...go...go";
            if (interval == 1) {

                timer.cancel();
                return go;
            }

            return --interval;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM