我在一个Java类,我已经被提示做一个实验室从这本书。
Sammy‘s Seashore向游客出租皮划艇、独木舟、沙滩椅和雨伞等海滩设备。编写一个程序,提示用户租用一件体育器材的分钟数。计算租金为每小时$40加上每增加一分钟$1。(您可能已经推测过,此比率存在逻辑缺陷,但目前,请按此处所述计算速率。在阅读了关于决策的章节之后,你可以解决这个问题。)用您在第1章SammysMotto2类中创建的边框显示Sammy的座右铭,然后显示小时、分钟和总价。将文件保存为SammysRentalPrice.jav
现在我试着用JOptionPane来做这件事,但是我不知道从我拥有的东西到哪里去。
package sammysrentalprice;
import javax.swing.JOptionPane;
public class SammysRentalPrice
{
public static void main(String[] args)
{
final int RENT_PER_HOUR = 40;
final int RENT_PER_ADDITIONAL_MINUTE = 1;
final int MINUTES_IN_AN_HOUR = 60;
int minutesRented;
int hoursRented;
int additionalMinutes;
int total;
JOptionPane.showMessageDialog(null,
"SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS\n"
+ "SS SS \n"
+ "SS Sammy's makes it fun in the sun. SS \n"
+ "SS SS \n"
+ "SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS\n");
}
}
Example of what we should use (This was the lab before the one we are on)
package chap2.carlyseventprice;
import javax.swing.JOptionPane;
public class Chap2CarlysEventPrice
{
public static void main(String[] args)
{
final int PRICE_PER_PERSON = 35;
final int LARGE_EVENT = 50;
String strNumOfGuests;
int numOfGuests;
int totalPrice;
strNumOfGuests = JOptionPane.showInputDialog(null,
"Enter the number of guests", "Guest Entry",
JOptionPane.INFORMATION_MESSAGE);
numOfGuests = Integer.parseInt(strNumOfGuests);
totalPrice = numOfGuests * PRICE_PER_PERSON;
JOptionPane.showMessageDialog(null,
"*****************************************************\n"
+ "* Carly's makes the food that makes it a party! *\n"
+ "*****************************************************");
JOptionPane.showMessageDialog(null,
"Number Of Guests: " + numOfGuests
+ "\nPrice per Guest: " + PRICE_PER_PERSON
+"\nTotal Price: $" + totalPrice
+ "\nLarge event? " + (numOfGuests >= 50));
}发布于 2020-09-01 02:38:31
首先,您需要更改它以显示输入对话框,而不是消息对话框。这可以通过将showMessageDialog更改为showInputDialog来实现。
请记住,showInputDialog返回一个字符串,因此您必须将其解析为int。
然后你就可以做计算了。
代码应该如下所示:
public static void main(String[] args)
{
final int RENT_PER_HOUR = 40;
final int RENT_PER_ADDITIONAL_MINUTE = 1;
final int MINUTES_IN_AN_HOUR = 60;
int minutesRented;
int hoursRented;
int additionalMinutes;
int total;
String strNumberOfMinutes = JOptionPane.showInputDialog(null,
"Enter the number of minutes", "Number of Minutes",
JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null,
"SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS\n"
+ "SS SS \n"
+ "SS Sammy's makes it fun in the sun. SS \n"
+ "SS SS \n"
+ "SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS\n");
minutesRented = Integer.parseInt(strNumberOfMinutes);
hoursRented = minutesRented / MINUTES_IN_AN_HOUR;
additionalMinutes = minutesRented - (hoursRented*MINUTES_IN_AN_HOUR);
total = additionalMinutes*RENT_PER_ADDITIONAL_MINUTE + hoursRented*RENT_PER_HOUR;
JOptionPane.showMessageDialog(null,
"Hours: "+hoursRented+"\n"
+ "Min: "+additionalMinutes+"\n"
+ "Total Cost: "+total);
}发布于 2020-09-01 22:27:15
既然您使用的是swing,为什么不创建一个扩展JFrame或JPanel并添加JScrollPane或JTextArea的类?还添加一个按钮来关闭窗格,就像它在JOptionPane中应该的那样。
调用main方法中的类。请注意,在JOptionPane中显示许多信息并不是最佳实践。
https://stackoverflow.com/questions/63679564
复制相似问题