Eingabe für Benutzer schreiben

bRainLaG

Mitglied
Hallo ich würde gerne eine Methode schreiben, die dem Benutzer die Wahl lässt, welche Methode er aufrufen möchte, und diese dann aufgerufen wird.

Ich habe von Benutzereingaben noch nicht so die Ahnung wollte mal fragen ob mir jemand ein Programmbeispiel geben kann, in der zumindest eine Methode durch nachfragen implementiert wird.

Code:
 public void append(String s) {
	        Cell c = new Cell(s);
	        if (front == null) front = c;
	        else rear.next = c;
	        rear = c;
	    }
	    /** fügt s am Anfang der Schlange ein,
	     *  s wird neues erstes Element */
	    public void insert(String s) {
	        Cell c = new Cell(s);
	        if (rear == null) rear = c;
	        else c.next = front;
	        front = c;
	    }
	    /** entfernt das vorderste Element und liefert es zurück
	     *  @throws QueueUnderflow falls Schlange leer */
	    public String remove() throws QueueUnderflow {
	        if (front == null) throw new QueueUnderflow();
	        Cell c = front;
	        front = front.next;
	        if (front == null) rear = null;
	        return c.value;
 
Hallo ich würde gerne eine Methode schreiben, die dem Benutzer die Wahl lässt, welche Methode er aufrufen möchte, und diese dann aufgerufen wird.

Ich habe von Benutzereingaben noch nicht so die Ahnung wollte mal fragen ob mir jemand ein Programmbeispiel geben kann, in der zumindest eine Methode durch nachfragen implementiert wird.

Moin,

irgendwie verstehe ich Deine Frage nicht so wirklich ... :confused:

Möchtest Du in etwa so was machen :
Code:
// Pseudocode
ArtDerMethode = getBenutzerabfrage();

if( ArtDerMethode == 'A' )
{
    // führe Methode A() aus
}
else if( ArtDerMethode == 'B' )
{
    // führe Methode B() aus
}
else if( ArtDerMethode == 'X' )
{
    // führe Methode X() aus
}
else
{
    ...
}

Und was bedeutet (in diesem Zusammenhang) der von Dir gepostete Code :confused:

Gruß
Klaus
 
Hallo,

schau mal hier:
Java:
package de.tutorials;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Date;
import java.util.Scanner;

public class CommandInterpreter {

  /**
   * @param args
   */
  public static void main(String[] args) {
    new CommandInterpreter().repl();
  }
  
  private void repl() {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Please enter a command!");
    System.out.print("> ");
    System.out.flush();
    while(scanner.hasNextLine()){
      String line = scanner.nextLine().trim();
      if(line.length() > 0){
        String methodName = line;
        tryInvokeMethodWithoutArguments(methodName);
        System.out.print("> ");
        System.out.flush();
      }
    }    
  }

  private void tryInvokeMethodWithoutArguments(String methodName) {
    try {
      getClass().getDeclaredMethod(methodName).invoke(this);
    }catch(NoSuchMethodException noSuchMethodException){
      System.out.println("unknown command: " + methodName);
      help();
     return; 
    }catch (Exception e) {
      e.printStackTrace();
    }
  }
  
  void help(){
    System.out.println("available commands: ");
    for(Method m : getClass().getDeclaredMethods()){
      int mod = m.getModifiers();
      if(!Modifier.isStatic(mod) && !Modifier.isPrivate(mod)){
        System.out.println(m.getName());
      }
    }
  }

  void test(){
    System.out.println("Test");
  }
  
  void time(){
    System.out.println(new Date());
  }
  
  void exit(){
    System.out.println("Bye bye...");
    System.exit(0);
  }
}

Ausgabe:
Code:
Please enter a command!
> test
Test
>  time
Thu Dec 03 18:43:58 CET 2009
> help
available commands: 
exit
test
time
help
> fuck
unknown command: fuck
available commands: 
exit
test
time
help
> exit
Bye bye...

Gruß Tom
 
Zurück