在我的情况下的其他声明......我是一个菜鸟

时间:2014-07-17 03:27:17

标签: java

我的代码需要帮助。我是java的菜鸟,但希望有一天真的很擅长。

static Scanner sc = new Scanner(System.in);
   public static void main(String[] args){
      Lottery();
   }

   public static void Lottery(){
      System.out.println("Welcome to Jacks Lottery!");
      System.out.println("If the Numbers match... You Win! It will pick a random number of the money you win!");
      int random = (int)(Math.random() * 50 + 1);
      int r = (int)(Math.random() * 50 + 1);
      int m = (int)(Math.random() * 1000 + 1);
      System.out.println("The First Number is: " + random);
      System.out.println("The Second Number is: " + r);

      if (random == r);
      System.out.println("You Win! You get: $" + m);
      if (random != r);
      System.out.println("You lose :( Try again?");
   }
}

我如何在这些之间签名?:

if (random == r);
   System.out.println("You Win! You get: $" + m);
if (random != r);
   System.out.println("You lose :( Try again?");

不要批评并说这是一个不起眼的小游戏,因为我知道它是。我正在制作一个有趣的小东西来展示我的家人并让他们惊叹。

4 个答案:

答案 0 :(得分:2)

if

中删除该分号
if (random == r);

;表示语句结束,因此无论条件

,下一个语句都将被执行

也可以切换到if else而不是2条件检查

if(condition) {

} else {

}

答案 1 :(得分:2)

在声明结尾处,您必须使用' {'没有';'

if (random == r){
       System.out.println("You Win! You get: $" + m);
}
else{
      System.out.println("You lose :( Try again?");
}

答案 2 :(得分:1)

if (random == r){
   System.out.println("You Win! You get: $" + m);
}
else{
   System.out.println("You lose :( Try again?");
}

答案 3 :(得分:1)

考虑以下代码行

  if (random == r); // when your if condition match it will execute nothing
                    // since there is no code block between close ) and ;
  System.out.println("You Win! You get: $" + m);

您可以使用以下

  if (random == r) // remove ;
  System.out.println("You Win! You get: $" + m);

现在您的代码正常运行。但是,作为一种好的做法,应该{}使用if

 if (random == r){
 System.out.println("You Win! You get: $" + m);
 }

您可以再次更改以下代码

  if (random == r);
  System.out.println("You Win! You get: $" + m);
  if (random != r);
  System.out.println("You lose :( Try again?");

更正版

  if (random == r){
   System.out.println("You Win! You get: $" + m);
  }else{    
   System.out.println("You lose :( Try again?");
  }