私の息子と一緒にJavaを学習しようとしています。私はすべての単語の組み合わせをGoogle検索しましたが、答えを見つけることができないようです。私はどんな助けや方向性にも感謝します。mooc java exercise 78、制限付きカウンターでユーザー入力を受け付けない
プログラムは、カウンタを開始するための分/時間のユーザー入力を受け付けていません。カウンタは23:59:50の入力に対して00:00:50に開始します。ここでは今までの私のコードは次のとおりです。
メインクラス:
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;
}
}
}
ありがとうスティーブ! – Tami
私は実際に問題を見つけました:私のsetValue if文はupperLimitなので、入力を無視していました。 newValue <= this.upperLimitに変更することで、入力を認識しました。 – Tami