【发布时间】:2016-01-15 21:29:10
【问题描述】:
我是一名初级程序员,我为我的学校班级写了这篇文章。但不知何故,即使我在 displaypay() 下的 JOptionPane.showMessageDialog 也不起作用!!!我希望会弹出一个消息框,但是在我输入每天的小时数之后,什么也没有发生!它甚至没有跳过部分,整个程序只是暂停!我感到很困惑!相反,如果我使用 System.out.println(),它可以正常工作。
另外我不想为 displaypay() 做 System.out.println,我必须使用 showMessageDialog。
package payroll.program;
import java.util.Scanner;
import javax.swing.JOptionPane; //imports
class Employee
{
int hourlypay;
int totalhours = 0;
String name; //variables
void getname()
{
Scanner scan = new Scanner(System.in);
System.out.println("What is this employee's name?");
name = scan.next(); //gets name
}
void calculatepay()
{
int[] hours = new int[5]; //creates array for hours
Scanner scan = new Scanner(System.in);
System.out.println("How much is " + name + " paid per hour?");
hourlypay = scan.nextInt(); //gets hourly pay
for (int i = 0; i < 5; i++)
{
System.out.println("How many hours did " + name + " work on day " + (i + 1) + "?");
hours[i] = scan.nextInt(); //gets hour on each day
totalhours = totalhours + hours[i]; //adds hours up
}
}
void displaypay()
{
JOptionPane.showMessageDialog(null, "You have to pay " + " $" + totalhours * hourlypay + " to " + name + "!"); //displays total pay
}
}
public class PayrollProgram {
public static void main(String[] args) {
int numberofemployees; //variable for # of employees
System.out.println("Welcome to the Payroll Program!"); //welcomes user
Scanner scan = new Scanner(System.in);
System.out.println("How many employees do you have?");
numberofemployees = scan.nextInt(); //gets input for # of employees
Employee[] ArrayofEmployees = new Employee[numberofemployees]; //creates array of employees with the same size as the number of employees
for (int i = 0; i < numberofemployees; i++)
{
ArrayofEmployees[i] = new Employee(); //creates an Employee for each space in the array
}
for (int i = 0; i < numberofemployees; i++)
{
ArrayofEmployees[i].getname();
ArrayofEmployees[i].calculatepay();
ArrayofEmployees[i].displaypay(); //runs functions in class Employee for each employee
}
}
}
这是我的程序,JOptionPane.showMessageDialog 不起作用。
请帮忙!
【问题讨论】:
-
定义“不起作用”(所以其他有同样问题的人可以找到这个问题)。您期望会发生什么以及会发生什么?
-
我无法复制它。对话框为我出现。
-
使用
Scanner#next...后尝试使用JOptionPane.showMessageDialog时出现问题。为我重现问题的 SSCCE:pastebin.com/NAMcmuEh -
对我来说似乎工作得很好
-
没关系!我几乎是个白痴。它出现在背景中。非常感谢!!!
标签: java user-interface dialog joptionpane