Ok, donc, je suis très nouveau à Java.
Je suis en train de concevoir un calculateur de score, pour un projet que j'ai longtemps laissé de côté. Cependant, j'aimerais savoir comment le faire, pour ma propre connaissance.Score calculatrice
Le programme est supposé demander un jet de dé, et l'ajouter aux jets précédents, pour chaque joueur.
J'ai supposé qu'une boucle while accomplirait cela, mais à chaque fois qu'elle traverse la boucle, elle remet la variable à l'état actuel. En tant que tel, je ne peux pas obtenir un total ...
est Ci-dessous un code:
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;
}
On dirait que vous affectez 'roll1' à' roll' au lieu d'ajouter sa valeur. Vous pouvez essayer: 'roll1 + = roll;' – jcern
quoi avec l'accolade d'ouverture avant la boucle while? Quelle variable est censée contenir le total? est-ce total1? si c'est le cas, il suffit de charger le rouleau dans roll1 puis de faire le total + = roll1 (+ = signifie total = total + roll) – Ben313