好的,所以,我對java很新。
我正在設計一個分數計算器,這個計劃對於我一直擱在一旁的項目來說。不過,我想知道如何做到這一點。得分計算器
該程序應該要求擲骰子,並將其添加到前面的每個玩家。
我假設一個while循環會完成這個任務,但是每次它通過循環時,都會將變量重置爲當前的卷。因此,我不能讓一個總...
下面是一些代碼:
static int players;
static String p1;
static String p2;
static String p3;
static String p4;
static int maxScore;
static int roll1;
static int roll2;
static int roll3;
static int roll4;
static int total1;
static int total2;
static int total3;
static int total4;
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter number of players: ");
players=keyboard.nextInt();
System.out.print("Enter Maximum Score: ");
maxScore=keyboard.nextInt();
if(players==2){ //will add more ifs when i get the code right
System.out.println("Please enter players names.");
System.out.print("Player 1: ");
p1=keyboard.next();
System.out.print("Player 2: ");
p2=keyboard.next();
System.out.println(p1 + "\t \t " + p2 + "\n"
+ "_______ \t _______ \n"); //displays scorecard look with players names
{
while (total1 < maxScore && total2<maxScore) {
//scorecard quits when maxScore is reached by either player
int roll;
total1=(roll+roll1);
System.out.print("");
roll=keyboard.nextInt(); //asks for current roll
System.out.print("\n"+"_____"+"\n");
System.out.print(roll+"+"+"\n"+roll1+"\n"+"_____"+"\n"+(roll+roll1)+"\n");
/*i want this to display total score + last roll and then
*total it again on the next line*/
roll1=roll;
}
看起來像是將'roll1'分配給'roll'而不是添加其值。你可以試試:'roll1 + = roll;' – jcern
while循環之前的大括號是什麼?什麼變量應該保持總量?總數是1嗎?如果是這樣,只需將卷裝入卷1,然後執行總計+ = roll1(+ =表示總數=總計+卷) – Ben313