我如何使扫描仪变量成为一个数组?

时间:2014-03-12 05:22:51

标签: java arrays

我怎么可能这样做?我尝试这样做,但它说param是字符串,它需要string[]

这是我的代码:

import java.util.Scanner;

public class Launch {

    public static String[] josh = {
        "Josh", "mcMc", "16", "grade 11"
    };
    public static void main(String[] args) {
        Scanner sc = new Scanner(System. in );
        System.out.print("Please enter the first name of the person you would love to know about : ");
        String hisName = sc.next();
        printSomeInfoAbout(hisName);
    }

    public static void printSomeInfoAbout(String[] name) {

        System.out.println("My name is " + name[0] + " " + name[1] + ". I am " + name[2] + " " +
            "Years old and I am currently in " +
            name[3] + ".");
    }
}

4 个答案:

答案 0 :(得分:3)

也许这就是你需要的:

String[] hisName=new String[4]; 
for(int i=0;i<hisName.length;i++){
    hisName[i]=sc.nextLine();
}
printSomeInfoAbout(hisName);

答案 1 :(得分:0)

这将得到你想要的东西:

public class Launch {
      public static String[] josh = {
            "Josh", "mcMc", "16", "grade 11"
        };
        public static void main(String[] args) {
            Scanner sc = new Scanner(System. in );
            System.out.print("Please enter the first name of the person you would love to know about : ");
            String hisName = sc.next();
            printSomeInfoAbout(hisName);
        }

        public static void printSomeInfoAbout(String name) {
            if(name.equals(josh[0])){
                System.out.println("My name is " + josh[0] + " " + josh[1] + ". I am " + josh[2] + " " +
                "Years old and I am currently in " +
                josh[3] + ".");
            }

        }
    }

答案 2 :(得分:0)

如果用名字表示用户输入&#34; josh&#34;,则按照以下方式实施printSomeInfoAbout()方法。

public static void printSomeInfoAbout(String name){
    if(name.equals(josh[0])){
        System.out.println("My name is " + josh[0] + " " + josh[1] + ". I am " + josh[2] + " " +
        "Years old and I am currently in " +
        josh[3] + ".");
    }
}

答案 3 :(得分:0)

public class Launch {

    public static String[] josh = {
        "Josh", "mcMc", "16", "grade 11"
    };
    public static void main(String[] args) {
        Scanner sc = new Scanner(System. in );
        System.out.print("Please enter the first name of the person you would love to know about : ");
        String hisName = sc.next();
        if(josh[0].toString().equalsIgnoreCase(hisName)){
             printSomeInfoAbout(josh);
        }
        else{
            System.out.println("Sorry unable to find search Name");
        }

    }

    public static void printSomeInfoAbout(String[] name) {

        System.out.println("My name is " + name[0] + " " + name[1] + ". I am " + name[2] + " " +
            "Years old and I am currently in " +
            name[3] + ".");
    }
}