This is Peders version of the quiz program for studying. it takes textfiles with the extention .pugg as input
import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Pugge { public static void main(String[] args) throws IOException { Map> wordLists = new HashMap>(); File f = new File("."); for(String filen : f.getCanonicalFile().list()) { if(filen.endsWith(".pugg")) wordLists.put(filen, getWords(filen)); } System.out.println("Velkommen! Hva vil du pugge?\n"); int tell = 1; for(String s : wordLists.keySet()) { System.out.println(tell++ + ". " + s.substring(0, s.length()-5)); } Scanner inn = new Scanner(System.in); System.out.print("\nSkriv inn valg: "); String tema = (String) wordLists.keySet().toArray()[Integer.parseInt(inn.nextLine())-1]; String temaEase = toProperCase(tema.substring(0, tema.length()-5)); Map ord = wordLists.get(tema); System.out.println("Da skal vi pugge " + temaEase + ". Vi har " + ord.size() + " ord i listen vår!\n"); int hints = 0; int forsok = 0; long startTid = System.currentTimeMillis(); while(true) { System.out.println("Ord du har klart til nå:"); // Skriver hvilke ord du har fått til int klart = 0; for (String ordet : ord.keySet()) if(ord.get(ordet)) { System.out.println(" - " + toProperCase(ordet)); klart ++; } // Skriver ut status if(klart == ord.size()) { System.out.println("\nGrattis! Det gikk fint. \"Bare\" " + hints + " hint og " + forsok + " forsøk i løpet av " + (int)((System.currentTimeMillis() - startTid)/1000) + " sekunder."); break; } else System.out.println("\nDu har klart " + klart + " av " + ord.size() + "\n"); // input System.out.print("$ "); String linje = inn.nextLine().toLowerCase(); // Om det er en kommando if(linje.equals("/h")) { for (String ordet : ord.keySet()) { if(!ord.get(ordet)) { int start = (int)(Math.random()*(ordet.length()-1)); int stopp = (int)(Math.random()*(ordet.length()-start)) + start+1; System.out.println(ordet.substring(start, stopp)); hints++; break; } } continue; } else if(linje.equals("/q")) break; // Riktig eller galt if(ord.get(linje) != null) ord.put(linje, true); else System.out.println("Fant ikke den..."); forsok++; } } private static String toProperCase(String inputString) { StringBuilder ff = new StringBuilder(); for(String f: inputString.split(" ")) { if(ff.length()>0) ff.append(" "); ff.append(f.substring(0,1).toUpperCase()).append(f.substring(1,f.length()).toLowerCase()); } return ff.toString(); } private static HashMap getWords(String filename) throws FileNotFoundException { Scanner s = new Scanner(new File(filename)); Map ord = new HashMap(); // Leser inn ord. Et per linje while(s.hasNextLine()) { ord.put(s.nextLine().toLowerCase().trim(), false); } return (HashMap) ord; } }
Related posts:
Your email is never published nor shared. Required fields are marked *
You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Simple quiz-program written in Java
This is Peders version of the quiz program for studying. it takes textfiles with the extention .pugg as input
import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Pugge { public static void main(String[] args) throws IOException { Map> wordLists = new HashMap>();
File f = new File(".");
for(String filen : f.getCanonicalFile().list()) {
if(filen.endsWith(".pugg"))
wordLists.put(filen, getWords(filen));
}
System.out.println("Velkommen! Hva vil du pugge?\n");
int tell = 1;
for(String s : wordLists.keySet()) {
System.out.println(tell++ + ". " + s.substring(0, s.length()-5));
}
Scanner inn = new Scanner(System.in);
System.out.print("\nSkriv inn valg: ");
String tema = (String) wordLists.keySet().toArray()[Integer.parseInt(inn.nextLine())-1];
String temaEase = toProperCase(tema.substring(0, tema.length()-5));
Map ord = wordLists.get(tema);
System.out.println("Da skal vi pugge " + temaEase + ". Vi har " + ord.size() + " ord i listen vår!\n");
int hints = 0;
int forsok = 0;
long startTid = System.currentTimeMillis();
while(true) {
System.out.println("Ord du har klart til nå:");
// Skriver hvilke ord du har fått til
int klart = 0;
for (String ordet : ord.keySet())
if(ord.get(ordet)) {
System.out.println(" - " + toProperCase(ordet));
klart ++;
}
// Skriver ut status
if(klart == ord.size()) {
System.out.println("\nGrattis! Det gikk fint. \"Bare\" " + hints + " hint og " +
forsok + " forsøk i løpet av " + (int)((System.currentTimeMillis() - startTid)/1000) +
" sekunder.");
break;
}
else
System.out.println("\nDu har klart " + klart + " av " + ord.size() + "\n");
// input
System.out.print("$ ");
String linje = inn.nextLine().toLowerCase();
// Om det er en kommando
if(linje.equals("/h")) {
for (String ordet : ord.keySet()) {
if(!ord.get(ordet)) {
int start = (int)(Math.random()*(ordet.length()-1));
int stopp = (int)(Math.random()*(ordet.length()-start)) + start+1;
System.out.println(ordet.substring(start, stopp));
hints++;
break;
}
}
continue;
}
else if(linje.equals("/q"))
break;
// Riktig eller galt
if(ord.get(linje) != null)
ord.put(linje, true);
else
System.out.println("Fant ikke den...");
forsok++;
}
}
private static String toProperCase(String inputString) {
StringBuilder ff = new StringBuilder();
for(String f: inputString.split(" ")) {
if(ff.length()>0)
ff.append(" ");
ff.append(f.substring(0,1).toUpperCase()).append(f.substring(1,f.length()).toLowerCase());
}
return ff.toString();
}
private static HashMap getWords(String filename) throws FileNotFoundException {
Scanner s = new Scanner(new File(filename));
Map ord = new HashMap();
// Leser inn ord. Et per linje
while(s.hasNextLine()) {
ord.put(s.nextLine().toLowerCase().trim(), false);
}
return (HashMap) ord;
}
}
Related posts: