简而言之,实现一个循环,使用户可以选择数组的字符串元素的索引值,然后将选择返回给用户并验证用户输入。
我注意到该循环由于一个错误而关闭的错误,因此包含了“ <=”“ |”。我已更改为一个数组初始化器,它不那么容易出错。循环仅打印出索引值,而不打印出字符串。我已经将数组更改为数组类,并得到了错误。 “课程构造函数无法应用于声明的类型,实际参数和表单参数的长度不同”,我研究了Javadoc声明。并看到在构造函数中这样声明。 = new ArrayList(Length)或类似的东西,但我不想覆盖任何内容。我试图在NetBeans中获得支持以解决该问题,但是在实现代码时,却得到了NetBeans支持为数组的第一个元素完全实现的代码
我想有效运行的循环代码
Lesson[] lessonList = new Lesson [4];
lessonList[0] = new Lesson ("Zumba");
lessonList[1] = new Lesson("Spin");
lessonList[3] = new Lesson("Yoga");
lessonList[4] = new Lesson("BodySculpt");
for(int count = 0; count <= lessonList.length; | count++){
System.out.println((count + 1) + ". " + lessonList[count]);
}
System.out.println("Please select ");
int selection = sc.nextInt();
selection -= 1;
if(selection >lessonList.length | selection < 0) {
System.out.println(" You have entered invalued value.");
lessonsList();
System.out.println("Can you please re-enter selection .");
}
return selection;
在Lesson类中,错误的发生与构造函数有关,错误“ Lesson构造函数无法应用于声明的类型,实际参数和表单参数的长度不同”
public class Lesson {
private String lessonTitle;
private String lessonDate;
private int lessonPrice;
private Rating rating;
public void Lesson (String title) {
this.setTitle(title);
rating = new Rating();
}
public void lesson (String zumba,
String spin,
String yoga,
String bodysculpt )
{
/// tried declaring recieve errors
}
public Lesson() {
this.lessonTitle = "Udentified";
this.rating = new Rating();
}
预期的结果将消除循环中的缺陷以及构造函数的基本解决方案提示。所以我可以处理下一个元素
答案 0 :(得分:0)
import java.util.*;
public class Tester {
public static void main(String [] args) {
Scanner sc = new Scanner(System.in);
Lesson [] lessonList = new Lesson [4];
lessonList[0] = new Lesson ("Zumba", "Monday & Wednesday", 100, 3.4);
lessonList[1] = new Lesson("Spin", "Tuesday & Thursday", 80, 3.9);
lessonList[2] = new Lesson("Yoga", "Sunday", 83, 4.0);
lessonList[3] = new Lesson("BodySculpt", "Friday and Saturday", 90, 3.85);
for(int count = 0; count < lessonList.length; count++){
System.out.println((count + 1) + ". " + lessonList[count]);
}
System.out.println("Please select between 1 - 4 for the lessons ");
int selection = sc.nextInt();
selection -= 1;
while(selection >lessonList.length | selection < 0) {
System.out.println("Can you please re-enter selection .");
selection = sc.nextInt();
}
System.out.println((selection + 1) + ". " + lessonList[selection]);
}
}
public class Lesson {
private String lessonTitle;
private String lessonDate;
private double lessonPrice;
private double rating;
public Lesson() { //default constructor
this.lessonTitle = null;
this.lessonDate = null;
this.lessonPrice = 0;
this.rating = 0;
}
public Lesson (String title, String date, double price, double rate) { //second constructor
this.lessonTitle = title;
this.lessonDate = date;
this.lessonPrice = price;
this.rating = rate;
}
**//When you want to print a class, you must need a toString method defined right**
public String toString () {
return "Lesson Title: " + this.lessonTitle + ", Lesson Date: " + this.lessonDate + ", $" + this.lessonPrice + ", Rate: " + this.rating;
}
}