Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使循环不那么混乱,并使其按设想的方式运行_Java_Oop_Netbeans - Fatal编程技术网

Java 如何使循环不那么混乱,并使其按设想的方式运行

Java 如何使循环不那么混乱,并使其按设想的方式运行,java,oop,netbeans,Java,Oop,Netbeans,长话短说,实现一个循环,允许用户选择数组字符串元素的索引值,然后将选择返回给用户并验证用户输入 我注意到的错误是,循环有一个off by one错误,因此包含“import java.util.*; 公共类测试员{ 公共静态void main(字符串[]args){ 扫描仪sc=新的扫描仪(System.in); 第[]课lessonList=新课[4]; lessonList[0]=新课(“尊巴”,“周一和周三”,100,3.4); lessonList[1]=新课(“旋转”,“周二和周四”,

长话短说,实现一个循环,允许用户选择数组字符串元素的索引值,然后将选择返回给用户并验证用户输入

我注意到的错误是,循环有一个off by one错误,因此包含“
import java.util.*;
公共类测试员{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
第[]课lessonList=新课[4];
lessonList[0]=新课(“尊巴”,“周一和周三”,100,3.4);
lessonList[1]=新课(“旋转”,“周二和周四”,80,3.9);
lessonList[2]=新课(“瑜伽”,“周日”,83,4.0);
lessonList[3]=新课(“塑身”,“周五和周六”,90,3.85);
for(int count=0;countlessonList.length | selection<0){
System.out.println(“您能重新输入选择吗?”);
选择=sc.nextInt();
}
System.out.println((选择+1)+“+”+lessonList[selection]);
}
}
公共课{
私人弦乐小标题;
私有字符串lessonDate;
私人双套教材价格;
私人双重评级;
公共课程(){//默认构造函数
this.lessonTitle=null;
this.lessonDate=null;
这个.lessonPrice=0;
这一等级=0;
}
公共课程(字符串标题、字符串日期、双倍价格、双倍费率){//第二个构造函数
this.lessonTitle=标题;
this.lessonDate=日期;
this.lessonPrice=价格;
这个。评级=比率;
}
**//当您想要打印一个类时,您必须需要一个正确定义的toString方法**
公共字符串toString(){
返回“课程标题:+this.lessonTitle+”,课程日期:+this.lessonDate+,$“+this.lessonPrice+”,评分:+this.rating;
}
}

你问的问题不太清楚。你能不能请你澄清一下?
公共void课程(字符串标题)
不是构造函数,而是一个名为
Lesson
void
方法。你需要删除
void
课程!=
课程
(int count=0;count
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;             
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();
    }
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;
    }

}