我不确定如何执行此操作。我在介绍java类,它要求我们使用消息框(而不仅仅是system.out.println)我记得我们导入了一些东西,这是一个简单的更改,但我找不到任何关于它的注释。
此外,我在网络和本网站上找到的所有示例都超出了本类(class)的范围。
如果这是一个不正确的格式,我提前道歉,这是我第一次在这里发帖。
TLDR:尝试改变
System.out.print("Enter renter name: ");
renterName = input.next();
显示在消息框中而不是 Eclipse 控制台中
我知道我们导入了一些东西(与导入 Scanner 的方式相同)来完成这项工作,但我发现的每个示例本质上都是在说创建您自己的对话框方法,这超出了我的知识范围和此类。
完整代码如下:
import java.util.Scanner;
public class RentYourVideo {
public static void main(String[] args) {
int numberOfRentals, finalBill;
VideoRental rental = new VideoRental(); //runs constructor
Scanner input = new Scanner(System.in);
String renterName;
System.out.print("Enter renter name: ");
renterName = input.next();
System.out.print("Enter number of videos to rent: ");
numberOfRentals = input.nextInt();
rental.setRentalFee(); //needs to set rental fee to $5 according to assignment
rental.calculateBill(numberOfRentals); //from prev input
finalBill = rental.getFinalBill();
System.out.println(renterName + " your total bill for " +numberOfRentals+ " videos is $" +finalBill);
input.close();
}
}
//将所有提示和输出更改为对话框/消息框!!!
public class VideoRental {
private int rentalFee, finalBill, numberOfRentals;
public VideoRental() { //constructor method
rentalFee = 0;
finalBill = 0;
}
public void setRentalFee() { //set method
rentalFee = 5;
} //the assignment claims this must set rentalFee = 5
public void calculateBill(int inRented) {
numberOfRentals = inRented;
finalBill = rentalFee * numberOfRentals;
}
public int getFinalBill() {
return finalBill;
}
}
最佳答案
看看这个:
String name = JOptionPane.showInputDialog(null, "Enter name here:");
http://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html
import javax.swing.JOptionPane;
[...]
public static void main(String[] args) {
int numberOfRentals, finalBill;
VideoRental rental = new VideoRental(); //runs constructor
String renterName;
renterName = JOptionPane.showInputDialog(null, "Enter renter name: ");
numberOfRentals = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter number of videos to rent: "));
rental.setRentalFee(); //needs to set rental fee to $5 according to assignment
rental.calculateBill(numberOfRentals); //from prev input
finalBill = rental.getFinalBill();
JOptionPane.showMessageDialog(null, renterName + " your total bill for " +numberOfRentals+ " videos is $" +finalBill);
}
关于java - 对于初学者来说最简单的对话框/消息框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34086570/