Increment and decrement | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Increment and decrement

Tom and Bob are playing a board game, in which both players start with the same number of points. Tom won the first game and got 1 point, while Bob lost the game, and therefore lost 1 point. You are given a program that is intended to take the initial score and increase Tom's score by 1 and decrease Bob's score by 1. But something is wrong: the program outputs the scores without the change. Task Fix the program to result in the expected outputs. Sample Input 5 Sample Output Round 1 results: 6 4 Explanation Both players had 5 points at the start of the game. After the first game, Tom gained 1 point (6, the first outputted number), and Bob lost 1 point (4, the second outputted number).

24th Feb 2023, 7:11 AM
Shreyas Vaidya
Shreyas Vaidya - avatar
8 Answers
+ 3
</CODER> completely missed the part about increments and decrements. (and adding a point to the team of locked cases) Shreyas Vaidya, what is the difference between pre-increment and post-increment. Your answer is almost correct and I believe that if you answer my question, you'll see the solution to your code error.
24th Feb 2023, 12:25 PM
Ausgrindtube
Ausgrindtube - avatar
+ 2
Before expecting answer from community, please provide yours attempts. The exersize is about to learn and use pre - increment/ decrement operators use. And know difference between pre and post increment/ decrement Here it uses post increment/ decrement which output same value, even though it updates scoreTom value. Instead if you use pre increment/ decrement thenga fixes the fault. So instead, //fix System.out.println(scoreTom++); System.out.println(scoreBob--); It expecting to use : System.out.println( ++scoreTom ); System.out.println( --scoreBob ); Hope it helps.
24th Feb 2023, 1:41 PM
Jayakrishna 🇮🇳
+ 1
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); //taking initial score int initScore = scanner.nextInt(); int scoreTom = initScore; int scoreBob = initScore; System.out.println("Round 1 results:"); //fix System.out.println(scoreTom++); System.out.println(scoreBob--); } }
24th Feb 2023, 7:14 AM
Shreyas Vaidya
Shreyas Vaidya - avatar
+ 1
System.out.println(scoreTom = scoreTom + 1); System.out.println(scoreBob = scoreBob - 1);
24th Feb 2023, 7:17 AM
Knight
Knight - avatar
0
Can I see your result?
24th Feb 2023, 7:12 AM
Knight
Knight - avatar
0
Can you please tell me the answer
24th Feb 2023, 7:15 AM
Shreyas Vaidya
Shreyas Vaidya - avatar
0
Thanks you
24th Feb 2023, 7:19 AM
Shreyas Vaidya
Shreyas Vaidya - avatar
0
Shreyas Vaidya You are welcome!
24th Feb 2023, 7:22 AM
Knight
Knight - avatar