14.12.2014, 15:59 |
Auf diesen Beitrag antworten » |
gg_ |
Strings vergleichen
Meine Frage:
Ich hab ein Java-Programm entwickelt, das eine Liste von Agentennamen der Form "Agent 001", "Agent 002" usw. bis "Agent 009" in einer for-Schleife durchläuft.
Als Erweiterung habe ich an den Namen jedes 4. Agenten den Zusatz "enttarnt" an, also "Agent 004 enttarnt", "Agent 008 enttarnt", "Agent 012 enttarnt", usw. hinzugefügt. die zweite Erweiterung ist, sobald der String "Agent 007" geschrieben wird, wird der Zusatz "James Bond" mit ausgegeben, das habe ich irgendwie nicht geschafft, ich weiß das ich die Methode equals benutzen muss.
Meine Ideen:
Meine Lösung:
code: |
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:
|
public static void main(String[] args) {
String pattern="000"; //um Agent001 einzustellen
DecimalFormat form = new DecimalFormat(pattern);
for(int i= 1; i<1000; i++)
{
System.out.println();
System.out.print("Agent"+form.format(i));
if(i%4 == 0)
{
System.out.print("\t entarnt");
}
}
}
|
|
Danke im vorraus |
14.12.2014, 16:04 |
Auf diesen Beitrag antworten » |
gg_ |
ich habe die Lösung
code: |
1:
2:
3:
4:
5:
6:
7:
8:
9:
|
if("Agent007".equals("Agent"+form.format(i)))
{
System.out.print("\t James Bond");
}
|
|
|
14.12.2014, 16:14 |
Auf diesen Beitrag antworten » |
gg_ |
Meine komplette Lösung:
code: |
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:
|
public class agent {
public static void main(String[] args) {
String pattern="000"; //um Agent001 einzustellen
DecimalFormat form = new DecimalFormat(pattern);
for(int i= 1; i<1000; i++)
{
System.out.println();
System.out.print("Agent"+form.format(i));
if("Agent007".equals("Agent"+form.format(i)))
{
System.out.print("\t James Bond");
}
if(i%4 == 0)
{
System.out.print("\t entarnt");
}
}
}
}
|
|
|