循环,用Java重复程序(Java初学者问题)
我想当用户输入任何选项(1,2,3,4)时,它将显示用户(仍处于压缩状态),然后他再次返回程序。我怎样才能使用if station或其他方法来实现这一点呢循环,用Java重复程序(Java初学者问题),java,Java,我想当用户输入任何选项(1,2,3,4)时,它将显示用户(仍处于压缩状态),然后他再次返回程序。我怎样才能使用if station或其他方法来实现这一点呢 import java.util.Scanner; public class Tt { public static void main(String [] args) { Scanner kb= new Scanner (System.in); int choice; do{
import java.util.Scanner;
public class Tt {
public static void main(String [] args) {
Scanner kb= new Scanner (System.in);
int choice;
do{
System.out.println("Please enter your choice from the following menu:");
System.out.println("1. Enter student tanscript");
System.out.println("2. Display transcript summary");
System.out.println("3. Read student transcript from a file");
System.out.println("4. Write 1transcript summary to a file");
System.out.println("5. Exit");
choice = kb.nextInt();
switch (choice) {
case 1:
case 2:
case 3:
case 4:
System.out.println("Under construction");
System.out.println();
break;
case 5:
break;
}
}while (choice > 0 && choice < 5);
}
}
import java.util.Scanner;
公共类Tt{
公共静态void main(字符串[]args){
扫描仪kb=新扫描仪(System.in);
智力选择;
做{
System.out.println(“请从以下菜单中输入您的选择:”);
System.out.println(“1.输入学生脚本”);
System.out.println(“2.显示成绩单摘要”);
System.out.println(“3.从文件中读取学生成绩单”);
System.out.println(“4.将1个Transcript摘要写入文件”);
System.out.println(“5.Exit”);
choice=kb.nextInt();
开关(选择){
案例1:
案例2:
案例3:
案例4:
System.out.println(“在建”);
System.out.println();
打破
案例5:
打破
}
}而(选择>0&&choice<5);
}
}
可能是模式匹配
String pattern = "[1234]";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
input = br.readLine();
if(input.matches(pattern)) {
// construction
}
也许是模式匹配
String pattern = "[1234]";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
input = br.readLine();
if(input.matches(pattern)) {
// construction
}
或
或
您可以使用一个选项执行器数组来代替开关。当用户点击一个数字(即1)时,它与数组元素0相关,然后执行该数组元素。这允许更多的可扩展性,因为您可以创建新的执行器
private interface Executor {
public void run();
}
...
public static void main(String[] str) {
Executor temp = new Executor() {
public void run() {
System.out.println("Under Construction");
}
}
Executor[] ex = {temp, temp, temp, temp};
while(true) {
System.out.println("Please enter your choice from the following menu:");
System.out.println("1. Enter student transcript");
System.out.println("2. Display transcript summary");
System.out.println("3. Read student transcript from a file");
System.out.println("4. Write 1transcript summary to a file");
System.out.println("5. Exit");
choice = kb.nextInt();
if(choice > 0 && choice < ex.length) {
ex[choice - 1].run();
} else {
break;
}
}
}
专用接口执行器{
公开无效运行();
}
...
公共静态void main(字符串[]str){
执行器温度=新执行器(){
公开募捐{
System.out.println(“在建”);
}
}
执行器[]ex={temp,temp,temp};
while(true){
System.out.println(“请从以下菜单中输入您的选择:”);
System.out.println(“1.输入学生成绩单”);
System.out.println(“2.显示成绩单摘要”);
System.out.println(“3.从文件中读取学生成绩单”);
System.out.println(“4.将1个Transcript摘要写入文件”);
System.out.println(“5.Exit”);
choice=kb.nextInt();
if(选项>0&&choice
您可以拥有一个选项执行器数组,而不是开关。当用户点击一个数字(即1)时,它与数组元素0相关,然后执行该数组元素。这允许更多的可扩展性,因为您可以创建新的执行器
private interface Executor {
public void run();
}
...
public static void main(String[] str) {
Executor temp = new Executor() {
public void run() {
System.out.println("Under Construction");
}
}
Executor[] ex = {temp, temp, temp, temp};
while(true) {
System.out.println("Please enter your choice from the following menu:");
System.out.println("1. Enter student transcript");
System.out.println("2. Display transcript summary");
System.out.println("3. Read student transcript from a file");
System.out.println("4. Write 1transcript summary to a file");
System.out.println("5. Exit");
choice = kb.nextInt();
if(choice > 0 && choice < ex.length) {
ex[choice - 1].run();
} else {
break;
}
}
}
专用接口执行器{
公开无效运行();
}
...
公共静态void main(字符串[]str){
执行器温度=新执行器(){
公开募捐{
System.out.println(“在建”);
}
}
执行器[]ex={temp,temp,temp};
while(true){
System.out.println(“请从以下菜单中输入您的选择:”);
System.out.println(“1.输入学生成绩单”);
System.out.println(“2.显示成绩单摘要”);
System.out.println(“3.从文件中读取学生成绩单”);
System.out.println(“4.将1个Transcript摘要写入文件”);
System.out.println(“5.Exit”);
choice=kb.nextInt();
if(选项>0&&choice
我不太清楚你这个问题的确切意思。如果用户选择“正在施工”选项,是否允许用户再次选择?在这种情况下,我会将其分解为一个方法,可以重新调用该方法以再次显示菜单
public static void main(String [] args) {
showMenu();
}
public static void showMenu() {
Scanner kb = new Scanner (System.in);
int choice;
System.out.println("Please enter your choice from the following menu:");
System.out.println("1. Enter student tanscript");
System.out.println("2. Display transcript summary");
System.out.println("3. Read student transcript from a file");
System.out.println("4. Write 1transcript summary to a file");
System.out.println("5. Exit");
choice = kb.nextInt();
switch (choice) {
case 1:
case 2:
case 3:
case 4:
System.out.println("Under construction");
System.out.println();
showMenu();
return;
case 5:
return;
default:
showMenu();
return;
}
}
如果要删除冗长的switch语句,可以创建一个Map
,其中MenuAction
是一个接口,该接口具有执行该行为的方法DoAction
public interface MenuAction {
void doAction();
}
public UnderConstructionAction implements MenuAction {
public void doAction() {
System.out.println("Under construction");
System.out.println();
}
}
public ExitAction implements MenuAction {
public void doAction() {
}
}
public class MainClass {
static {
Map<Integer, MenuAction> menuActions = new HashMap<Integer, MenuAction>();
menuActions.put(1, new UnderConstructionAction());
menuActions.put(2, new UnderConstructionAction());
menuActions.put(3, new UnderConstructionAction());
menuActions.put(4, new UnderConstructionAction());
menuActions.put(5, new ExitAction());
}
public static void main(String [] args) {
showMenu();
}
public static void showMenu() {
Scanner kb = new Scanner (System.in);
int choice;
System.out.println("Please enter your choice from the following menu:");
System.out.println("1. Enter student tanscript");
System.out.println("2. Display transcript summary");
System.out.println("3. Read student transcript from a file");
System.out.println("4. Write 1transcript summary to a file");
System.out.println("5. Exit");
choice = kb.nextInt();
if (!menuActions.containsKey(choice)) {
showMenu();
return;
}
menuActions.get(choice).doAction();
}
}
公共接口菜单操作{
无效动作();
}
公共欠构造实现菜单操作{
公共无效行为(){
System.out.println(“在建”);
System.out.println();
}
}
公共出口执行菜单操作{
公共无效行为(){
}
}
公共类主类{
静止的{
Map menuActions=new HashMap();
menuActions.put(1,新的UnderstructionAction());
menuActions.put(2,新的UnderstructionAction());
menuActions.put(3,新的UnderstructionAction());
menuActions.put(4,新的UnderstructionAction());
menuActions.put(5,newexitAction());
}
公共静态void main(字符串[]args){
showMenu();
}
公共静态无效显示菜单(){
扫描仪kb=新扫描仪(System.in);
智力选择;
System.out.println(“请从以下菜单中输入您的选择:”);
System.out.println(“1.输入学生脚本”);
System.out.println(“2.显示成绩单摘要”);
System.out.println(“3.从文件中读取学生成绩单”);
System.out.println(“4.将1个Transcript摘要写入文件”);
System.out.println(“5.Exit”);
choice=kb.nextInt();
if(!menuActions.containsKey(选项)){
showMenu();
返回;
}
menuActions.get(choice.doAction();
}
}
您甚至可以进一步创建一个StudentTranscriptAction
,TranscriptSummaryAction
等,它继承自UnderstructionAction
,但有一个Description
字段,并使用这些字段建立菜单输出
注意:我只做了一点Java,根本没有测试过这段代码。我不太确定你所说的问题的确切含义。如果用户选择“正在施工”选项,是否允许用户再次选择?在这种情况下,我会将其分解为一个方法,可以重新调用该方法以再次显示菜单
public static void main(String [] args) {
showMenu();
}
public static void showMenu() {
Scanner kb = new Scanner (System.in);
int choice;
System.out.println("Please enter your choice from the following menu:");
System.out.println("1. Enter student tanscript");
System.out.println("2. Display transcript summary");
System.out.println("3. Read student transcript from a file");
System.out.println("4. Write 1transcript summary to a file");
System.out.println("5. Exit");
choice = kb.nextInt();
switch (choice) {
case 1:
case 2:
case 3:
case 4:
System.out.println("Under construction");
System.out.println();
showMenu();
return;
case 5:
return;
default:
showMenu();
return;
}
}
如果要删除冗长的switch语句,可以创建一个Map
,其中MenuAction
是一个
public class Foo extends Observable {
// The Observers would normally be in their own file
static class OneHandler implements Observer {
public void update(Observable o, Object val) {
if (val != null && val.equals(1)) {
System.out.println("One pressed");
}
}
}
static class TwoHandler implements Observer {
public void update(Observable o, Object val) {
if (val != null && val.equals(2)) {
System.out.println("Two pressed");
}
}
}
static class EverythingHandler implements Observer {
public void update(Observable o, Object val) {
if (val != null) {
System.out.println(val + " pressed");
} else {
System.out.println("Null pressed");
}
}
}
public void askQuestion() {
// ask the question
System.out.println("Ask Question");
setChanged(); // otherwise observers are not notified
notifyObservers(1); // in this example 1 is pressed (will be autoboxed to Integer)
}
public static void main(String[] args) {
// main and Foo would usually not be in the same class
Foo foo = new Foo();
// Register observers.
// Note that you do not bind OneHandler to 1 here, but that OneHandler
// itself knows when to react. It could be that more Observables would react
// to the same event
// You would not know the order in which they are called.
foo.addObserver(new OneHandler());
foo.addObserver(new TwoHandler());
foo.addObserver(new EverythingHandler());
foo.askQuestion();
}
}