pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

Java pastebin - collaborative debugging tool View Help


Posted by kenny on Sun 4 May 10:45
report abuse | download | new post

  1.  
  2. public class Ziegenproblem {
  3.        
  4.         static boolean wechsel = false;
  5.        
  6.         public static int zufall() {
  7.                 // Die Zufallsfunktion soll die Zahlen von 0 bis 2 gleichverteilt zurückgegeben
  8.                 int z = (int) (Math.random() * 3000);
  9.                 if (z <= 1000) return 0;
  10.                 if (z <= 2000) return 1;
  11.                 if (z <= 3000) return 2;
  12.                 return 0;
  13.         }
  14.        
  15.         public static long runTest(long intHaufigkeit) {
  16.                 int richtigesTor        = 0;
  17.                 int ersteWahl           = 0;
  18.                 int offenesTor          = 0;
  19.                 int zweiteWahl          = 0;
  20.                 int anzAutos            = 0;
  21.                 for (int i=0; i <= intHaufigkeit; i++) {
  22.                         // Der Spielleiter stellt hinter eines der Tore ein Auto
  23.                         richtigesTor    = zufall();
  24.                        
  25.                         // Der Spieler wählt eines der Tore aus
  26.                         ersteWahl               = zufall();
  27.                        
  28.                         // Der Spielleiter öffnet ein Tor mit einer Ziege, nicht aber das was der Spieler gewählt hat
  29.                         offenesTor              = ersteWahl;
  30.                         for (int j=0; j < 3;j++) {
  31.                                 if ((j!=ersteWahl) && (j!=richtigesTor)) offenesTor = j;
  32.                         }
  33.                        
  34.                         // Der Spielleiter bietet dem Spieler an nun noch einmal das Tor zu wechseln
  35.                         if (wechsel) {
  36.                                 // Der Spieler entscheidet sich dafür
  37.                                 zweiteWahl = ersteWahl;
  38.                                 for (int j=0; j < 3;j++) {
  39.                                         if ((j!=ersteWahl) && (j!=offenesTor)) zweiteWahl = j;
  40.                                 }
  41.                         }
  42.                         else {
  43.                                 // Der Spieler bleibt bei seiner Entscheidung
  44.                                 zweiteWahl = ersteWahl;
  45.                         }
  46.                        
  47.                         // Auswertung
  48.                         if (zweiteWahl==richtigesTor) anzAutos++;
  49.                 }
  50.                
  51.                 return anzAutos;
  52.         }
  53.        
  54.         public static void main(String[] args) {               
  55.                
  56.                 wechsel                         = false;
  57.                 long ohneWechsel                = runTest(100000);
  58.                
  59.                 wechsel                         = true;
  60.                 long mitWechsel                 = runTest(100000);             
  61.                
  62.                 System.out.println("Häufigkeit ohne Wechsel:\t" +  ohneWechsel);
  63.                 System.out.println("Häufigkeit  mit Wechsel:\t" +  mitWechsel);
  64.                
  65.         }
  66. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post