如何从用户输入将字符串转换为整数类型并键入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。谢谢*

猜你喜欢

转载自blog.csdn.net/qq_38354073/article/details/82586024
今日推荐