Und hier eine Roh-Version für Beanshell (ich verwende jEdit).
Natürlich ohne Lösungsweg.
Code java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Zuerst ein paar Hilfsklassen und Methoden definieren
void vrPrint(String str)
{
    textArea.setSelectedText(str+"\n");
}
class Ei
{
    public String name;
    public int gramm;
    public int kcal;
    public Ei () {}
    public Ei(String name, int gramm, int kcal)
    {
        this.name = name;
        this.gramm = gramm;
        this.kcal = kcal;
    }
    public String toString()
    {
        str = new String(name);
        str += ": "+gramm+" Gramm";
        str += ", "+kcal +" kcal" ;
        return str;
    }
}
class Ostersuche
{
    private int tragkraft;
    private Ei[] nest;
    private Ei[] korb;
    public static void erstelleNest(int cntEier, int tragkraft
    , int kcalMin, int kcalMax
    , int gMin, int gMax)
    {
        int g; // Masse in Gramm
        int c; // kcal
        int gDiff =    gMax -    gMin;
        int cDiff = kcalMax - kcalMin;
        vrPrint(""+tragkraft);
        for ( i = 0; i < cntEier; i++ )
        {
            vrPrint("Ei "+i);
            g =    gMin+(int)Math.rint(Math.random()*gDiff);
            c = kcalMin+(int)Math.rint(Math.random()*cDiff);
            vrPrint(g+" "+c);
        }
    }
    public static void erstelleNest()
    {
        int cntEier = 500;
        int tragkraft = 500;
        int kcalMin = 50;
        int kcalMax = 1500;
        int gMin = 50;
        int gMax = 300;
        Ostersuche.erstelleNest(cntEier, tragkraft
        , kcalMin, kcalMax
        , gMin, gMax);
    }
    Ostersuche()
    {
        tragkraft = 0;
        nest = new Ei[0];
        korb = new Ei[0];
    }
    public void einlesen()
    {
        String   str;
        String[] tkn;
        BufferedReader br 
        = new BufferedReader(new StringReader(textArea.getText()));
        buf = new LinkedList(); 
        str = br.readLine();
        tragkraft = Integer.valueOf(str);
        int cnt; 
        for ( cnt = 0; (str=br.readLine()) != null; cnt++ )
        {
            str = str.trim();
            n = str;
            str = br.readLine();
            if ( str == null ) break;
            tkn = str.split("\\s+");
            if ( tkn.length < 2 ) break;
            g = Integer.valueOf(tkn[0]);
            c = Integer.valueOf(tkn[1]);
            buf.add(new Ei(n,g,c));
        }
        nest = new Ei[cnt];
        cnt = 0;
        for ( ei : buf )
        { nest[cnt++] = (Ei)ei; }
    }
    private void ausgeben(Ei[] e)
    {
        int cCnt = 0;
        int gCnt = 0;
        for ( ei : e ) 
        {
            vrPrint(ei.toString());
            cCnt += ei.kcal;
            gCnt += ei.gramm;
        }
        str = "Summe";
        str += ": "+gCnt+" Gramm";
        str += ", "+cCnt+" kcal" ;
        vrPrint(str);
    }
    public void nestAusgeben()
    {
        ausgeben(nest);
    }
    public void korbAusgeben()
    {
        ausgeben(korb);
    }
    public void fuelleKorb()
    {
        // das verrate ich euch nicht ;)
        return;
    }
}
 
// Ab hier beginnt die eigentliche Verarbeitung
ret = Macros.confirm(view
, "Soll eine neue Datei angelegt werden?"
, JOptionPane.YES_NO_CANCEL_OPTION);
if ( ret == JOptionPane.CANCEL_OPTION ) return;
if ( ret == JOptionPane.YES_OPTION    ) jEdit.newFile(view);
Ostersuche.erstelleNest();
suche = new Ostersuche();
suche.einlesen();
jEdit.newFile(view);
suche.nestAusgeben();