Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/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 If,else If循环问题_Java_Loops_If Statement_Arraylist_Try Catch - Fatal编程技术网

Java If,else If循环问题

Java If,else If循环问题,java,loops,if-statement,arraylist,try-catch,Java,Loops,If Statement,Arraylist,Try Catch,所以我刚刚完成我的第一节计算机编程课,我的主要方法有问题。我有两门课。第一个类是homeClass.java,另一个类是homeInventory.java。我将向您展示一些我的homeInventory.java类,除非您更聪明的ppl认为使用我的其他类是明智的。基本上,我的else-if语句有问题。程序运行w/if语句,但不会完全运行其他选项(else-if)。我肯定错过了一些很明显的东西,但我看看你有什么要说的。提前谢谢 public static void main(String[] a

所以我刚刚完成我的第一节计算机编程课,我的主要方法有问题。我有两门课。第一个类是homeClass.java,另一个类是homeInventory.java。我将向您展示一些我的homeInventory.java类,除非您更聪明的ppl认为使用我的其他类是明智的。基本上,我的else-if语句有问题。程序运行w/if语句,但不会完全运行其他选项(else-if)。我肯定错过了一些很明显的东西,但我看看你有什么要说的。提前谢谢

public static void main(String[] args) {
        ArrayList<homeClass> homes = new ArrayList<>();
        Scanner scnr = new Scanner(System.in);

        int i = 0;
        int j = 0;
        String option = "";
        String answer = "";
        String statusAnswer = "";


        do {
            System.out.println("Menu:");
            System.out.println("1. Add a new home.");
            System.out.println("2. Remove a home.");
            System.out.println("3. Update home sale status.");
            System.out.println("4. Exit");
            System.out.print("Option chosen: ");
        try {
            while(true) {
                if(scnr.hasNext()) {
                    option = scnr.next();
                break;
                }
            }
        }
        catch (NoSuchElementException NSEE) {
            continue;
        }
        catch (Exception excpt) {
            excpt.printStackTrace();
            System.out.print("Error, non-integer");
        break;
        }
            try {           
                    if(option.equals("1")) {
                        homes.add(addHome(scnr));
                        System.out.println("Home added.");
                        homes.get(i).getListing();
                    break;
                    }
                    else if (option.equals("2")) {
                        for(j = 0; j < homes.size(); ++j) {
                            homes.get(i).getListing();
                            System.out.print("Remove this listing? Y or N: ");
                            answer = scnr.next();
                            if (answer.equalsIgnoreCase("Y")) {
                                homes.get(i).removeListing();
                                System.out.println("Home removed.");
                            }
                            else if (!answer.equalsIgnoreCase("Y")) {
                                System.out.println("Home not removed. Choose next option.");
                            }
                        }
                    break;
                    }
                    else if (option.equals("3")) {
                        for(j = 0; j < homes.size(); ++j) {
                            homes.get(i).getListing();
                            System.out.print("Do you want to change the sale status? Y or N: ");
                            answer = scnr.next();
                            if (answer.equalsIgnoreCase("Y")) {
                                System.out.println("Enter home sale status: ");
                                    statusAnswer = scnr.next();
                                    homes.get(i).setSaleStatus(statusAnswer);
                                    homes.get(i).getListing();
                            }   
                        }
                    break;
                    }
            }
            catch (Exception excpt) {
                excpt.printStackTrace();
                System.out.println("Error, option failed.");
            }
        } while(!option.equals("4"));


        System.out.print("Do you want the home information stored in a file? Y or N: ");
            answer = scnr.next();

            String outputFileName = "C:\\Temporary\\Home.txt";

            for (j = 0; j < homes.size(); ++j) {
                String listing = homes.get(i).getListing();
                if (answer.equalsIgnoreCase("Y")) {
                    try {
                        printToFile(listing, outputFileName);
                    } 
                    catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } 
                    catch (IOException e) {
                        e.printStackTrace();
                    }
                    System.out.println("File printed to: " + outputFileName);
                }
                else {
                    System.out.println("File not printed.");
                }
            }

        scnr.close();
        return;

    }
}



publicstaticvoidmain(字符串[]args){
ArrayList homes=新的ArrayList();
扫描仪scnr=新扫描仪(System.in);
int i=0;
int j=0;
字符串选项=”;
字符串答案=”;
字符串statusAnswer=“”;
做{
System.out.println(“菜单:”);
System.out.println(“1.添加新家”);
System.out.println(“2.移除一个家”);
System.out.println(“3.更新房屋销售状态”);
System.out.println(“4.Exit”);
系统输出打印(“选择选项:”);
试一试{
while(true){
if(scnr.hasNext()){
option=scnr.next();
打破
}
}
}
捕获(非接触元素异常NSEE){
继续;
}
捕获(例外情况除外){
printStackTrace()除外;
系统输出打印(“错误,非整数”);
打破
}
试试{
如果(选项等于(“1”)){
添加(添加主页(scnr));
System.out.println(“主页添加”);
homes.get(i.getListing();
打破
}
否则,如果(选项等于(“2”)){
对于(j=0;j
程序使用if语句运行,但不会完全通过其他选项运行-什么意思?我在哪里声明?它似乎是初始化的,因为对于选项1,您说homes.get(i),但在其他地方,它作为for循环计数器执行任务。尝试使for循环计数器int j=0,在本地声明,并且不在内部引用i。您的代码甚至不应该编译。您应该看到“在开关或循环外中断”编译错误。@MarsAtomic,就是这样!休息时间提前结束了。查看它在何处显示while(!option.equals(“4”))。Katelyn,试着评论一下中断声明。Jeremy Kahan和@MarsAtomic,我被宣布在我的主要声明的顶部,我没有包括在内。我绝对能做到。我试着用int j做for循环,结果程序停止了。如果我注释掉break语句,程序就不会运行If/else语句。我将添加其余的主要内容供您查看(在我原来的帖子中)。对不起,我没有解释清楚…这里肯定很挣扎