2014-11-23 55 views
1

我怎樣才能改變一個字符串輸入到整型 的示例 -如何改變字符串到整數

read_line_to_codes(user_input,L), 
atom_codes(C,L). 

在這種C被存儲string.Suppose用戶輸入18。所以我想用這個18作爲一個整數,以便我可以使用像>=C這樣的操作。這在Prolog中可能嗎?

+0

的可能的複製[序言:如何將字符串轉換爲整數(https://stackoverflow.com/questions/6782007/prolog-how-to-convert-string-to-integer) – 2018-02-22 03:12:20

回答

0

Prolog數據類型不包含「字符串」。 SWI-Prolog的最近添加,但你可以留在ISO安全通道與number_codes

0

可以強制分配一個int數據類型的用戶輸入,你讓JVM知道,你知道你在做什麼,這會讓如果int數據類型小於用戶輸入字符串大小,程序將進行編譯。這裏是一個示例代碼來幫助你。

public class stringToInt{ 

    public static void main(String []args){ 

    string C = 18; //User input, make sure to use the scanner class to get the user input value 
    int int_C = (int) C; 

    /**Your new value is now in int datatype and you can go ahead and use int_C for your arithmetic 
      opreation **/ 
    } 

} 
+0

你的答案超出範圍 - 這是關於[tag:prolog] – false 2014-11-23 10:05:42

+0

我在問prolog不是java – Coder 2014-11-23 10:23:29

+0

對於誤會感到抱歉。那麼請不要理會 – 2014-11-23 14:45:40