2016-12-12 3 views
-1

Ich arbeite an einem Projekt und aus irgendeinem Grund, wenn ich es starte und Option B auswähle, bringt es mich zurück zum Menü und ich bin mir nicht sicher warum, es tut nicht den Fall B Prozess ... Jemand Wer kann mir helfen, meinen Fehler zu finden? Vielen Dank!Vektoren und Arrays in Java

Das ist, was ich tat:

package arreglosprincipal; 

/** 
* 
* @author Thais Arelis 
*/ 
import java.util.Scanner; 
public class ProblemasArreglos { 
    Scanner sc= new Scanner(System.in); 
    int Arreglos []; 
    String opcion=" "; 
    public ProblemasArreglos(){ 
} 
public void llenarArreglo(){ 
    String MenuPrincipal=("Seleccione lo que desea hacer: " 
         +"\n A- Ingresar Valores" 
         +"\n B- Imprimir" 
         +"\n C- Calcular el mayor" 
         +"\n D- Ordenar Valores" 
         +"\n E- Buscar Valor" 
         +"\n F- Salir"); 
    do{ 
    System.out.println(MenuPrincipal); 
    opcion=sc.next(); 
     switch(opcion) { 
     case "A": 
     case "a": 
    System.out.println("How many numbers do you wish to enter?"); 
    int cantidad; 
    cantidad=sc.nextInt(); 
    Arreglos=new int[cantidad]; 
    for(int i=0;i<Arreglos.length;i++){ 
     System.out.println("Please write a number"); 
     Arreglos[i]=sc.nextInt(); 

    } 
    for(int i=0;i<Arreglos.length;i++){ 
     System.out.println("The numbers are:"+ Arreglos[i]); 
    } 
    System.out.println(" "); 
    break; 
    case "B": 
    case "b": 
     String Arreglos []= new String[5]; 
     String numero; 
    for(int i=0;i<Arreglos.length;i++){ 
     System.out.println("Numero:"); 
     numero=sc.nextLine(); 
     Arreglos[i]=numero;  
    } 
    for(int i=0;i<Arreglos.length;i++){ 
     System.out.println("Los numeros son:"+ Arreglos[i]); 
    } 
    break; 
} 
}while(opcion!="F"); 

} 
} 
+0

Unten Fall "b": Sie schrieb "String Arreglos [] = new String [5]". Bist du dir da sicher? Ich lese deinen Code, ich denke, es sollte "Arreglos = new int [5];" – Shondeslitch

Antwort

1

ich diesen Code in Eclipse lief, ist es normalerweise dann lief außer die Cursor unter System.out.print nicht an die neuen Linie war. Ein anderes Phänomen ist, dass das Drücken von "F" nicht wie erwartet beendet wird.

In Java vergleicht Object == Object die beiden Referenzen, nicht ihre Werte. Sie können char option verwenden, was ein primitiver Datentyp ist, oder option.euqals("F") verwenden, um Referenzvergleich zu vermeiden.