mooc java练习78,有界计数器不接受用户输入
和我儿子一起学java。我用谷歌搜索了所有的单词组合,但似乎找不到答案。我将感谢任何帮助或指导 程序在分钟/小时内不接收用户输入以启动计数器。所以计数器从00:00:50开始输入23:59:50。以下是我迄今为止的代码: 主要类别:mooc java练习78,有界计数器不接受用户输入,java,Java,和我儿子一起学java。我用谷歌搜索了所有的单词组合,但似乎找不到答案。我将感谢任何帮助或指导 程序在分钟/小时内不接收用户输入以启动计数器。所以计数器从00:00:50开始输入23:59:50。以下是我迄今为止的代码: 主要类别: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner reader = new Scanner(System.in);
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;
}
}
}
import java.util.Scanner;
公共班机{
公共静态void main(字符串[]args){
扫描仪阅读器=新扫描仪(System.in);
BoundedCounter秒=新的BoundedCounter(59);
BoundedCounter分钟数=新的BoundedCounter(59);
BoundedCounter小时数=新的BoundedCounter(23);
系统输出打印(“秒:”;
int s=Integer.parseInt(reader.nextLine());
系统输出打印(“分钟:”);
int m=Integer.parseInt(reader.nextLine());
系统输出打印(“小时:”;
inth=Integer.parseInt(reader.nextLine());
秒。设置值;
分钟。设定值(m);
小时。设定值(h);
int i=0;
而(i<121){
系统输出打印项次(小时+“:“+分钟+”:“+秒);
秒。下一步();
如果(秒数。getValue()==0){
分钟。下一步();
}
//如果分钟变为零,则提前几个小时
如果(分钟.getValue()==0和秒.getValue()==0){
小时。下一个();
}
i++;
}
}
}
公共类边界计数器{
私有int值;
私有整数上限;
公共边界计数器(整数上限){
该值=0;
这个。上限=上限;
}
下一个公共空间(){
如果(值<上限){
值++;
}
否则{
该值=0;
}
}
公共字符串toString(){
如果(值<10){
返回“0”+此值;
}
否则{
返回“+”this.value;
}
}
public int getValue(){
返回此.value;
}
公共void设置值(int newValue){
if(newValue>0&&newValue
两条建议
将while等替换为for(int i=0;i<121;i++)。您的方法可行,但使用for是更常见的方法
你可以键入不同的输入,一行是秒,下一行是分钟,第三行是小时。请注意,您正在以相反的顺序读取值。这将使您现有的代码正常工作
或者,看看API。useDelimiter()接受设置分隔符的正则表达式。在您的情况下,“:”应该起作用。然后,使用nextInt()。当然,如果输入错误,这将引发异常
祝你好运 您在BoundedCounter类中的setValue方法只是使您的值稍微偏离。您的setValue方法应该是>=和=0&&newValue我实际上刚刚发现了一个问题:我的setValue if语句忽略了输入,因为它是上限。通过将其更改为newValue
public void setValue(int newValue){
if(newValue >= 0 && newValue <= this.upperLimit){
this.value = newValue;
}}