亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將字符串轉換為整數類型并從用戶輸入中輸入 double 類型?

如何將字符串轉換為整數類型并從用戶輸入中輸入 double 類型?

呼啦一陣風 2021-09-12 10:48:54
/** * 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 JOptionPanepublic 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);    }}**你好。這周我剛剛在高中計算機科學課上開始學習如何編寫 Java 代碼。我正在嘗試將字符串轉換為雙精度和整數。我要轉換的兩個變量是 wholeNumberFriends(整數)和 numberMiles(雙精度)。我為它們中的每一個都創建了一個新變量,以便它們可以輕松轉換為雙精度和整數。我不斷收到此轉換的錯誤是,類型不兼容: double 無法轉換為 java.lang.String 。謝謝你。**
查看完整描述

2 回答

?
米琪卡哇伊

TA貢獻1998條經驗 獲得超6個贊

字符串轉整數

int num = Integer.parseInt("1");

字符串加倍

double num = Double.parseDouble("1.2");


查看完整回答
反對 回復 2021-09-12
?
森林海

TA貢獻2011條經驗 獲得超2個贊

您可以使用Integer.valueOf(java.lang.String)


       String userInput = "2";


        try {

            Integer.valueOf(userInput);

        } catch (NumberFormatException e) {

            // Not a Number

        }

同樣的valueOf方法是可用的Double,Float,Long等。


該valueOf方法將返回一個對象而不是原始對象。


查看完整回答
反對 回復 2021-09-12
  • 2 回答
  • 0 關注
  • 246 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號