我的程序是“战略游戏”,用户可以自行制作军队和化身。我的问题是,当用户不得不为自己的军队购买士兵时,我不知道如何使变量记住结果和用户可以买另一名士兵。我来自捷克共和国,所以在线上可以寻求帮助。
int zustatek = 0; //residue
int penize = 50;//money
ArrayList<Vojak> armada = new ArrayList<Vojak>();//army
do {
System.out.println("Jaké si vybereš?"); //What you choose
System.out.println("Pešák-1(10)"); //plebs
System.out.println("Lučištník-2 (15)"); //archer
System.out.println("Jezdectvo-3 (30)"); //cavalry
System.out.println("Težká jednotka-4 (50)"); //hevy soldier
int volbaVojaku = Integer.parseInt(sc.nextLine());//choose soldiers
Vojak vyberArmady = null;//picking soldiers
switch(volbaVojaku) {
case 1:
System.out.println("A kolik jich cheš?");//how many soldiers do you want?
int pocet = Integer.parseInt(sc.nextLine());//number of soldiers
for(int i =0; i < pocet ;i++) {
Zbran mec = new Zbran(0,0,0,10,"meč");//weapon
Vojak pesak = new Pesak(nazevJednotky,0,0,0,mec,vysledekBrneni);//soldier
vyberArmady = pesak;
armada.add(pesak);//army
System.out.println(pesak);
}
pocet *=10;
zustatek = penize - pocet;
System.out.println("Zůstalo ti: " + zustatek);//Result how many user have
break;
答案 0 :(得分:0)
我认为您应该将剩余金额计算更改为此:
pocet *=10;
penize = penize - pocet;
System.out.println("Zůstalo ti: " + penize);//Result how many user have
每次购买时,初始货币价值(笔数)都会减少并保存,您实际上不必保存剩余的钱。