import java.util.*;
import javax.swing.*; public class ArraylistTest
{
public static void main(String[] args)
{
ArrayList staff=new ArrayList();
do
{
String name=JOptionPane.showInputDialog
("Please input your name:");
String input=JOptionPane.showInputDialog
("Please inout your salary:");
int salary=Integer.parseInt(input);
input=JOptionPane.showInputDialog
("Please input your year:");
int year=Integer.parseInt(input);
input=JOptionPane.showInputDialog
("Please input your mouth:");
int mouth=Integer.parseInt(input);
input=JOptionPane.showInputDialog
("Please input your day:");
int day=Integer.parseInt(input);
staff.add(new Employee(name,salary,year,mouth,day));
input=JOptionPane.showInputDialog
("Ready:?(Y/N)");
input=input.toUpperCase();
}
while (input.equals("N"));
System.exit(0);
for(int i=0;i<staff.size();i++)
{
Employee e=(Employee)staff.get(i);
System.out.println
("name="+e.getName()+",salary"+e.getSalary()
+",hireDay"+e.getHireDay());
}
}
}
这段程序为什么错在这里呢?我不明白,谢谢!
symbol : variable input
location: class ArraylistTest
while (input.equals("N"));
^
1 error
import javax.swing.*; public class ArraylistTest
{
public static void main(String[] args)
{
ArrayList staff=new ArrayList();
do
{
String name=JOptionPane.showInputDialog
("Please input your name:");
String input=JOptionPane.showInputDialog
("Please inout your salary:");
int salary=Integer.parseInt(input);
input=JOptionPane.showInputDialog
("Please input your year:");
int year=Integer.parseInt(input);
input=JOptionPane.showInputDialog
("Please input your mouth:");
int mouth=Integer.parseInt(input);
input=JOptionPane.showInputDialog
("Please input your day:");
int day=Integer.parseInt(input);
staff.add(new Employee(name,salary,year,mouth,day));
input=JOptionPane.showInputDialog
("Ready:?(Y/N)");
input=input.toUpperCase();
}
while (input.equals("N"));
System.exit(0);
for(int i=0;i<staff.size();i++)
{
Employee e=(Employee)staff.get(i);
System.out.println
("name="+e.getName()+",salary"+e.getSalary()
+",hireDay"+e.getHireDay());
}
}
}
这段程序为什么错在这里呢?我不明白,谢谢!
symbol : variable input
location: class ArraylistTest
while (input.equals("N"));
^
1 error
input是什么类型的,值为多少,在程序里根本就没有说明,所以就出错了~~~
String input=JOptionPane.showInputDialog
{
public static void main(String[] args)
{
ArrayList staff=new ArrayList();
String name = "";
String input = "";
do
{
name=JOptionPane.showInputDialog
("Please input your name:");
input=JOptionPane.showInputDialog
("Please inout your salary:");
int salary=Integer.parseInt(input);
input=JOptionPane.showInputDialog
("Please input your year:");
int year=Integer.parseInt(input);
input=JOptionPane.showInputDialog
("Please input your mouth:");
int mouth=Integer.parseInt(input);
input=JOptionPane.showInputDialog
("Please input your day:");
int day=Integer.parseInt(input);
staff.add(new Employee(name,salary,year,mouth,day));
input=JOptionPane.showInputDialog
("Ready:?(Y/N)");
input=input.toUpperCase();
}
while (input.equals("N"));
System.exit(0);
for(int i=0;i<staff.size();i++)
{
Employee e=(Employee)staff.get(i);
System.out.println
("name="+e.getName()+",salary"+e.getSalary()
+",hireDay"+e.getHireDay());
}
}
} 这样,把变量声明在 do-while 的外边
因为它的生存期只在do执行的时候~~~