陷入Java中的矢量

时间:2014-05-29 23:00:32

标签: java arrays vector

我创建了一个字符串向量,我想将所选类型的咖啡存储在向量中,例如,如果我选择数字3,4,5和1,将它们存储在Vector1中的变量(“LATTE”,“AMERICAN”) ,“CAPUCCINO”和“MOKA”)并打印这些存储的名称,但问题是它只是打印,编号为6“BLACK”,总是。

 import java.io.*;

    class Exercise{

        public static void main (String[] args) throws java.io.IOException {
            DataInputStream receivedata= new DataInputStream(System.in);
            String keyboard; 
                int seleccafe,n=0,contvector=0;
                String []Vector1=new String[200];

                            do{

                                 System.out.println("\n1. MOKA");
                                 System.out.println("2. EXPRESO");
                                 System.out.println("3. LATTE");
                                 System.out.println("4. AMERICAN");
                                 System.out.println("5. CAPUCCINO");
                                 System.out.println("6. BLACK");
                                 System.out.println("0. OUT");
                                 System.out.print("\nTYPE THE NUMBER: ");

                                 seleccafe=Read_Da("");
                                 SelectCo(Vector1,seleccafe,n);

                                 contvector++;
                                 n++;
                                 if(seleccafe==0) break;

                            }while(true);

                                for(int i=0; i<contvector-1; i++){
                                System.out.println(Vector1[i]);
                                }

        }
            public static int  Read_Da (String TxtMsg) throws java.io.IOException{
                int X=0;
                String keyboard;
                DataInputStream reveivedata= new DataInputStream(System.in);
                do{
                    System.out.print("");
                    keyboard=reveivedata.readLine();
                    try{
                        X=Integer.parseInt(keyboard);
                        if(X<0){
                            System.out.println("SELECT JUST 0+");
                            continue;
                        }
                        return X; 
                    }catch(NumberFormatException e){
                        System.out.println("JUST NUMBERS");         
                    }
                }while(true);
        }

        public static void SelectCo(String xVector[], int xseleccafe, int xi){

            switch(xseleccafe){
                case 1:
                    xVector[xi]= "MOKA";
                case 2: 
                    xVector[xi]="EXPRESSO";
                case 3: 
                    xVector[xi]="LATE";
                case 4: 
                    xVector[xi]="AMERICAN ";
                case 5:
                    xVector[xi]="CAPUCCINO"; 
                case 6: 
                    xVector[xi]="BLACK";
            }
        }

    }

有任何帮助吗?谢谢!

1 个答案:

答案 0 :(得分:0)

您的问题是,在break;中的每个case条款之后,您遗漏了SelectCo这个词,因此每个case都是&#34;通过&#34;到下面的那个,并以case 6:结束。

应该是这样的。

      switch(xseleccafe){
            case 1:
                xVector[xi]= "MOKA";
                break;
            case 2: 
                xVector[xi]="EXPRESSO";
                break;
            case 3: 
                xVector[xi]="LATE";
                break;
            case 4: 
                xVector[xi]="AMERICAN ";
                break;
            case 5:
                xVector[xi]="CAPUCCINO"; 
                break;
            case 6: 
                xVector[xi]="BLACK";
                break;
            default:
                xVector[xi]="UNKNOWN";
        }

更多细节here