java题声明布尔变量isRight来标识用户的输入是否正确 声明布尔变量isRight来标识用户的输入是否正确,初值为true.如果输入错误,其值变为false。使用do-while循环:循环体中接受用户的输入,利用switch语句执行不同的操作,循环体至少执行一次循环条件是判断isRight的值。如果为false 则继续执行循环体;否则退出循环,程序结束有注释更好 谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 朋友,图片在这个上面是看不到的,我做了的例子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(); } 上面少复制了一点,这个类名是Testimport 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(); }} 谢谢啊,不过不是我想要的结果。我们刚学java,你里面很多内容都还没有学到哪。我们只学了基本的变量。运算符,if选择结构,switch选择结构,while循环和do while.for还没学那。 由于看不到你的图,所以。你可能是要这种吧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(); }} LZ要的是常见的菜单系统还是只运行一次的菜单程序?看LZ的需求应该是后者,用4楼的代码就可以实现了。 本人收集,学习技术必不可少教程 包怎么生成和编译 大家来讨论一下classpath 求不连续的随机数字 ServletRequest问题?在线(to :cbhyk() 、zhousm6([email protected])) 是否安装了JBuilder8之后就不用安装JDK了?里面已经包含所有的API了么? 招聘java项目经理,月薪8000-10000 在jBuilder中怎样开发RMI 请问用JDBC怎么做一对多的关系的主/从表? 怎么OR mapping? WCP中是如何从泛型中获取泛型T的 JIN类型转换函数内存泄漏 菜鸟向大神求教~~~
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();
}
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();
}
}
我们只学了基本的变量。运算符,if选择结构,switch选择结构,while循环和do while.
for还没学那。
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();
}
}