首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java帮助:运行2个相互连接的循环。带有while的扫描仪

Java帮助:运行2个相互连接的循环。带有while的扫描仪
EN

Stack Overflow用户
提问于 2021-01-25 07:09:09
回答 1查看 11关注 0票数 0

有人能在这方面给我指点一下吗?主要思想是有两个循环,它们被调用来运行

代码语言:javascript
复制
public class time9 {
   static void userInput() {
      Scanner input = new Scanner(System.in);
      String input2check = input.next();
      //condition when there is an input to stop loop
   }
   static void timeUnit() {
      for(int i = 1; i<60; i++) {
        **//this makes seconds, minutes and hours**
      }
   }
  public static void main(String[] args) {
    for (int i = 1; i<60; i++){
       userInput(input2check);
       timeUnit(i);
    **// for every i in timeUnit check user input.
    // if no user input, time loop continues
    // if there is user input, stop time loop**
    }
  }
}
EN

回答 1

Stack Overflow用户

发布于 2021-01-28 12:38:55

您声明static void userInput()不接受任何参数,但是您尝试传入一个参数:userInput(input2check)。您还应该使用userInput static boolean userInput(),因为您正在寻找关于循环是否应该中断的真/假陈述。因此,在第二个for循环中,会有一条if语句引用userInput()

代码语言:javascript
复制
    public class time9 {
       static boolean userInput() {
          Scanner input = new Scanner(System.in);
          String input2check = input.next();
   return (input2check != null);
 /*true/false statement: return whether input2check is not null. Return true if not null, false otherwise.*/
          
       }
       static void timeUnit() {
          for(int i = 1; i<60; i++) {
            **//this makes seconds, minutes and hours**
          }
       }
      public static void main(String[] args) {
        for (int i = 1; i<60; i++){
           if (userInput()){break;}
           else{timeUnit);
        }
      }
    }

您再次尝试将参数i传递给不接受任何参数的static void timeUnit()。不要这样做。要么声明一个像[accessModifier] [returnType] [methodName] (parameter(s))这样的方法并将其调用为[methodName](parameters),要么将其声明为[accessModifier] [returnType] [methodName]()并将其调用为[methodName]()

如果您将params传递给一个不接受任何参数的方法,则它没有处理这些params的算法,也不会进行编译。如果你没有将params传递给一个接受params的方法,那么它将没有一个算法来处理缺少params的问题,也不会进行编译。

最后,请注意,您的时间循环应该与数字60无关,除非是巧合。在for循环中迭代60次不会花费60秒。For循环每秒可以迭代数十万次,因此您的60次for循环将花费大约纳秒的时间,除非您正在使用Thread.sleep(millis, nanos)之类的东西,它会将过程暂停您设置的毫秒和nanos。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65876982

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档