gpt4 book ai didi

Java GUI 错误 - 类型不匹配 : cannot convert from String to double

转载 作者:行者123 更新时间:2023-12-02 00:45:17 25 4
gpt4 key购买 nike

所附代码的

第 100 行cost = JOptionPane.showInputDialog("Cost"); 所附代码报告错误“类型不匹配:无法从字符串转换为 double ” - 请问有什么建议导致此问题吗?

基本上,它是一个访问另一个类“BookShelf”的类,并且此行是访问 BookShelf 类以返回 costofBookshelf 的操作命令的一部分

import java.awt.FlowLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class BookGUI extends JFrame implements ActionListener

{

//String addBook="";
// public ArrayList<Book> books;

Book books = new Book ("", "", 0, "", 0);
Book book = new Book("", "", 0, "", 0);
String title = "";
String author = "";
int year = 0;
String publisher = "";
double cost = 0;

public BookShelf bookShelf = new BookShelf();
public static final int WIDTH = 300;
public static final int HEIGHT = 200;

//Creates & displays a window of the class FlowLayoutDemo
public static void main(String[] args)
{
BookGUI gui = new BookGUI( );
gui.setVisible(true);
}

// public String getTitle()
// {
// return title;
//}

public void setTitle(String title) //this is relevant
{
this.title = title;
}

public void setAuthor(String author) //this is relevant
{
this.author = author;
}

public void setYear(int year) //this is relevant
{
this.year = year;
}
public void setPublisher(String publisher) //this is relevant
{
this.publisher = publisher;
}

public void setCost(double cost) //this is relevant
{
this.cost = cost;
}

public BookGUI( )
{

setSize(WIDTH, HEIGHT);
addWindowListener(new WindowDestroyer( ));
setTitle("GUI Assignment");
Container content = getContentPane( );

content.setLayout(new FlowLayout());

JButton button1 = new JButton("Title");
content.add(button1);
button1.addActionListener(this);
//contentPane.add(button1);

JButton button2 = new JButton("Cost of Bookshelf");
content.add(button2);
button2.addActionListener(this);

JButton button3 = new JButton("Size of BookShelf");
content.add(button3);
button3.addActionListener(this);

JButton button4 = new JButton("Add Book");
content.add(button4);
button4.addActionListener(this);

}

public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("Add Book"))
//book = JOptionPane.showInputDialog("Add Book");
{ //set up the book object with all the data passed in
title = JOptionPane.showInputDialog("Title");
author = JOptionPane.showInputDialog("Author");
publisher = JOptionPane.showInputDialog("Publisher");
***cost = JOptionPane.showInputDialog("Cost");***
book.setTitle(title);
book.setAuthor(author);
book.setPublisher(publisher);
bookShelf.addBook(book);

String message = "The title of the book is :" + title +
"the Author of the Book is : " + author + " and it's published by " + publisher;
JOptionPane.showMessageDialog(null, message, "Book Details", JOptionPane.PLAIN_MESSAGE);
}
else if (e.getActionCommand().equals("Size of BookShelf")) {
int sizeOfBookShelf = bookShelf.sizeOfBookshelf();
String message = "The book shelf has " + sizeOfBookShelf + " book(s)";
JOptionPane.showMessageDialog(this, message);
}
else if (e.getActionCommand().equals("Cost of BookShelf"))
{
double costOfBookshelf = bookShelf.costOfBookshelf();
String message = "The book shelf value is " + costOfBookshelf + "Euro";
JOptionPane.showMessageDialog(this, message);
}

}
}

最佳答案

JOptionPane.showInputDialog 返回一个字符串。您不能将 String 值分配给 double 类型的变量。

您可以使用 Double.parseDouble 将字符串转换为 double 型。

关于Java GUI 错误 - 类型不匹配 : cannot convert from String to double,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5157379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com