我的代码没有编译,我最大的问题是第32行编译但42不编译,它们来自的方法完全相同。错误消息是错误找不到符号。
import java.util.*;
import java.text.*;
public class CourseApp{
public static void main(String[] args){
Scanner s = new Scanner (System.in);
DecimalFormat df = new DecimalFormat("$0.00");
int initialSize;
boolean q = true;
System.out.println("Enter how many courses you would like to enter info rmation for? ");
try{initialSize = s.nextInt();}
catch(NumberFormatException sonic){
while(initialSize <= 0){
System.out.println("Please enter an integer greater than 0 (ie5). ");
initialSize = s.nextInt();}}
ArrayList <Course> courseArrayList = new ArrayList<Course> (initialSize);
String number="";
String name="";
String instr="";
String text="";
for (int i = 0; i < initialSize; i++){
courseArrayList.add(new Course(number, name, instr, text));
System.out.println("Enter in course information. ");
do{
courseArrayList.get(i).setNumber("");
System.out.println("Please enter the course number: ");
courseArrayList.get(i).setNumber(s.nextLine());
}while(courseArrayList.get(i).getNumber().equals(""));
do{
courseArrayList.get(i).setName("");
System.out.println("Please enter the course name: ");
courseArrayList.get(i).setName(s.nextLine());
}while(courseArrayList.get(i).getName().equals(""));
do{
courseArrayList.get(i).setLastName("");
System.out.println("Please enter the Instructor's last name: ");
courseArrayList.get(i).setLastName(s.nextLine());
}while(courseArrayList.get(i).getLastName().equals(""));
do{
courseArrayList.get(i).setFirstName("");
System.out.println("Please enter the Instructor's first name: ");
courseArrayList.get(i).setFirstName(s.nextLine());
}while(courseArrayList.get(i).getFirstName().equals(""));
do{
courseArrayList.get(i).setUserName("");
System.out.println("Please enter the Instructor's user name: ");
courseArrayList.get(i).setUserName(s.nextLine());
}while(courseArrayList.get(i).getUserName().equals(""));
String[] instrr = new String[3];
instrr[0]=courseArrayList.get(i).getLastName()+", ";
instrr[1]=courseArrayList.get(i).getFirstName()+"\n";
instrr[2]=courseArrayList.get(i).getUserName()+"@K-State.ksu";
instr = instrr[0]+instrr[1]+instrr[2];
System.out.println("Please enter the required text book's title: ");
courseArrayList.get(i).setTitle(s.nextLine());
System.out.println("Please enter the required text book's author: ");
courseArrayList.get(i).setAuthor(s.nextLine());
System.out.println("Please enter the required text book's price: ");
try{courseArrayList.get(i).setPrice(s.nextDouble());}
catch(NumberFormatException shadow){
while(courseArrayList.get(i).setPrice(s.nextDouble()) < 0){
System.out.println("Please enter a positive numerical value. ");
courseArrayList.get(i).setPrice(s.nextDouble());}}
String[] textt = new String[3];
textt[0]=courseArrayList.get(i).getTitle()+"\n";
textt[1]=courseArrayList.get(i).getAuthor()+"\n";
textt[2]=df.format(courseArrayList.get(i).getPrice())+"\n";
text = textt[0]+textt[1]+textt[2];}
for (int i = 0; i < initialSize; i++){
System.out.println("Press enter to display each of the courses");
s.nextLine();
courseArrayList.get(i).toString();}
System.out.println("Please enter a course number");
String a = s.nextLine();
for (int i = 0; i < initialSize; i++){
if (a.equals(courseArrayList.get(i).getNumber())){
courseArrayList.remove(i);}
else if (! a.equals(courseArrayList.get(initialSize-1).getNumber())){
do {
System.out.println("Course number not found.");
System.out.println("Please enter a valid course number: ");
a = s.nextLine();
for (int j = 0; j < initialSize; j++){
if (a.equals(courseArrayList.get(j).getNumber())){
q=false;
courseArrayList.remove(j);}}
} while (q=true);}
else {}}
for (int i = 0; i < initialSize; i++){
System.out.println("Press enter to display each of the courses");
s.nextLine();
courseArrayList.get(i).toString();}}}
这是来自不同的类,这部分不编译
public void setLastName(String lname){
lastName=lname;}
讲师班是:
public class Instructor
{
private String lastName; // Last name
private String firstName; // First name
private String userName; // Username ID
public Instructor(String lname, String fname, String un){
lastName = lname;
firstName = fname;
userName = un;}
public Instructor(Instructor object2){
lastName = object2.lastName;
firstName = object2.firstName;
userName = object2.userName;}
public void setLastName(String lname){
lastName=lname;}
public String getLastName(){
return lastName;}
public void setFirstName(String fname){
firstName=fname;}
public String getFirstName(){
return firstName;}
public void setUserName(String un){
userName=un;}
public String getUserName(){
return userName;}
public String toString()
{
return str;
}
}
这是来自不同的类,这部分确实编译
public void setName(String name){
courseName=name;}
课程类是:
public class Course
{
private String courseNumber; // e.g. CIS 200
private String courseName; // e.g. Programming Fundamentals
private Instructor instructor; // Course instructor (object)
private TextBook textBook; // Required Course textbook (object)
public Course(String number, String name, String instr, String text){
courseNumber = number;
courseName = name;
instructor = new Instructor(instr);
textBook = new TextBook(text);}
public String getName(){
return courseName;}
public void setName(String name){
courseName=name;}
public String getNumber(){
return courseNumber;}
public void setNumber(String number){
courseNumber=number;}
public Instructor getInstructor(){return new Instructor(instructor);}
/**getTextBook method
@return A reference to a copy of this course's TextBook object.*/
public TextBook getTextBook(){return new TextBook(textBook);}
/**toString method
@return A string containing the course information.*/
public String toString(){
String str = courseNumber + " " + courseName+ "\n"+
instr+"\n" +
text;
return str;}}
最后是TextBook类:
import java.text.*;
import java.util.*;
public class TextBook
{
DecimalFormat df = new DecimalFormat("$0.00");
private String title; // Title of the book
private String author; // Author's last name
private double price; // Wholesale cost of the book
public TextBook(String t, String a, double p){
title = t;
author = a;
price = p;}
public TextBook(TextBook object2){
title = object2.title;
author = object2.author;
price = object2.price;}
public void setTitle(String t){
title=t;}
public void setAuthor(String a){
author=a;}
public void setPrice(String p){
price=p;}
public void getTitle(){
return title;}
public void getAuthor(){
return author;}
public void getPrice(){
return price;}
public String toString(){
String str = "Required Textbook: \n" + " " + title+", " + author + ", " + df.format(price);
return str;}}
为什么.setLastName()不起作用,但.setName()不起作用
答案 0 :(得分:0)
在你的班级中有这个
public void setLastName(String lname){
lastName=lname;
}
确保lastName
作为类变量存在。我猜这应该是这样的:
private String lastName;
如果你发布了Course.java代码,我们都会更好地解释可能缺少的内容。