![](/img/trans.png)
[英]Why isn't my java program working?? It doesn't allow me to input my string statement scale.
[英]My program doesn't allow me to ask for user input inside a constructor
我正在为数字时钟创建一堆方法和构造函数,我正在尝试制作一个用户输入所有内容但程序不允许我要求用户输入的方法和构造函数。
import java.util.Scanner;
import java.util.Calendar;
public class DigitalClock {
Calendar calendar = Calendar.getInstance();
int hour = 0;
int minute = 0;
boolean am = true;
public DigitalClock() {
hour = 12;
minute = 0;
am = true;
}
public void setTime() {
hour = input.nextInt();
minute = input.nextInt();
}
public void getHour() {
hour = calendar.get(Calendar.HOUR_OF_DAY);
}
public void getMinute() {
minute = calendar.get(Calendar.MINUTE);
}
public void isMorning() {
if(hour > 12){
am = false;
}else{
am = true;
}
}
}
Scanner object is not used. Main method is missing as well.
Scanner input = new Scanner(System.in); needs to be added. Add a println to ask for inputs.
example:
Scanner input= new Scanner(System.in); // Read from System.in
System.out.println("Enter the Hour: ");
hour = input.nextInt(); // Scans the input as an int.
input.close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.