I'm doing a programming task which asks the user some questions, if the user gets the answer wrong/right it doesn't matter, ad a random number generator is used to determine their points for that question. However if they choose the 'impossible answer' (something that it couldn't possibly be, their score is reset to 0.
When i run the code,
If i get 23 points on the first questions and 0 on the second, it should reset my score to 0 on the second question, but at the end the total score shows as 23.
Any help would be appreciated!! :)
import java.util.Scanner;
public class Main {
public static double randnum(){
int random = (int)(Math.random() * 50 + 1);
return random;
}
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
int score1 = 0;
int score2 = 0;
int score3 = 0;
int totalscore = 0;
final double NumberofQuestions = 2;
String[][] questions ={
{"What is the largest bone in the human body? ", "\n Choose 1 for Femur \n Choose 2 for Tibia \n Choose 3 for Palatine Bone \n Choose 4 for Tongue ", "1"},
{"What is the capital of Albania? ! ","\n Choose 1 for Shkoder \n Choose 2 for Tirana \n Choose 3 for Durres \n Choose 4 for Rome ", "2"}
};
String[] Answers = new String[(int) NumberofQuestions];
int x=0;
do
{
System.out.print("" + (x+1) + ". " + questions[x][0] + " "+questions[x][1]);
Answers[x] = String.valueOf(input.nextInt());
Answers[x].toLowerCase();
if(questions[x][2].equals(Answers[x])) {
score1 = (int) randnum();
System.out.println("Correct: " + score1 + " points");
totalscore = totalscore + score1;
}
if (Answers[x].equals("4")){
System.out.println("\n Thats a impossible answer! The right answer is "+questions[x][2]);
totalscore = 0;
score2 = 0;
System.out.println(score2 + " points");
}
else{
System.out.println("\nIncorrect. The right answer is "+questions[x][2]);
score3 = (int) randnum();
System.out.println(score3 + " points");
totalscore = totalscore + score3;
}
System.out.print("\n");
x++;
}
while(x<NumberofQuestions);//close outer loop
totalscore = score1 +score2 +score3;
System.out.println("\n\t\tYou got " + totalscore + " points !\n\n\n");
System.exit(0);
}
}
Aucun commentaire:
Enregistrer un commentaire