Below, there is the part of the dice game programme. Then, i couldnt solve a lit
ID: 3546555 • Letter: B
Question
Below, there is the part of the dice game programme. Then, i couldnt solve a little thing, how can i do, if a user or computer rolls 1, the sum will equal zero during that turn!!! But, the previous sum(the final score) will remain same. The sample run is ;
Computer rolled : 5 , Do you want to hold this score Y/N ? N
Computer rolled : 4 , Do you want to hold this score Y/N ? Y
Computer's final score is : 9
You, it's your turn now.
You rolled : 6, Do you want to hold this score Y/N? Y
You, your final score is : 6
Computer rolled : 2 , Do you want to hold this score Y/N ? Y
Computer's final score is : 11
You , it's your turn now.
You rolled : 4 , Do you want to hold this score Y/N? N
You rolled : 1 ,,,,
You, your final score is 6 . (Here, i couldnt hold this score, i always hold zero :S ??? i want to hold this number, not zero. )
............................
.
.
.
.
--------------------------------------
The part of the programme
//main
do{
int x = (int)(1 + Math.random() * 6);
sum += x;
/* if(x==1) { //( Here [if(x==1)] ,I know, its wrong cause everytime the sum is zero. i dont want to be zero. It's enough to change this part
sum = 0; accurately..)
System.out.println("You rolled : " + x); break;
} */
System.out.print("You rolled : " + x + ", Do you want to hold this score Y/N?");
s = input.next();
choice = s.charAt(0);
}while(choice=='r' || choice=='R');
System.out.println(sum );
----------------
Please, do not use method or whatever , i know it is simple but i locked! and please add ur codes in the above programme
Explanation / Answer
// take another variable local_sum which will intialized to 0 for every turn
// if any x rolled during rolling make local_sum to zero and exit.
// if local_sum not equal to zero after exiting loop add that to sum.
int local_sum =0;
//main
do{
int x = (int)(1 + Math.random() * 6);
local_sum += x;
if(x==1)
{
System.out.println("You rolled : " + x);
local_sum=0;
break;
}
/* if(x==1) { //( Here [if(x==1)] ,I know, its wrong cause everytime the sum is zero. i dont want to be zero. It's enough to change this part
sum = 0; accurately..)
break;
} */
System.out.print("You rolled : " + x + ", Do you want to hold this score Y/N?");
s = input.next();
choice = s.charAt(0);
}while(choice=='r' || choice=='R');
if(local_sum!=0)
sum = sum+local_sum;
System.out.println(sum );
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.