This commit is contained in:
121
src/main/java/com/mycompany/roleta_desafios/Roleta_Desafios.java
Normal file
121
src/main/java/com/mycompany/roleta_desafios/Roleta_Desafios.java
Normal file
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
*/
|
||||
|
||||
package com.mycompany.roleta_desafios;
|
||||
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.Random;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 250407
|
||||
*/
|
||||
public class Roleta_Desafios {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
String[] desafios = new String[10];
|
||||
int total = lerFicheiro(desafios);
|
||||
int opcao = -1;
|
||||
|
||||
while(opcao != 0){
|
||||
System.out.println("|===Roleta de Desafios===|");
|
||||
System.out.println("1 - girar roleta");
|
||||
System.out.println("0 - sair");
|
||||
System.out.println("|========================|");
|
||||
opcao = scanner.nextInt();
|
||||
|
||||
switch(opcao){
|
||||
case 1:
|
||||
total = girar(desafios, total);
|
||||
break;
|
||||
case 2:
|
||||
scanner.nextLine();
|
||||
System.out.println("escreve o desafio");
|
||||
String novo = scanner.nextLine();
|
||||
guardarDesafio(novo);
|
||||
System.out.println("desafio guardado");
|
||||
total = lerFicheiro(desafios);
|
||||
break;
|
||||
case 0:
|
||||
System.out.println("Fim!");
|
||||
break;
|
||||
default:
|
||||
System.out.println("opcao invalida");
|
||||
}
|
||||
|
||||
}while(opcao != 0);
|
||||
}
|
||||
|
||||
public static int lerFicheiro(String[] desafios){
|
||||
int i = 0;
|
||||
|
||||
try {
|
||||
Scanner file = new Scanner(new File("Desafios.txt"));
|
||||
while(file.hasNextLine() && i < desafios.length){
|
||||
desafios[i] = file.nextLine();
|
||||
i++;
|
||||
}
|
||||
|
||||
}catch(Exception e) {
|
||||
System.out.println("erro ao ler ficheiro: " + e.getMessage());
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
public static void guardarDesafio(String desafio) throws IOException{
|
||||
try{
|
||||
FileWriter fileWriter = new FileWriter("Desafios.txt", true);
|
||||
fileWriter.write(desafio);
|
||||
}catch (IOException e){
|
||||
System.out.println("Erro ao escrever" + e.getMessage());
|
||||
}
|
||||
}
|
||||
public static int girar(String[] desafios, int total){
|
||||
if (total == 0){
|
||||
System.out.println("sem deafios disponiveis!");
|
||||
return total;
|
||||
}
|
||||
Random random = new Random();
|
||||
System.out.println("A girar a Roleta...");
|
||||
int delay = 50;
|
||||
|
||||
try{
|
||||
for(int i = 0; i < 20; i++){
|
||||
int indice = random .nextInt(total);
|
||||
|
||||
System.out.println("\rDesafio: " + desafios[indice]);
|
||||
Thread.sleep(delay);
|
||||
delay += 30;
|
||||
}
|
||||
int finalIndex = random.nextInt(total);
|
||||
System.out.println(">>>>Desafio Final:<<<< " + desafios[finalIndex]);
|
||||
|
||||
for(int i = finalIndex; i < total - 1; i++){
|
||||
desafios[i] = desafios[i + 1];
|
||||
}
|
||||
total--;
|
||||
|
||||
}catch (InterruptedException e){
|
||||
System.out.println("erro na animacao.");
|
||||
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user