简体   繁体   English

Mooc Java练习78,有界计数器不接受用户输入

[英]mooc java exercise 78, bounded counter not accepting user input

Trying to learn java with my son. 试图和我儿子一起学习Java。 I've Googled every combination of words but can't seem to find an answer. 我已经搜索了单词的所有组合,但似乎找不到答案。 I would appreciate any help or direction. 我将不胜感激任何帮助或指示。

The program is not taking in the user input for mins/hrs to initiate counter. 程序没有在分钟/小时内接受用户输入以启动计数器。 So counter starts at 00:00:50 for input of 23:59:50. 因此,计数器从00:00:50开始输入23:59:50。 Here is my code to date: 这是我到目前为止的代码:

Main Class: 主类:

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);

    BoundedCounter seconds = new BoundedCounter(59);
    BoundedCounter minutes = new BoundedCounter(59);
    BoundedCounter hours = new BoundedCounter(23);

    System.out.print("seconds: ");
    int s = Integer.parseInt(reader.nextLine());
    System.out.print("minutes: ");
    int m = Integer.parseInt(reader.nextLine());
    System.out.print("hours: ");
    int h = Integer.parseInt(reader.nextLine());

    seconds.setValue(s);
    minutes.setValue(m);
    hours.setValue(h);


    int i = 0;
    while ( i < 121 ) {
        System.out.println( hours + ":" + minutes + ":" + seconds);   

        seconds.next();

        if(seconds.getValue()== 0){
        minutes.next();
        }
        // if minutes become zero, advance hours
        if(minutes.getValue()== 0 && seconds.getValue()== 0){
            hours.next();
        }
        i++;
    }


}
}

public class BoundedCounter {
    private int value;
    private int upperLimit;

 public BoundedCounter(int upperLimit){
     this.value = 0;
     this.upperLimit = upperLimit;
 }
 public void next(){
    if(value < upperLimit){
        value++;
    }
    else {
        this.value = 0;
    }
    }

 public String toString(){
     if(value < 10){
         return "0" + this.value;
     }
     else{
     return "" + this.value;
}
}
 public int getValue(){
     return this.value;
 }
 public void setValue(int newValue){
     if(newValue > 0 && newValue < this.upperLimit){
         this.value = newValue;
     }

 }
}

Two suggestions. 两个建议。

Replace the while, etc., with for (int i = 0; i < 121; i++). 将for等替换为while(int i = 0; i <121; i ++)。 Your way works, but using for is the more common approach. 您的方法可行,但使用for是更常见的方法。

You could type your input differently, with the seconds on one line, then the minutes on the next, and then the hours on a third. 您可以输入不同的输入,在一行上输入秒,然后在下一行上输入分钟,然后在第三上输入小时。 Note that you are reading the values in reverse order. 请注意,您正在以相反的顺序读取值。 That should make your existing code work. 那应该使您现有的代码正常工作。

Or, look at the API. 或者,查看API。 useDelimiter() takes a regular expression that sets a delimiter. useDelimiter()采用设置分隔符的正则表达式。 In your case, ":" should work. 在您的情况下,“:”应该有效。 Then, use nextInt(). 然后,使用nextInt()。 Of course, this will throw an exception if you mistype the input. 当然,如果您输错了输入,这将引发异常。

Good luck! 祝好运!

Your setValue method in the BoundedCounter class is just making your values slightly off. BoundedCounter类中的setValue方法只是使您的值稍微偏离。 Your setValue method should be >= and <=: 您的setValue方法应为> =和<=:

public void setValue(int newValue){
 if(newValue >= 0 && newValue <= this.upperLimit){
     this.value = newValue;
 }}

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

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