声明布尔变量isRight来标识用户的输入是否正确,初值为true.如果输入错误,其值变为false。使用do-while循环:循环体中接受用户的输入,利用switch语句执行不同的操作,循环体至少执行一次循环条件是判断isRight的值。如果为false 则继续执行循环体;否则退出循环,程序结束有注释更好 谢谢

解决方案 »

  1.   

    朋友,图片在这个上面是看不到的,我做了的例子
    public static void main(String[] args) {
    Test test = new Test();
    boolean isnext = true;
    while (isnext) {
    Scanner sc = new Scanner(System.in);
    try {
    int option = test.input(sc);
    System.out.println(option);
    isnext = false;
    if (option > 4 || option < 1) {
    isnext = true;
    }
    } catch (Exception e) {
    }
    }
    System.out.println("输入成功");
    } public int input(Scanner sc) {
    System.out.println("请输入");
    return sc.nextInt();
    }
      

  2.   

    上面少复制了一点,这个类名是Test
    import java.util.Scanner;public class Test {
    public static void main(String[] args) {
    Test test = new Test();
    boolean isnext = true;
    while (isnext) {
    Scanner sc = new Scanner(System.in);
    try {
    int option = test.input(sc);
    System.out.println(option);
    isnext = false;
    if (option > 4 || option < 1) {
    isnext = true;
    }
    } catch (Exception e) {
    }
    }
    System.out.println("输入成功");
    } public int input(Scanner sc) {
    System.out.println("请输入");
    return sc.nextInt();
    }
    }
      

  3.   

    谢谢啊,不过不是我想要的结果。我们刚学java,你里面很多内容都还没有学到哪。
    我们只学了基本的变量。运算符,if选择结构,switch选择结构,while循环和do while.
    for还没学那。
      

  4.   

    由于看不到你的图,所以。你可能是要这种吧import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.util.Scanner;public class Test {
    public static void main(String[] args) {
    Test test = new Test();
    boolean isnext = true;
    while (isnext) {
    //接收用户的输入,你们可能用的是
    //BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    //bf.readLine();
    Scanner sc = new Scanner(System.in);
    try {
    int option = test.input(sc);
    switch (option) {
    case 1:
    isnext = true;
    System.out.println("增加");
    break;
    case 2:
    isnext = true;
    System.out.println("删除");
    break;
    case 3:
    isnext = true;
    System.out.println("修改");
    break;
    case 4:
    isnext = false;
    System.out.println("退出");
    break;
    }
    } catch (Exception e) {
    //这里你不管,就当是用户如果输入的是a、b、c不输入,系统不会退出而已
    }
    } } public int input(Scanner sc) {
    System.out.println("请输入");
    return sc.nextInt();
    }
    }
      

  5.   

    LZ要的是常见的菜单系统还是只运行一次的菜单程序?看LZ的需求应该是后者,用4楼的代码就可以实现了。