メソッドを使用して、.contains()
おそらく次の行に沿って「windows」文字列を確認することもできます
if (System.getProperty("os.name").toLowerCase().contains("windows") && System.getProperty("os.name").toLowerCase().contains(windows version here [xp, 7, 8, etc]))){}
Windows のバージョンが必要な場合は、すべてのバージョンを確認してから、8.1 または 10 を想定してバグを回避できます。
if (System.getProperty("os.name").toLowerCase().contains("windows") && System.getProperty("os.name").toLowerCase().contains("xp")){
//code for windows xp }
else if (System.getProperty("os.name").toLowerCase().contains("windows") && System.getProperty("os.name").toLowerCase().contains("vista")){
//code for windows vista
else if (System.getProperty("os.name").toLowerCase().contains("windows") && System.getProperty("os.name").toLowerCase().contains("7")){
//code for windows 7}
else if (System.getProperty("os.name").toLowerCase().contains("windows") && System.getProperty("os.name").toLowerCase().contains("8")){
//code for windows 8}
else if (System.getProperty("os.name").toLowerCase().contains("windows") && System.getProperty("os.name").toLowerCase().contains("8.1")){
//code for both windows 8.1 and 10
}
ここで何が起こっているかを説明します。
if ステートメントは、Windows のバージョンを判断するための単なる条件です。
OSのSystem.getProperty("os.name")
名前を文字列として返します。
この.toLowerCase()
メソッドは、返された文字列を小文字にします
この.contains(String)
メソッドは、指定された入力文字列が呼び出されている文字列に含まれているかどうかを確認します
最後のステートメントでは、1 つのブロックとして処理する必要がある 8.1 と 10 を除いて、各 OS に異なるコードを使用できます :(