循环,重复Java程序(Java初学者问题)

时间:2011-04-24 20:35:29

标签: java

我希望当用户输入任何选项(1,2,3,4)时,它会显示用户(仍处于收缩状态),然后他再次返回该程序。如何使用if语句或SWTICH方法以外的其他方法?

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);

    }


}

5 个答案:

答案 0 :(得分:2)

if (choice == 1 || choice == 2 || choice == 3 || choice == 4) {
  System.out.println("Under construction");
  System.out.println();
}

if (choice >= 1 || choice <= 4) {
  System.out.println("Under construction");
  System.out.println();
}

编辑:如果你想要空间来实现每个选项(类似于你的switch语句现在给你的那个),你可以这样写:

if (choice == 1) {
  System.out.println("Under construction");
  System.out.println();
} else if (choice == 2) {
  System.out.println("Under construction");
  System.out.println();
} else if (choice == 3) {
  System.out.println("Under construction");
  System.out.println();
} else if (choice == 4) {
  System.out.println("Under construction");
  System.out.println();
} else {
  System.out.println("Unrecognised selection");
  System.out.println();
}

答案 1 :(得分:2)

您可以拥有一组选项执行程序,而不是切换。当用户点击数字(即1)时,它与数组元素0相关,然后数组元素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;
    }
  }
}

答案 2 :(得分:1)

我不太确定你的问题究竟是什么意思。如果用户选择“正在建设中”选项,是否允许用户再次选择?在这种情况下,我会将其分解为一个可以重新调用以再次显示菜单的方法。

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<int, MenuAction>,其中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();
    }
}

您甚至可以进一步创建StudentTranscriptActionTranscriptSummaryAction等继承自UnderConstructionAction但具有Description字段并使用这些字段构建菜单输出

注意:我已经完成了很少的Java,并且根本没有测试过这段代码。

答案 3 :(得分:0)

也许模式匹配?

String pattern = "[1234]";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));                
input = br.readLine();
    if(input.matches(pattern)) {
   // construction
}

答案 4 :(得分:0)

ICR的替代方案是使用Observer pattern。如果进行了选择,则会生成一个事件(比如推送JButton时),而其他对象可以订阅该事件。

您可以选择本地处理事件,例如Java Swing体系结构,或者选择像体系结构这样的中央事件总线。

一方面,观察者模式更容易扩展,因为您根本不必更改MainClass代码,另一方面,它可能会使代码更不透明,因为所有代码都依赖于运行时配置 - 哪些是侦听器注册了自己。

另请参阅最终知识网站上的示例wikipedia:)

一个例子:

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();
    }
}