开关中有奇怪的错误

时间:2015-09-26 06:41:27

标签: java loops switch-statement

我创建了一个用于处理刑事案件并存储它的程序,然后我添加了另一个开关,以便我可以访问我希望在程序中添加的其他内容。但是执行选择时似乎有错误。 Switch不会识别我的选择,而是重复循环中的菜单。编译期间没有错误。这是编码...

import java.util.ArrayList;
import java.util.Scanner;

public class CriminalCase {




    private String batput;
    public String getBatput(){return batput;}


    public CriminalCase(String batput){
        this.batput = batput;

    }

private static class robin{
   String Batman(){
Scanner s=new Scanner (System.in);
System.out.println();
System.out.println("Enter name.");
String a=s.nextLine();
System.out.println("Enter Date of birth.");
String b=s.nextLine();
System.out.println("Enter Sex.");
String c=s.nextLine();
System.out.println("Enter Crime Committed.");
String d=s.nextLine();
System.out.println("Enter Date of Crime Committed.");
String e=s.nextLine();
System.out.println("Enter Victim.");
String f=s.nextLine();
System.out.println();
String g=""+"\n"+""+"Name:- "+a +"\nDOB:- "+b +"\nSex:- "+c +"\nCrime Committed:- "+d +"\nDate of Crime Committed:- "+e +"\nVictim:- "+f; 
System.out.println();
return g;
  }
}



    public static void main(String[] args) {


        ArrayList<CriminalCase> cases = new ArrayList<>();
        boolean quit = false;    

        Scanner s = new Scanner(System.in);

         robin j=new robin();  
        boolean exit=false;
for(;!exit;){
System.out.println("For cases press 1.\nFor printing thank you, press 2.\nTo exit, press 3.");
int choice=s.nextInt();
switch (choice){
case 1:{

        while (!quit) {
            System.out.println();
            System.out.println("To view current cases enter v\nto add a case enter a\nto quit enter q");
            String input = s.nextLine();

            switch(input){
                case ("v"): {
                    System.out.println("");
                    System.out.println("The following cases exist:");
                    System.out.println("\nName:- Batman\nDOB:- Unknown\nSex:- Male\nCrime Committed:- Tresspassing a crime scene, Fleeing scene of crime, Carrying unlicensed vehicles and 

weapons.\nDate of Crime Committed:- 18/9/2015\nVictim:- None.");
                    for (CriminalCase c : cases)
                    System.out.println(c.getBatput());
                    break;
                }
                case("a"):{
                    String batput=j.Batman();

                    cases.add(new CriminalCase(batput));
                    break;
                }
                case("q"):{
                    quit = true;


                }

            }
        }
break;    
}
case 2:System.out.println("Thank you."); 
break;

case 3:exit=true;
        }
     }
  }

}

2 个答案:

答案 0 :(得分:2)

如果没有测试,我猜错误就在这段代码中:

int choice=s.nextInt();
switch (choice)
{
case 1:{

    while (!quit) {
        System.out.println();
        System.out.println("To view current cases enter v\nto add a case enter a\nto quit enter q");
        String input = s.nextLine();

您正在使用nextInt()方法读取整数,然后,您应该按照以下方式调用nextLine()

int choice=s.nextInt();
s.nextLine()
switch (choice)
{
....

请查看此问题以了解此行为:

Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods

编辑(我的个人建议):

我正在读取一行中的单个整数,如下所示:

int choice = Integer.parseInt(s.nextLine());

尝试实施此版本

答案 1 :(得分:1)

布尔值没有被重置为false,因此从未访问过循环! 这是答案的最终编码!

import java.util.ArrayList;
import java.util.Scanner;

public class CriminalCase {




    private String batput;
    public String getBatput(){return batput;}


    public CriminalCase(String batput){
        this.batput = batput;

    }

private static class robin
{
   String Batman()
{
Scanner s=new Scanner (System.in);
System.out.println();
System.out.println("Enter name.");
String a=s.nextLine();
System.out.println("Enter Date of birth.");
String b=s.nextLine();
System.out.println("Enter Sex.");
String c=s.nextLine();
System.out.println("Enter Crime Committed.");
String d=s.nextLine();
System.out.println("Enter Date of Crime Committed.");
String e=s.nextLine();
System.out.println("Enter Victim.");
String f=s.nextLine();
System.out.println();
String g=""+"\n"+""+"Name:- "+a +"\nDOB:- "+b +"\nSex:- "+c +"\nCrime Committed:- "+d +"\nDate of Crime Committed:- "+e +"\nVictim:- "+f; 
System.out.println();
return g;
}
}



    public static void main(String[] args) {


        ArrayList<CriminalCase> cases = new ArrayList<>();
        boolean quit = false;    

        Scanner s = new Scanner(System.in);

         robin j=new robin();  
        boolean exit=false;
for(;!exit;)
{
System.out.println("For cases press 1.\nFor printing thank you, press 2.\nTo exit, press 3.");
int choice=Integer.parseInt(s.nextLine());
switch (choice)
{
case 1:{

        while (!quit) {
            System.out.println();
            System.out.println("To view current cases enter v\nto add a case enter a\nto quit enter q");
            String input = s.nextLine();
            switch(input){
                case ("v"): {
                    System.out.println("");
                    System.out.println("The following cases exist:");
                    System.out.println("\nName:- Batman\nDOB:- Unknown\nSex:- Male\nCrime Committed:- Tresspassing a crime scene, Fleeing scene of crime, Carrying unlicensed vehicles and 

weapons.\nDate of Crime Committed:- 18/9/2015\nVictim:- None.");
                    for (CriminalCase c : cases)
                    System.out.println(c.getBatput());
                    break;
                }
                case("a"):{
                    String batput=j.Batman();

                    cases.add(new CriminalCase(batput));
                    break;
                }
                case("q"):{
                    quit = true;


                }

            }
        }
quit=false;
break;    
}
case 2:System.out.println("Thank you."); 
break;

case 3:exit=true;
}
}
}

}