如何为谁将赢得比赛分配字符串

时间:2013-10-06 00:37:11

标签: java if-statement random

我想知道如何使代码执行此操作,让我们说如果newSumDealer> newSumPlayer,然后经销商获胜。到目前为止,我只获得玩家获胜或其平局。我是java的新手,所以请在这里帮助我..给我一个线索左右,我会试着弄明白。

编辑:

//output
 System.out.println( "Player has " +number1+ " , " +number2+ " for a hand worth of ");
  if ( sumPlayer <= 10)
     System.out.println(  +sumPlayer+ " .");
  else if (sumPlayer > 10) {
      sumPlayer = sumPlayer - 10;
   System.out.println( sumPlayer - 10);
    }


  System.out.println( "Dealer has " +number3+ " , " +number4+ " for a hand worth of ");
    if ( sumDealer <= 10 )
  System.out.println( +sumDealer+ " .");
    else if ( sumDealer > 10 ) {
   sumDealer = sumDealer - 10;
  System.out.println( sumDealer - 10);
   }



//who wins
if(sumPlayer == sumDealer) {
   System.out.println("It's a tie!");
} else if (sumPlayer > sumDealer) {
   System.out.println("Player wins!");
} else {
   System.out.println("Dealer wins!");
}


  }
 }

2 个答案:

答案 0 :(得分:0)

您可以在if语句中使用其他条件。

if(sumPlayer == sumDealer) {
 System.out.println("It's a tie!");
} else if (sumPlayer > sumDealer) {
 System.out.println("Player wins!");
} else {
 System.out.println("Dealer wins!");
}

旁注:Java与JavaScript不同。事实上,他们完全不同。

答案 1 :(得分:0)

你还没有完成所有比较的条件。试试这个:

//who wins
if(sumPlayer == sumDealer) {
 System.out.println( " It's a tie! ");
} else if (sumPlayer > sumDealer) {
 System.out.println( " sumPlayer wins ");
} else {
 System.out.println( " sumDealer wins ");
}

编辑:根据评论,您似乎只是打印值而不是分配它们。试试这个:

if ( sumPlayer <= 10)
   System.out.println(  +sumPlayer+ " .");
 else if (sumPlayer > 10) {
       sumPlayer = sumPlayer - 10;
       System.out.println( sumPlayer - 10);
    }


 System.out.println( "Dealer has " +number3+ " , " +number4+ " for a hand worth of ");
 if ( sumDealer <= 10 )
   System.out.println( +sumDealer+ " .");
 else if ( sumDealer > 10 ) {
    sumDealer = sumDealer - 10;
    System.out.println( sumDealer - 10);
   }