如何從用戶輸入將字符串轉換爲整數類型並鍵入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。謝謝*

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章