2017-04-01 17 views
-1

如果玩家輸入某個名稱,則會調用並執行測試程序。執行代碼時變量不會增加

可變testerWrong犯規添加一個當testerWrong ++執行

private void Tester(){ 

    int testerTotal; 
    int testerScore; 
    int testerWrong; 
    testerTotal = 0; 
    testerScore = 0; 
    testerWrong = 0; 

    System.out.println(""); 
    System.out.println("Hello tester, you're the designated tester. Would you like to take the quiz? Y/N"); 
    Scanner yesno = new Scanner(System.in); 
    String YesNo = yesno.next(); 
    if(YesNo.equals("Y") || YesNo.equals("y")){     //This type of code will appear very often 
     System.out.println("Okay, let's being!");  //if the user input (YesNo) is Y or y then... 
    }else{ 
     if(YesNo.equals("N") || YesNo.equals("n")){ 
      System.out.println("Okay, maybe some other time"); 
     }else{             //else... 
      System.out.println("Sorry, i do not recognise what you entered. Please restart the program."); 
     } 
    } 
    System.out.println(""); 
    QUIZ enter = new QUIZ(); 
    enter.e2c(); 
    System.out.println(""); 
    System.out.println(""); 
    System.out.println(""); 
    System.out.println("Question #1"); 
    System.out.println(""); 
    System.out.println("The answer is A"); 
    System.out.println(""); 
    System.out.println(" - A. "); 
    System.out.println(" - B. "); 
    System.out.println(" - C. "); 
    System.out.println(" - D. "); 
    Scanner testerQ1 = new Scanner(System.in);  
    String TesterQ1 = testerQ1.next(); 
    if(TesterQ1.equals("A") || TesterQ1.equals("a")){ 
     testerScore++; 
     System.out.println("Correct! You have answered " + testerScore + " correct and " + testerWrong + " incorrect!"); 
     System.out.println(""); 
     System.out.println("Next Question."); 
     System.out.println(""); 
    }else{ 
     testerWrong++; 
     System.out.println("Incorrect! You have answered " + testerScore + " correct and " + testerWrong + " incorrect!"); 
     System.out.println(""); 
    } 

是否有一種方法,使所述可變執行,而不必在它之前添加系統輸出?

感謝

+2

請發表一個https://stackoverflow.com/help/mcve ---我們不知道你是如何初始化你的變量和其他事情。 – Robert

+0

只需指出,你可以省略'System.out.println(「」)'調用一個換行符。相反,只要繼續前面的字符串並添加'\ n'。 – XavCo7

+0

可能因爲這些都是局部變量 - 與您的掃描儀一起,這不會導致問題的結束。 – chrylis

回答

1

這不是一個最小(太多的打印語句),甚至完整的例子(不包括QUIZ類)。

縮小你的代碼減少到最低限度例如:

import java.util.Scanner; 

public class Tester { 
    public static void main(String args[]) { 
     int testerScore = 0; 
     int testerWrong = 0; 

     System.out.println("The answer is A"); 
     Scanner scanner = new Scanner(System.in); 
     String answer = scanner.next(); 
     if (answer.equals("A") || answer.equals("a")) { 
      testerScore++; 
      System.out.print("Correct!"); 
     } 
     else { 
      testerWrong++; 
      System.out.println("Incorrect! "); 
     } 
     System.out.println(" You hve answered " + testerScore + 
      " correct and " + testerWrong + " incorrect!"); 
    } 
} 

這對我的作品。比較你的代碼,看看你在做什麼不同。

如果以這種方式找不到問題,請在調試器中運行代碼。瀏覽程序,看看它在什麼時候做什麼。

您可能還想遵循Java命名約定(變量以小寫字母開頭,類以大寫字母開頭,但不是全部大寫字母),以便其他人閱讀您的代碼。

+0

謝謝你的迴應。所以最好是爲每個球員設置Classes而不是方法(即我有玩家1,玩家2等方法)並且調用類而不是方法?我應該刪除多個答案問題,只是檢查正確/不正確的答案,而不是顯示多個答案? – user7431342

+0

@ user7431342這是一個不同的問題,取決於你的實際程序以及你想要做什麼。 – Robert