Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

java.util.scanner - Java help: running 2 loops interconnected. scanner with while

Can anyone guide me on this, please? The main idea is to have 2 loops which are called to run

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**
    }
  }
}
question from:https://stackoverflow.com/questions/65876982/java-help-running-2-loops-interconnected-scanner-with-while

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You declare static void userInput() as taking no parameters, but you attempt to pass in 1 parameter: userInput(input2check). You should also make userInput static boolean userInput(), because you are looking for a true/false statement as to whether the loop should break. Therefore, in your second for loop, you would have an if statement referring to userInput():

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

You again tried to pass parameter i into static void timeUnit() which takes no parameters. Don't do it. Either declare a method like [accessModifier] [returnType] [methodName] (parameter(s)), and call it as [methodName](parameters) or declare it as [accessModifier] [returnType] [methodName](), and call it as [methodName]().

If you pass params into a method that takes none, it has no algorithm to deal with those params and will not compile. If you pass no params into a method that takes params, it will not have an algorithm to deal with the lack of params, and will not compile.

Lastly, please know that your time loop should have nothing to do with the number 60 except by coincidence. Iterating 60 times through a for loop will NOT take 60 seconds. For loops can iterate several hundreds of thousands of times per second, so your 60 iteration for loop would take a matter of nanoseconds, unless you are using something like Thread.sleep(millis, nanos) which pauses the process for the milliseconds and nanos you set it to.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...