繁体   English   中英

我正在发生资源泄漏

[英]I'm getting Resource Leaks

每当我运行此代码时,它都会非常平稳地运行,直到while循环运行一次。 它将返回并再次询问名称,然后跳过String b = sc.nextLine();,并打印下一行。

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

static public void main(String [] argv) {
    Name();
}

static public void Name() {

boolean again = false;
do
{
    System.out.println("What is your name?");

    String b = sc.nextLine();
    System.out.println("Ah, so your name is " + b +"?\n" +
            "(y//n)");
    int a = getYN();
    System.out.println(a + "! Good.");
    again = askQuestion();
} while(again);



}

static public boolean askQuestion() {
    System.out.println("Do you want to try again?");
    int answer = sc.nextInt();

    if (answer == 1) {
        return true;
    }
    else {
        return false;
    }

}

static int getYN() {
    switch (sc.nextLine().substring(0, 1).toLowerCase()) {
    case "y":
        return 1;
    case "n":
        return 0;
    default:
        return 2;
    }
}

}

另外,我正在尝试以一种可以问三个问题(例如某人的姓名,性别和年龄,也许更像是种族和诸如此类)的方式创建该程序,然后将所有这些答案都带回来。 就像在最后说,“因此,您的名字是+名字+,您是+性别+,您是+年龄+岁?是/否。” 遵循这些原则。 我知道有一种方法可以执行此操作,但是我不知道如何将这些响应保存到任何地方,并且我无法抓住它们,因为它们仅在方法实例中出现。

不要尝试在同一扫描仪上使用nextInt()之后再使用nextLine()扫描文本! 可能会引起问题。 建议仅对int打开扫描仪方法。 您总是可以解析扫描仪的字符串答案。

同样,以这种方式使用扫描仪也不是一个好习惯,您可以将问题组织成数组,然后选择循环读取以获取独特的扫描仪实例,如下所示:

public class a {

    private static String InputName; 
    private static String Sex; 
    private static String Age; 
    private static String input; 
    static Scanner sc  ; 

    static public void main(String [] argv) {
        Name();
    } 

    static public void Name() {

      sc =  new Scanner(System.in);

       String[] questions = {"Name?","Age","Sex?"};//
       int a = 0; 
       System.out.println(questions[a]); 

      while (sc.hasNext()) {
          input = sc.next();
           setVariable(a, input);
            if(input.equalsIgnoreCase("no")){
                sc.close();
                break;
            }
            else if(a>questions.length -1)
            {
                a = 0;
            }
            else{
                a++;
            }
            if(a>questions.length -1){
                System.out.println("Fine " + InputName 
                        + " so you are " + Age + " years old and " + Sex + "." );
                Age = null;
                Sex = null;
                InputName = null;
                 System.out.println("Loop again?"); 

              }
               if(!input.equalsIgnoreCase("no") && a<questions.length){
              System.out.println(questions[a]);
              } 
        } 

    }


    static void setVariable(int a, String Field) {
        switch (a) {
        case 0:
            InputName = Field;
            return;
        case 1:
            Age = Field;
            return;
        case 2:
            Sex = Field; 
            return;
        }
    }
}

请注意全局变量,它会存储您的信息,直到将其设置为null或为空为止为止...您可以将其用于最终确认。

希望这可以帮助! 希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM