2016-11-18 34 views
-1
//Author: Asim 
//Date: 10th November 2016 
//Description: This program is used to ask the user what theyre favourite music genre is. The user receives a score which count towards a team score. 

    import java.util.Scanner; //Importing the scanner 
    import java.util.Random; //Importing the dice 

    public class MiniProject6{ 

//Main method 

     public static void main(String[]p){ 
     for(int counter=1; counter<=4; counter++){ 
     Info i1 = new Info(); 
      i1 = setQuestion(i1, "What is the best music genre?"); //Here are the vales which are being set for Question and CorrectAnswer 
      System.out.print(getQuestion(i1)); 
      i1 = setCorrectanswer(i1, "Drill"); 

     String ans = answer(); 
     int [] pScore = new int[2]; 
     boolean finish; 
     finish = checker(ans, getCorrectanswer(i1), pScore); //Cross references the users input with the CorrectAnswer 
     } 
    } 
//End main 

//Answer method returns the value the user types 

    public static String answer() 
    { 
     Scanner asim = new Scanner(System.in); 
     String music; 
     music = asim.nextLine(); 

     return music; 
    } 
//End answer 
//Checker method is used to see if the users answer is the same as the record correct answer  

     public static boolean checker(String ans, String Correctanswer, int pScore[]){  

     boolean finish; 
     if (ans.equals(Correctanswer)){ 
      print("Congrats, " + Correctanswer + " is the correct answer!");   
        finish = true; 
      if (finish == true){ 
       int score = dice(); 

        print("The score is " + score); 
       if (score<6){    
        print("Lets add another 3 onto the score. "); 
        pScore[0] = score+3; 
         print("Total score is: " + pScore[0]); 
         pScore[1] = pScore[1] + pScore[0]; 
       } 
       else if (score>5){ 
        print("Lets add another 6 onto the score. "); 
        pScore[0] = score+6; 
         print("Total score is: " + pScore[0]); 
         pScore[1] = pScore[1] + pScore[0]; 
       } 
      }  
         System.out.println(finish); 
         System.out.println("Currently the teamscore is " + pScore[1]); 

     } 
      else{ 
       print("The correct answer is " + Correctanswer + ", unfortunately you got it wrong"); 
       finish = false;  
       System.out.println(finish); 
      } 
      return finish;   
    } 
//End Checker 

//Dice method is used to create the dice 

    public static int dice(){  
     Random Dice = new Random(); 
     int answer = Dice.nextInt(6) + 1; 

     return answer; 
    } 
//End dice 
+0

使用IDE調試代碼 – SkrewEverything

回答

0

如果您希望彙總分數,您需要檢查分數變量的範圍/創建一個未包含在for循環中的分數變量。如果變量在for循環中初始化,那麼每次迭代都會重置該變量。只需將循環外部的變量實例化並在循環內引用即可。然後你的分數不會被重置。

+0

如何在沒有全局變量的情況下執行此操作? – asdasd

+0

你現在這樣做的方式並不是一個很好的方法。根據我的理解,您正在創建一個2 int值的數組,並將它們一起添加到您的檢查方法中。但是,這很簡單,只需將'int [] pScore = new int [2];'移出for循環並在主方法之外。 – Jay

+0

因爲通過每次迭代你正在做的是創建一個全新的Array!這會擦除舊陣列中的任何值,這意味着您沒有保存的信息。同樣,最好使用一個全局變量(也許稱之爲「全部」),然後通過添加它來彙總你的分數,但代碼組織取決於你。 – Jay