如何将字符串输入转换为java中的双重?

发布于 2025-02-03 02:49:43 字数 2084 浏览 4 评论 0原文

我的代码不起作用,“ checkvalidnumber(Momentary_Value)”中存在错误,说字符串无法转换为双倍。

/**
 * Add a pokémonCard to collection
 */
public void addPokémonCard()
{
    final int INCREMENT = 1;
    
    boolean repeat = true;  // a boolean variable return true
    
    // Ask user for details
    String name = UI.askString("Name of PokémonCard: ");
    if (name.equals("")) {
        UI.println("I don't recognise that command");
        addPokémonCard();
    } else if (name == null ) {
        UI.println("Please type a PokémonCard's name: ");
        addPokémonCard();
    } else {
        // Check boundaries for the number of the momentary value of PokémonCard added
        do {
            String momentary_value = UI.askString("Momentary value: ");
            // Check a suitable value of a PokémonCard 
            if (checkValidNumber(momentary_value)){
                double momentary_value1 = Double.parseDouble(momentary_value);
                // Increment the PokémonCard ID count and add to hashmap
                pokémonCards.setPokémonCardId(); // Increment the id by 1
                //add a PokémonCard image to display in the GUI
                String imgFileName = UIFileChooser.open("Choose Image File: ");
                pokémonCards.addPokémonCard(name, momentary_value1, imgFileName);
                UI.println("Added");
                repeat = false;
            } else if (checkValidNumber(momentary_value) == false) { // Check for invalid value
                UI.println("Please input a valid price!");
            } else { // Check for invalid value
                UI.println("Must be a number!");
            }
        }while (repeat); //repeat the method again if there's null input
    }
}

/**
 * Check a valid number for momentary value
 * @return boolean false if it's an invalid number
 * @param momentary_value for the price of PokemonCard
 */
public boolean checkValidNumber(double momentary_value){
    boolean validNumber = false;
    if (momentary_value > 0) { 
        validNumber = true;
    } 
    return validNumber;
}

My code is not working and there's errors in "checkValidNumber(momentary_value)" saying that String can not convert to double.

/**
 * Add a pokémonCard to collection
 */
public void addPokémonCard()
{
    final int INCREMENT = 1;
    
    boolean repeat = true;  // a boolean variable return true
    
    // Ask user for details
    String name = UI.askString("Name of PokémonCard: ");
    if (name.equals("")) {
        UI.println("I don't recognise that command");
        addPokémonCard();
    } else if (name == null ) {
        UI.println("Please type a PokémonCard's name: ");
        addPokémonCard();
    } else {
        // Check boundaries for the number of the momentary value of PokémonCard added
        do {
            String momentary_value = UI.askString("Momentary value: ");
            // Check a suitable value of a PokémonCard 
            if (checkValidNumber(momentary_value)){
                double momentary_value1 = Double.parseDouble(momentary_value);
                // Increment the PokémonCard ID count and add to hashmap
                pokémonCards.setPokémonCardId(); // Increment the id by 1
                //add a PokémonCard image to display in the GUI
                String imgFileName = UIFileChooser.open("Choose Image File: ");
                pokémonCards.addPokémonCard(name, momentary_value1, imgFileName);
                UI.println("Added");
                repeat = false;
            } else if (checkValidNumber(momentary_value) == false) { // Check for invalid value
                UI.println("Please input a valid price!");
            } else { // Check for invalid value
                UI.println("Must be a number!");
            }
        }while (repeat); //repeat the method again if there's null input
    }
}

/**
 * Check a valid number for momentary value
 * @return boolean false if it's an invalid number
 * @param momentary_value for the price of PokemonCard
 */
public boolean checkValidNumber(double momentary_value){
    boolean validNumber = false;
    if (momentary_value > 0) { 
        validNumber = true;
    } 
    return validNumber;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋风の叶未落 2025-02-10 02:49:43

您需要先转换为双人

checkValidNumber(Double.parseDouble(momentary_value))

You need to convert to a double first

checkValidNumber(Double.parseDouble(momentary_value))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文