有人能在这方面给我指点一下吗?主要思想是有两个循环,它们被调用来运行
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**
}
}
}发布于 2021-01-28 12:38:55
您声明static void userInput()不接受任何参数,但是您尝试传入一个参数:userInput(input2check)。您还应该使用userInput static boolean userInput(),因为您正在寻找关于循环是否应该中断的真/假陈述。因此,在第二个for循环中,会有一条if语句引用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);
}
}
}您再次尝试将参数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。
https://stackoverflow.com/questions/65876982
复制相似问题