为什么我的用户输入在main方法中返回null?

时间:2016-10-12 11:04:38

标签: java class variables methods null

我是Java新手,这就要让我把头发撕掉了。请注意我添加了一个打印声明,以确保扫描仪工作正常。

package dknowlton_program5;

import java.util.Scanner;

//Driver ~ Gathers Input

public class DKnowlton_MartianMaker {

static Scanner userInput = new Scanner(System.in);

public static String name;
public static int eyes, arms;

public static void main(String[] String)
{

    printWelcome();
    getName();
    getEyes();
    getArms();

    System.out.print("Your martian " + name + " has... ");

}

public static void printWelcome()
{

    System.out.print("Welcome to the Martian Program Revamped!");

}

public static String getName()
{

    System.out.print("\nWhat would you like to name your martian? ");
    String name = userInput.nextLine();

    System.out.println(name);

    return name;

}

public static int getEyes()
{

    System.out.print("How many eyes does your martian have? ");
    int eyes = userInput.nextInt();

    return eyes;

}

public static int getArms()
{

    System.out.print("How many arms does your martian have? ");
    int arms = userInput.nextInt();

    return arms;

}

}

3 个答案:

答案 0 :(得分:1)

以下内容应该有效。请注意,顶部的静态变量已被删除,现在我们保存从函数返回的结果。 我建议读一下java中的范围,一般来说最好有尽可能低的范围,避免全局变量。

200

答案 1 :(得分:1)

而不是3 你不应该使用x array([['wisD', 'netl', 'stu', '', 'entl'], ['maat', 'wisD', 'stu', '', ''], ['lo', 'nat', 'stu', 'schk', 'maat'], ['nat', 'fatl', 'stu', 'wisB', 'netl'], ['fatl', 'wisB', 'stu', 'wisB', 'ges'], ['schk', 'entl', 'stu', 'ges', ''], ['wisB', '', 'stu', 'nat', 'AK'], ['AK', '', '', '', 'AK'], ['', '', '', '', '']], dtype='<U4')

希望它有效。

编辑:为什么要两次声明String名称?

第二次编辑:试试这个

 np.savetxt("test.csv", x, delimiter=",")
Traceback (most recent call last):
  File "C:\Users\Michiel\AppData\Local\Programs\Python\Python35\lib\site-packages\numpy\lib\npyio.py", line 1158, in savetxt
fh.write(asbytes(format % tuple(row) + newline))
TypeError: a float is required

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<pyshell#19>", line 1, in <module>
    np.savetxt("test.csv", x, delimiter=",")
  File "C:\Users\Username\AppData\Local\Programs\Python\Python35\lib\site-packages\numpy\lib\npyio.py", line 1162, in savetxt
% (str(X.dtype), format))
TypeError: Mismatch between array dtype ('<U4') and format specifier ('%.18e,%.18e,%.18e,%.18e,%.18e')

}

答案 2 :(得分:0)

这个“名字”:

public static String name; 

与此“名称”不同:

String name = userInput.nextLine();
  • 您将值分配给第二个,并打印第一个,这就是您获得null的原因。

  • 您只需删除字符串并使用您在开头(第一个)定义的相同“名称”:

    package dknowlton_program5;
    import java.util.Scanner;
    
    //Driver ~ Gathers Input
    
    public class DKnowlton_MartianMaker {
    
    static Scanner userInput = new Scanner(System.in);
    
    public static String name;
    public static int eyes, arms;
    
    public static void main(String[] String)
    {
    
        printWelcome();
        getName();
        getEyes();
        getArms();
    
        System.out.print("Your martian " + name + " has... ");
    
    }
    
    public static void printWelcome()
    {
    
        System.out.print("Welcome to the Martian Program Revamped!");
    
    }
    
    public static String getName()
    {
    
        System.out.print("\nWhat would you like to name your martian? ");
        name = userInput.nextLine();
    
        System.out.println(name);
    
        return name;
    
    }
    
    public static int getEyes()
    {
    
        System.out.print("How many eyes does your martian have? ");
        int eyes = userInput.nextInt();
    
        return eyes;
    
    }
    
    public static int getArms()
    {
    
        System.out.print("How many arms does your martian have? ");
        int arms = userInput.nextInt();
    
        return arms;
    
    }
    
    }