如何在每次循环后停止重置团队分数?

时间:2016-11-18 18:41:13

标签: java loops

 //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

1 个答案:

答案 0 :(得分:0)

如果您想要汇总您的分数,您需要检查分数变量的范围/创建一个未包含在for循环中的分数变量。如果变量在for循环中初始化,则每次迭代都将重置变量。只需将变量实例化移到循环外部并在循环内引用它。那么你的分数将不会被重置。