我必須將這種格式的24小時制轉換爲分鐘,同時允許用戶輸入他們想要的任何時間。到目前爲止,我的代碼只允許他們進入XX:XX,只要兩個XX之間有一個空格,而不是冒號。我曾嘗試使用分隔符,但只是最後兩個XX將作爲分鐘的剪輯。如何以24小時格式輸入xx:xx而不顯示「:」給出錯誤?
import java.util.*;
public class TaskA {
///import the tools necessary i.e java.util.* and Scanner console. I have also used a delimiter to render the ":" as not being read.
static Scanner console = new Scanner(System.in).useDelimiter(":");
public static void main(String[] args) {
///This is how I will define the time. using both hours and minutes for the console.
int hours;
int minutes;
int total_mins;
///Here I have written how the question will come out and what the user will have to enter.
System.out.print("Specify time (HH:MM) : ");
/// Here I have the hours and minutes being inputed, as well as a draft on the final statement.
hours = console.nextInt();
minutes = console.nextInt();
///This is the mathematical equation for total minutes.
total_mins = (hours * 60) + minutes;
///final statement.
System.out.print(hours + ":" + minutes + " = " + total_mins + " Mins !");
}
}
在分隔符上分割字符串,您將擁有兩個部分 – Whymarrh
你見過我的答案嗎?看起來你的代碼應該按原樣工作... – janos