2017-09-15 50 views
-3

我想打一個用戶輸入與圓點分隔像(11.56.98) 後,要使用整數x = 11 Y = 56,Z = 98如何在java中使用單獨的兩個整數?

`Scanner s = new Scanner(System.in); 
    System.out.print("Enter Your number like (36.52.10): "); 
    int x = s.nextInt(); 
    int y = s.nextInt(); 
    int z = s.nextInt();` 

現在怎麼usedelimiter將變爲空白,以點以及如何再次返回空白

+0

取值的字符串,然後除以值按烏爾要求 – Kick

+0

你能不能請澄清你想要做什麼?你的最後一句話很難閱讀,並且(至少對我而言)是不可能理解的。 – Mischa

+0

只需使用'string.split()'https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String) – Achilles

回答

2

如果你想要在同一行使用:s.next()

如果你想在接下來的行中的文本,你需要做的:s.nextLine()

無論你採用什麼方法,它會返回一個java.lang.String

您可以使用String[] numbers = yourString.split(".")

你會得到什麼,如果你分割字符串你可以得到所有的數字數組:

int x = Integer.valueOf(numbers[0]); 
int y = Integer.valueOf(numbers[1]); 
int z = Integer.valueOf(numbers[2]); 
//Dont forget it can throw a NumberFormatException if the current String is not a valid number. 
相關問題