如何從使用者輸入將字串轉換為整數型別並鍵入Double?
/** * MadLib.java * * @author: Jackie Hirsch * Assignment: Madlib * * Brief Program Description: This program has will read a madlib with the inputs that the user gives the computer. * * */ import javax.swing.JOptionPane; //import of JOptionPane public class MadLib { public static void main (String[] args) { String cheeseType; //Cheese Character String interjection; //interjection String treeType; //tree type String wholeNumberFriends; // number for number of friends on //line 27 String numberMiles; //number of miles int wholeNumberFriendsConverted; // number for number of //friends on line 27 converted double numberMilesConverted; //number of miles //ask user for variable string cheese type cheeseType = JOptionPane.showInputDialog ("Enter a type of cheese"); //ask user for varaible string interjection interjection = JOptionPane.showInputDialog ("Enter an interjection"); //ask user for variable string tree type treeType = JOptionPane.showInputDialog ("Enter a type of tree"); //ask user for variable integer number for number of friends wholeNumberFriends = JOptionPane.showInputDialog ("Enter a whole number"); //ask user for variable double number for number of miles numberMiles = JOptionPane.showInputDialog ("Enter a decimal or whole number"); //string converted wholeNumberFriends = Integer.parseInt (wholeNumberFriendsConverted); numberMiles = Integer.parseInt (numberMilesConverted); //Madlib reading printed JOptionPane.showMessageDialog (null, "There once was a " + cheeseType + "and this " + cheeseType + "was super exciting. " + "Because " + cheeseType + "was so exciting he would shout, " + interjection + ". " + "His " + wholeNumberFriendsConverted + "friends, the " + treeType + ", would sing home and the whole" + "neighborhood hated them. One neighboor walked outside and said, " + "''You annoying hooligans are crazy!!!''. They were so confused that" + "they ran away to Neverland which was " + numberMilesConverted + "miles so they never" + "had to grow up. Then they ran into captain hook and then Peter Pan saved them."); System.exit (0); //ends the program } }
*你好。這個星期,我剛在高中電腦科學課上開始學習如何編寫Java程式碼。我正在嘗試將一個字串轉換為一個雙整數和一個整數。我試圖轉換的兩個變數是全文數(整數)和數字邁爾斯(雙)。我已經為每個變數建立了一個新變數,這樣它們就可以很容易地轉換為一個雙整數和一個整數。這個轉換的錯誤是不相容的型別:Double不能轉換為java.lang.String。謝謝*
相關推薦
如何從使用者輸入將字串轉換為整數型別並鍵入Double?
/** * MadLib.java * * @author: Jackie Hirsch * Assignment: Madlib * * Brief Program Description: This program has will read a mad
《劍指Offer》面試題:將字串轉換為整數
題目 題目:把字串轉化為整數 ,若輸入無效,則返回0且將標誌位設為true 自己以前在一些書上面看到過關於 字串轉化為整數的例子,心中有點印象,知道要考慮一些特殊情況。今天決定寫下這段程式碼,發現程式碼寫的稀爛,重複度太大,需要改善。 剛實現的程
實現atoi將字串轉換為整數。
leetcode第八題:仔細考慮所有可能的輸入情況。如果您想要一個挑戰,請不要在下面看到並問您自己什麼是可能的輸入情況。在找到第一個非空白字元之前,函式首先會丟棄許多空白字元。然後,從這個字元開始,取一個可選的初始加號或減號,然後儘可能多的數字數字,並將其解釋為一個數值。字串
[C/C++筆面試]不使用庫函式將字串轉換為整數
與數字轉換為字串類似,C/C++提供了幾個標準的庫函式,可以將字串轉換為任意型別(整型,長整型,浮點型)的數字。下面列舉其函式 atof();將字串轉換為雙精度浮點型值 atoi();將字
將字串轉換為整數
這是一道經典的面試題,在收到這樣一道題時我們不要急於動手,先思考一下這道題的解決思路: 1.在函式呼叫時我們需要用指標來接收陣列的地址,那麼指標是否需要判空? 2.如果獲取的字串起始位出現“+”"-"號該如何處理? 3.如果獲取到的並非數字字串該如何處理? 4.如果獲
實現atoi這個函式,將一個字串轉換為整數。如果沒有合法的整數,返回0。如果整數超出了32位整數的範圍,返回INT_MAX(2147483647)如果是正整數
public class Solution { /** * @param str: A string * @return: An integer */ public int atoi(String str) { // write your code here
8. String to Integer (atoi)(將輸入的字串轉化為整數)
官網 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge,
資料型別之間的轉換:如何將字串轉換為基本資料型別? 如何將基本資料型別轉換為字串?
1. 呼叫基本資料型別對應的包裝類中的方法parseXXX(String)或valueOf(String)即可返回相應基本型別; 2. 一種方法是將基本資料型別與空字串("")連線(+)即可獲得其所對應的字串;另一種方法是呼叫String 類中的valueOf()方法返回相
【sql】將 float 轉換為資料型別 numeric 時出現算術溢位錯誤
sql中float型別的取值範圍? 大於9999999999的情況下,就會報錯將 float 轉換為資料型別 numeric 時出現算術溢位錯誤 insert into[dbo].[T_DATA_DISEASE](STATIONCODE,MONIDATE,RATE,STYL
MyBatis操作PostgreSQL中時間字串轉換為timestamp型別
在插入String型別的屬性到表字段型別為timestamp的格式轉換:TO_TIMESTAMP(#{引數}, 'yyyy-mm-dd hh24:mi:ss')或者TO_TIMESTAMP(#{引數}, 'yyyy-mm-dd') 例:if (record.實體類屬性
C# 將字串轉換為MD5值
StrConversionMD5: 方法能夠將傳入的字串轉換為MD5值 namespace MD5加密 { class Program { static void Main(
Java將字串轉換為時間,支援多種格式
適用格式 //2017/11/25 11:11:11//2017-11-25 11:11:11//2017.11.25 11:11:11//2017/11/25//2017-11-25//2017.11.25 public String judg
使用ParseExact方法將字串轉換為日期格式
實現效果: 知識運用: DateTime結構的ParseExact方法 public static DateTime ParseExact(string s,string format,IFormatProvider provider); 實現程式碼:
stof()、atoi()、atol()、strtod()、strtol()、strtoul() 共6個可以將字串轉換為數字的函式
標頭檔案:#include <stdlib.h> atoi() 函式用來將字串轉換成整數(int),其原型為: int atoi (const char * str); 【函式說明】atoi() 函式會掃描引數 str 字串,跳過前面的空白字元(例如空格,tab縮排等,可以通過 
將資料型別轉換成字串,將字串轉換成資料型別
方法1:採用靜態方法 int a = 123456789; String str = String.valueOf(a); 方法2:用包裝類轉換 float a=2.33f; String str=Float.toString(a); doubl
python中,如何將字串轉換為數字(將數字轉換為整型),字串的10轉換為整型的10,10.5轉換為10
說明: 在實際的應用過程中,有的時候可能會遇到字串的10,需要將字串的10轉換為數字的10 在此記錄下,通過int函式轉換的過程。 操作過程: 1.將字串轉換為整型的10 >>> str1 = "10" #將一個字串的10賦給變數str1 >&g
用json.loads()將字串轉換為json格式出錯
今天爬取今日頭條的街拍時,需要將裡面的一個字串變為json格式,結果直接轉換就出現了 json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
將字串轉化為整數
Java內部實現 /* @param s the {@code String} containing the integer * representation to be parsed * @p
演算法2:IP字串轉換為整數
關鍵技術點 將IP地址轉化成整數的方法如下: 通過String的indexOf方法找出IP字串中的點"."的位置。 根據點的位置,使用String的substring方法把IP字串分成4段。 使用Long的parseLong方法把子段轉化成一個3位整數。
劍指offer-將字串轉化為整數
49.將字串轉化為整數 題目描述 將一個字串轉換成一個整數(實現Integer.valueOf(string)的功能,但是string不符合數字要求時返回0),要求不能使用字串轉換整數的庫函式。 數值為0或者字串不是一個合法的數值則返回0。 輸入描述:輸入一個字串,包括數字字母符號,可以