循环和/或if else双返回

时间:2014-02-26 17:59:21

标签: java if-statement while-loop

我正在尝试编写一个与用户投注高或七的玩骰子游戏的程序。到目前为止,我已成功获得骰子并添加部分正确,我的投注功能不顺利。以下是我到目前为止的情况:

import java.util.Random;
import java.util.Scanner;

public class Project07
{
  public static void main(String[] args)
  {
    Scanner keyboard = new Scanner(System.in);
    Random r = new Random();
    int money = 100;
    int newtotal = 100;
    System.out.println("You have " + money + " dollars");
    while (money > 0)
    {
      System.out.println("Enter an amount to be or 0 to quit");
      int bet = keyboard.nextInt();
      if (bet == 0)
      {
        System.out.println("You end with " + money + " dollars");
        System.exit(0);
      }

      System.out.println("Bet 1 for high, 2 for low, or 3 for 7's");
      int choose = keyboard.nextInt();
      int diceb = r.nextInt(7 - 1) + 1;
      int dicea = r.nextInt(7 - 1) + 1;
      int dicetotal = dicea + diceb;
      System.out.println(dicetotal);
      System.out.println("You bet $" + bet);
      if ((choose == 1) && (dicetotal > 7))
      {
        newtotal = newtotal + bet;
        System.out.println("You win $" + bet);
        System.out.print("You have " + newtotal + " dollars left");
      }
      else if ((choose == 1) && (dicetotal <= 7))
      {
        newtotal = newtotal - bet;
        System.out.println("You have " + newtotal + "dollars left");
      }
      else if ((choose == 2) && (dicetotal < 7))
      {
        newtotal = newtotal + bet;
        System.out.println("You have " + newtotal + "dollars left");
      }
      else if ((choose == 2) && (dicetotal >= 7))
      {
        newtotal = newtotal - bet;
        System.out.println("You have " + newtotal + "dollars left");
      }
      else if ((choose == 3) && (dicetotal == 7))
      {
        newtotal = newtotal + bet;
        System.out.println("You have " + newtotal + "dollars left");
      }
      else if ((choose == 3) && (dicetotal != 7))
        ;
      {
        newtotal = newtotal - bet;
        System.out.println("You have " + newtotal + "dollars left");
      }
    }
  }
}

我得到的输出应该是这样的:你有100美元 输入金额或0以退出 20 下注1为高,2为低,或3为7 1 11 你打赌20美元 你赢了20美元 你剩下120美元 输入金额或0以退出 0 你以120美元结束

然而它似乎激活if else块的输赢部分并给我这个:你有100美元 输入金额或0以退出 20 下注1为高,2为低,或3为7 1 11 你打赌20美元 你赢了20美元 你剩下120美元你剩下100美元 输入金额或0以退出 0 你以100美元结束

不太确定我的问题在哪里,也许我需要专门针对它的投注功能编写一个新方法?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

格式正确(两种主要风格之一):

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    Random r = new Random();
    int money = 100;
    int newtotal = 100;
    System.out.println("You have " + money + " dollars");
    while (money > 0) {
        System.out.println("Enter an amount to be or 0 to quit");
        int bet = keyboard.nextInt();
        if (bet == 0) {
            System.out.println("You end with " + money +" dollars");
            System.exit(0);
        }
        System.out.println("Bet 1 for high, 2 for low, or 3 for 7's");
        int choose = keyboard.nextInt();
        int diceb = r.nextInt(7 - 1) + 1;
        int dicea = r.nextInt(7 - 1) + 1;
        int dicetotal = dicea + diceb;
        System.out.println(dicetotal);
        System.out.println("You bet $" + bet);
        if ((choose == 1) && (dicetotal >7)) {
            newtotal= newtotal + bet; 
            System.out.println("You win $" + bet); 
            System.out.print("You have " + newtotal +" dollars left");
        }
        else if ((choose == 1) && (dicetotal <= 7)) { 
            newtotal = newtotal - bet; 
            System.out.println("You have " + newtotal + "dollars left"); 
        }
        else if ((choose == 2) && (dicetotal < 7)) {
            newtotal = newtotal + bet; 
            System.out.println("You have " + newtotal  + "dollars left");
        }
        else if ((choose == 2) && (dicetotal >= 7)) {
            newtotal = newtotal - bet; 
            System.out.println("You have " + newtotal  + "dollars left");
        }
        else if ((choose == 3) && (dicetotal == 7)) {
            newtotal = newtotal + bet; 
            System.out.println("You have " + newtotal + "dollars left");
        }
        else if ((choose == 3) && (dicetotal != 7)); { <<== Wayward semicolon!!
            newtotal = newtotal - bet; 
            System.out.println("You have " + newtotal  + "dollars left");
        }
    }
}
任性的分号很明显,因为除了在一行之外,你不会在分号之外放置分号。可以轻松浏览代码并查看if条件 - 您无需将它们从杂乱中挖掘出来。 <{1}}的结尾在缩进处显而易见。

(另一种主要风格是将while个字符放在他们自己的行上:

{

我不喜欢这种风格,因为它散布了太多文本(屏幕空间在尝试阅读和理解大型程序时很有价值)并且因为,任性 ... } else if ((choose == 1) && (dicetotal <= 7)) { newtotal = newtotal - bet; System.out.println("You have " + newtotal + "dollars left"); } else if ((choose == 2) && (dicetotal < 7)) { newtotal = newtotal + bet; System.out.println("You have " + newtotal + "dollars left"); } ... ;不合适并不明显。)

(还有一种样式可以将结束;放在与后续}相同的行上:

else

这个更紧凑,但是我的眼睛太紧凑了,并且开始变得杂乱无章。另外,它使得无法在 ... } else if ((choose == 1) && (dicetotal <= 7)) { newtotal = newtotal - bet; System.out.println("You have " + newtotal + "dollars left"); } else if ((choose == 2) && (dicetotal < 7)) { ... 之前放置注释行。但其他人有不同的偏好。主要选择一种风格并坚持使用它,并在一个团队中尝试同意一个共同的方案(没有压迫它)。)