Jump to content

Recommended Posts

Posted

Boas ppl...

Alguem me pode dar uma dica? Como saber o valor, em inteiro, das teclas???????

Isto é, tenho a seguinte funcão para ler o input:

//Ler informacao a partir do teclado
	public static String leString(){
		String s = " ";

		try{
    		byte [] b = new byte[128];
    		int lidos = System.in.read(b);
    		//lidos-1 para tirar o caracter "enter"
    		s         = new String(b,0,lidos-1);
		}

		catch(Exception e){ s = "Erro!!!";}
		return s;
	}

Agora a duvida tá em fazer:

int entrada = ??????.leString();

ou

int entrada = leString().?????????;

para depois fazer um

while(entrada <= x){
    if(entrada == x-1){
System.out.println("Editor " + "\n" );
    }

  (....)


}  // x <- um inteiro que depois definirei

E assim, dependendo da tecla pressionada, executa determinado método!!!

Obrigado!!!

Posted

Nao garanto que a sintaxe seja exactamente assim (nao me apetece abrir o IDE neste PC)


        int x = 20;
        int entrada = x;
        while (entrada <= x) {

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            String integerAsString = bufferedReader.readLine();
            try {
                entrada = Integer.parseInt(integerAsString);
            } catch (NumberFormatException e) {
                // do nothing
            }

        }

Ou , se quiseres da tua forma


public int leString(){
      int entrada = x;
        while (entrada <= x) {

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            String integerAsString = bufferedReader.readLine();
            try {
                entrada = Integer.parseInt(integerAsString);
            } catch (NumberFormatException e) {
                // do nothing
            }

        }
       return entrada;
}


e chamas:

int oNumeroLido = leString();
       

Posted

pois tens da apanhar ou lançar

podes fazer por ex:


public int leString(){
      int entrada = x;
        while (entrada <= x) {

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            String integerAsString = bufferedReader.readLine();
            try {
                entrada = Integer.parseInt(integerAsString);
            } catch (NumberFormatException e) {
                // do nothing
            } catch (IOException e){
               System.out.println("Erro do sistema.");
            }

        }
       return entrada;
}

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.