簡體   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