Commit a3ea8d22 authored by Sepehr NAMDAR FARD's avatar Sepehr NAMDAR FARD

Exemples de Sepehr

parent dbc156c0
public class FizzBuzz {
public static String getFirstPlayerResponse(int number) {
StringBuilder response = new StringBuilder();
if (isModuloThree(number)) {
response.append("Fizz");
}
if (isModuloFive(number)) {
response.append("Buzz");
}
if (isEmpty(response)) {
response.append(number);
}
return response.toString();
}
public static String getSecondPlayerResponse(int number) {
if (isModuloThree(number)) {
if (isModuloFive(number)) {
return "FizzBuzz";
} else {
return "Fizz";
}
} else if (isModuloFive(number)) {
return "Buzz";
}
return String.valueOf(number);
}
public static String getThirdPlayerResponse(int number) {
if (isModuloThree(number) && isModuloFive(number)) {
return "FizzBuzz";
} else if (isModuloFive(number)) {
return "Buzz";
} else if (isModuloThree(number)) {
return "Fizz";
}
return String.valueOf(number);
}
private boolean isModuloThree(int number) {
return number % 3 == 0;
}
private boolean isModuloFive(int number) {
return number % 5 == 0;
}
private boolean isEmpty(String response) {
return response.length() == 0
}
}
\ No newline at end of file
public class Application {
public static String fizzBuzz1(int i) {
StringBuilder sb = new StringBuilder();
if (modulo3(i)) {
sb.append("Fizz");
}
if (i % 5 == 0) {
sb.append("Buzz");
}
if (sb.length() == 0) {
sb.append(i);
}
return sb.toString();
}
public static String fizzBuzz2(int i) {
if (i % 3 == 0) {
if (i % 5 == 0) {
return "FizzBuzz";
} else {
return "Fizz";
}
} else if (i % 5 == 0) {
return "Buzz";
}
return String.valueOf(i);
}
public static String fizzBuzz3(int i) {
if (i % 15 == 0) {
return "FizzBuzz";
} else if (i % 5 == 0) {
return "Buzz";
} else if (i % 3 == 0) {
return "Fizz";
}
return String.valueOf(i);
}
private boolean modulo3(int getNumber) {
return getNumber % 3 == 0;
}
}
public class ConsoleQuiz extends Quiz {
private int score;
private long timeElapsed;
private boolean isDone = false;
private int nbreQuestions;
Scanner clavier = new Scanner(System.in);
public ConsoleQuiz(int nbq) {
nbreQuestions = nbq;
}
public void start () {
try {
long startTime = System.currentTimeMillis();
for (Question question : generate(nbreQuestions)) {
System.out.println(question.getText());
String userAnswer = clavier.nextLine();
if (userAnswer.equalsIgnoreCase(question.getResponse())){
score++;
System.out.println("Bonne Reponse");
}else{
System.out.println("Mauvaise Reponse");
System.out.println("la bonne reponse etait: " +question.getResponse());
}
}
isDone = true;
long endTime = System.currentTimeMillis();
timeElapsed = endTime - startTime;
} catch(IllegalArgumentException e){
isDone = false;
System.out.println(e.getMessage());
}
}
private int getTimeElapsedInSeconds(long timeInMilliSeconds) {
// TODO Auto-generated method stub
return (int) (timeInMilliSeconds / 1000);
}
public void displayResultats(){
if(isDone){
displayScore();
displayTimeElapsed();
}
}
private void displayTimeElapsed() {
System.out.printf("Il vous a fallu environ %d secondes pour repondre aux questions", getTimeElapsedInSeconds(timeElapsed), nbreQuestions);
}
private void displayScore() {
System.out.printf("votre score final est de: %d/%d\n ", score, nbreQuestions);
}
public ArrayList<Question> generateQuestions(int nbreQuestions){
String[][] countriesAndTheirCapital = getCountriesAndTheirCapital();
int numberOfCountries = countriesAndTheirCapital.length;
if (nbreQuestions > numberOfCountries) {
throw new IllegalArgumentException("On ne peut generer que: " + numberOfCountries + " question maximun");
}
ArrayList<Question> questions = new ArrayList<Question>();
int index;
ArrayList<Integer> indexesAlreadyTaken = new ArrayList<Integer>();
indexesAlreadyTaken.clear();
for (int i = 0; i < nbreQuestions; i++){
do {
Random random = new Random();
index = random.nextInt(numberOfCountries);
} while (indexesAlreadyTaken.contains(index));
indexesAlreadyTaken.add(index);
String pays = countriesAndTheirCapital[index][0];
String capitale = countriesAndTheirCapital[index][1];
String q = String.format("Quelle est la capitale de ce pays: %s? ", pays);
questions.add(new Question(q, capitale));
}
return questions;
}
private static String[][] getCountriesAndTheirCapital(){
String[][] countriesAndTheirCapital = { {"Senegal", "Dakar"} , {"France", "Paris"},
{"Haiti", "Paup"} , {"Argemtine", "Brenos.A"}, {"Brasil", "brasilia"},
{"Itali", "Rome"} , {"Perou", "Lima"} , {"Almagne", "berlin"} , {"Canada", "Otawa"}};
return data;
}
}
\ No newline at end of file
public class ConsoleQuiz {
private int s;
private long timeElapsed;
private boolean done = false;
private int nbreQuestions;
Scanner clavier = new Scanner(System.in);
public ConsoleQuiz(int nbq) {
nbreQuestions = nbq;
}
public void start () {
try {
long st = System.currentTimeMillis();
for (Question question : generate(nbreQuestions)) {
System.out.println(question.getText());
String userAnswer = clavier.nextLine();
if (userAnswer.equalsIgnoreCase(question.getResponse())){
s++;
System.out.println("Bonne Reponse");
}else{
System.out.println("Mauvaise Reponse");
System.out.println("la bonne reponse etait: " +question.getResponse());
}
}
done = true;
long endTime = System.currentTimeMillis();
timeElapsed = endTime - st;
} catch(IllegalArgumentException e){
done = false;
System.out.println(e.getMessage());
}
}
private int getTime(long timeInMilliSeconds) {
// TODO Auto-generated method stub
return (int) (timeInMilliSeconds / 1000);
}
public void displayResultats(){
if(done){
displayScore();
displayTimeElapsed();
}
}
private void displayTimeElapsed() {
System.out.printf("Il vous a fallu environ %d secondes pour repondre aux questions", getTime(timeElapsed), nbreQuestions);
}
private void displayScore() {
System.out.printf("votre score final est de: %d/%d\n ", s, nbreQuestions);
}
public ArrayList<Question> generate(int nbreQuestions){
String[][] data = getData();
if (nbreQuestions > data.length) {
throw new IllegalArgumentException("On ne peut generer que: " + data.length + " question maximun");
}
ArrayList<Question> questions = new ArrayList<Question>();
int index;
ArrayList<Integer> indexesAlreadyTaken = new ArrayList<Integer>();
indexesAlreadyTaken.clear();
for (int i = 0; i < nbreQuestions; i++){
do {
Random random = new Random();
index = random.nextInt(data.length);
} while (indexesAlreadyTaken.contains(index));
indexesAlreadyTaken.add(index);
String pays = data[index][0];
String capitale = data[index][1];
String questionText = String.format("Quelle est la capitale de ce pays: %s? ", pays);
questions.add(new Question(questionText, capitale));
}
return questions;
}
private static String[][] getData(){
String[][] data = { {"Senegal", "Dakar"} , {"France", "Paris"},
{"Haiti", "Paup"} , {"Argemtine", "Brenos.A"}, {"Brasil", "brasilia"},
{"Itali", "Rome"} , {"Perou", "Lima"} , {"Almagne", "berlin"} , {"Canada", "Otawa"}};
return data;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment