为什么这个循环不会再次运行? (JAVA)

时间:2017-11-13 00:54:26

标签: java while-loop

我设计了一个使用while循环的简单老虎机。由于某种原因,循环总是在一个循环结束时中断。如果用户输入投注,我希望在奖金出现后运行循环。在循环结束时,重新分配赌注,但循环中断了。

//declarations
int tokens = 50;
int bet;
int winnings;
int slot1=0;
int slot2=0;
int slot3=0;
Scanner scan = new Scanner(System.in);
Random generator = new Random();

//initializatons
System.out.println("You have "+tokens+" tokens");
System.out.println("How much do you want to bet?");
bet = scan.nextInt();  

tokens = tokens-bet;

  while(bet>0&&tokens>0){
  slot1=generator.nextInt(9);
  slot2=generator.nextInt(9);
  slot3=generator.nextInt(9);
  bet=bet-bet;
}

System.out.println((slot1)+"\t"+(slot2)+"\t"+(slot3));

if(slot1 == slot2 && slot2 == slot3) {
  winnings = (10*bet);
  System.out.println("Jackpot! You won "+winnings+"  tokens!");
  System.out.println("Bet again?");
  bet = scan.nextInt();
  tokens = tokens+bet;   
}
else if(slot1==slot2 || slot2 == slot3) {
  winnings = (2*bet);
  System.out.println("You win 10 tokens!");
  System.out.println("Bet again?"); 
  bet = scan.nextInt();
  tokens= winnings+tokens;
}
else {
  System.out.println("Bet again?");
  bet= scan.nextInt();
}
scan.close();

1 个答案:

答案 0 :(得分:1)

while(bet>0&&tokens>0){
  slot1=generator.nextInt(9);
  slot2=generator.nextInt(9);
  slot3=generator.nextInt(9);
  bet=bet-bet;
}

在此代码的末尾,您有bet=bet-bet;,这意味着0因此循环中断,因为如果bet > 0 bet -bet = 0//String grabber Convert.ToString(gvOrders.SelectedRow.Cells[2].Text); //Checkbox grabber Boolean.TryParse(gvOrders.SelectedRow.Cells[11].Text, out bool pep);

,您的循环将会执行