我知道这是一个愚蠢的问题,但我想做的是输入 1 行并将其分成 3 个变量。我想输入的是时间、小时、分钟和秒“10:30:40”,我希望它存储到变量 a b c 中。 a = 小时 b = 分钟 c = 秒
公共(public)课Bituka{
public static void main (String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Biek littlepig = new Biek();
String x = null;
String y = null;
String z = null;
System.out.println("Enter Time");
x = br.readLine();
y = br.readLine();
z = br.readLine();
int a = Integer.parseInt(x);
int b = Integer.parseInt(y);
int c = Integer.parseInt(z);
littlepig.setTime(a,b,c);
System.out.println(littlepig.toMilitary());
}
}
最佳答案
输入时间时,将其作为字符串,然后分割字符串:(输入为“小时:分钟:秒”)
System.out.println("Enter Time");
String timeString = br.readLine();
String[] timeArray = timeString.split(":"); //Splits the string with regex ":"
int hour = Integer.parseInt(timeArray[0]);
int minute = Integer.parseInt(timeArray[1]);
int second = Integer.parseInt(timeArray[2]);
并用小时
、分钟
和秒
设置时间。
关于java - 一个输入分为 3 个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27727707/