0

私はこのコードを持っています:

byte[] nombre = new byte[20];
                System.out.print("Cuenta a modificar: ");
                cuenta = s.nextInt();
                boolean existe = estaRepetido(cuenta);
                if (existe == false){
                    System.out.println("La cuenta no existe");
                }
                else {
                    String nomCliente;
                    System.out.print("Nombre: ");
                    nomCliente = s.nextLine();
                    System.out.print("Cantidad: ");
                    double cantidad = Double.parseDouble(s.nextLine());
                    for( int i = 0; i < 20 && i < nomCliente.getBytes().length; i++ ){
                        nombre[i] = nomCliente.getBytes()[i];
                    }
                    String nomModificar = new String (nombre);
                    modificar(cuenta, nomModificar, cantidad);
                }

しかし、ターミナルで実行すると、海外でnomCliente = s.nextLine(); 次のようなもので終わります:

Cuenta a modificar: 0
Nombre: Cantidad: 0

何か案は?これは非常に大きな方法の一部にすぎませんが、問題を引き起こしているのはこれだけです。

4

2 に答える 2

1

このnextInt()メソッドは(終了行)記号を残し、次の入力をスキップして、\nによってすぐに取得されます。nextLine()あなたがしたいのはnextLine()、すべてに使用し、後でそれを解析することです:

String nextIntString = keyboard.nextLine(); //get the number as a single line
int nextInt = Integer.parseInt(nextIntString); //convert the string to an int

これは、問題を回避するための最も簡単な方法です。「次の」方法を混在させないでください。のみを使用し、その後でsまたは個別の単語nextLine()を解析します。int

于 2013-03-25T22:35:31.003 に答える
0

s.nextLine()次の入力をスキャンする前に、ユーザーがキーを入力するのを待つ場合に使用する必要があり ます。使用する:

try {
    cuenta = Integer.parseInt(s.nextLine());
} catch (Exception e){
    //Do whatever you want.
}
于 2013-03-25T22:35:39.910 に答える