System.out.println("请选择运输方式:1.卡车    2.火车    3.飞机");
String method = scan.next();
while(!method.matches("\\d+([.]\\d+)?")||!method.matches("[1-3]"))
{
System.out.println("输入有误,请重新输入!");
method = scan.next();
}那int呢?
用什么?System.out.println("请选择操作:1.查询    2.删除    3.新增");
Scanner scan = new Scanner(System.in);
int selection = scan.nextInt();
while(!selection.matches("\\d+([.]\\d+)?")||selection<1||selection>3)
{
System.out.println("输入有误,请重新输入!");
selection = scan.nextInt();
}
switch(selection)
{
case 1:
display();
break;
case 2:
delete();
break;
case 3:
add();
}

解决方案 »

  1.   

    如果用String那后面Switch就不能用了
      

  2.   

    你可以再定义一个String变量去做
      

  3.   

    不能用switch,那就不用嘛。多简单,这问题还好意思拿出来问。
    System.out.println("请选择操作:1.查询    2.删除    3.新增");
    Scanner scan = new Scanner(System.in);
    String selection = scan.next();
    while(!selection.matches("\\d+([.]\\d+)?")||!selection.matches("[1-3]"))
    {
    System.out.println("输入有误,请重新输入!");
    selection = scan.next();
    }
    if(selection.equals(1))
    {
    display();
    }
    else if(selection.equals(2))
    {
    delete();
    }
    else
    {
    add();
    }
      

  4.   

    已经是int了怎么可能出现小数点?
    selection.matches("\\d+([.]\\d+)?")
    还有啥意义
      

  5.   

    这个好像是正则表达式,String专用。我用了那个if else if , 运行不管选123 都执行第一个啊,为什么呢?求解
      

  6.   

    可以先用String来判断,然后再转换成数值类型
    System.out.println("请选择操作:1.查询    2.删除    3.新增");
            Scanner scan = new Scanner(System.in);
            String method = scan.next();
            //int selection = scan.nextInt();
            while(!selection.matches("\\d+([.]\\d+)?")||selection<1||selection>3)
            {
                System.out.println("输入有误,请重新输入!");
                selection = scan.nextInt();
            }
            int selection = (int)Float.parseFloat(method);
            switch(selection)
            {
            case 1:
                display();
                break;
            case 2:
                delete();
                break;
            case 3:
                add();                
            }
      

  7.   

    String可以用这样的吗  selection<1||selection>3
      

  8.   

    这两种实现方式,可以限定123才能执行但是问题是:不管输入123,都只执行第一个方法,求解!完整代码:
    package community;import java.util.List;
    import java.util.Scanner;import user.DbStore;public class CommunityManager 
    {
    public static void main(String args[])
    {
    System.out.println("请选择操作:1.查询    2.删除    3.新增");
    Scanner scan = new Scanner(System.in);
    String method = scan.next();
    while(!method.matches("\\d+([.]\\d+)?")||!method.matches("[1-3]"))
    {
    System.out.println("输入有误,请重新输入!");
    method = scan.next();
    }
    int selection = (int)Float.parseFloat(method);
    switch(selection)
    {
    case 1:
    display();
    break;
    case 2:
    delete();
    break;
    case 3:
    add();
    }
    }

    public static void display()
    {
    DbStore abcd = new DbStore();
    List<CommunityEntity> list = abcd.getCommList();

    Scanner scan = new Scanner(System.in);
    System.out.println("请输入区域:");
    String area = scan.next();

    boolean judge = false; //设置一个标志变量
    for(int i=0;i<list.size();i++)
    {
    if(list.get(i).getCommArea().equals(area))
    {
    System.out.println(list.get(i));
    judge = true;
    break;
    }
    }
    if(!judge)
    {
    System.out.println("输入有误或该区域不存在!");
    }
    } public static void delete()
    {
    DbStore abcd = new DbStore();
    List<CommunityEntity> list = abcd.getCommList();

    Scanner scan = new Scanner(System.in);
    System.out.println("请输入要删除的社区编号");
    String deletingId = scan.next();

    boolean judge = false;
    for(int i=0;i<list.size();i++)
    {
    if(list.get(i).getCommId().equals(deletingId))
    {
    System.out.println("您要删除的社区编号是:"+deletingId);
    list.remove(i);
    System.out.println("删除成功!余下的小区信息:");
    System.out.println("小区编号"+"\t\t"+"小区名称"+"\t\t"+"小区经理"+"\t\t"+"区域"+"\t\t"+"小区状态"+"\t\t"+"渗透率"+"\t\t"+"覆盖用户数");
    System.out.println("----------------------------------------------------------------------------------------------------------");
    judge = true;
    break;
    }
    }
    if(!judge)
    {
    System.out.println("该社区不存在!");
    }
    for(int j=0;j<list.size();j++)
    {
    System.out.println(list.get(j).getCommId()+"\t\t"+list.get(j).getCommName()+"\t\t"+list.get(j).getCommManager()+"\t\t"+list.get(j).getCommArea()+"\t\t"+list.get(j).getCommStatue()+"\t\t"+list.get(j).getPermeability()+"\t\t"+list.get(j).getUserCount()); }
    } public static void add()
    {
    DbStore abcd = new DbStore();
    Scanner scan = new Scanner(System.in);
    System.out.println("请输入小区编号:"+"\r\n");
    String id = scan.next();
    System.out.println("请输入小区名称:"+"\r\n");
    String name = scan.next();
    System.out.println("请输入小区经理:"+"\r\n");
    String manager = scan.next();
    System.out.println("请输入所在区域:"+"\r\n");
    String area = scan.next();
    System.out.println("请输入小区状态:"+"\r\n");
    String statue = scan.next();
    System.out.println("请输入渗透率:"+"\r\n");
    double permeability = scan.nextDouble();
    System.out.println("请输入覆盖用户数:"+"\r\n");
    int userCount = scan.nextInt();

    List<CommunityEntity> list = abcd.getCommList();
    CommunityEntity ce = new CommunityEntity();
    ce.setCommId(id);
    ce.setCommName(name);
    ce.setCommManager(manager);
    ce.setCommArea(area);
    ce.setCommStatue(statue);
    ce.setPermeability(permeability);
    ce.setUserCount(userCount);
    list.add(ce); System.out.println("新增成功!");
    System.out.println("该社区信息如下:"+"\n");
    System.out.println("小区编号"+"\t\t"+"小区名称"+"\t\t"+"小区经理"+"\t\t"+"区域"+"\t\t"+"小区状态"+"\t\t"+"渗透率"+"\t\t"+"覆盖用户数");
    System.out.println("----------------------------------------------------------------------------------------------------------");
    System.out.println(ce.getCommId()+"\t\t"+ce.getCommName()+"\t\t"+ce.getCommManager()+"\t\t"+ce.getCommArea()+"\t\t"+ce.getCommStatue()+"\t\t"+ce.getPermeability()+"\t\t"+ce.getUserCount());
    }}
      

  9.   

    JAVA1.7已经支持使用case语句使用String了貌似