New course! Every coder should learn Generative AI!
Try a free lesson0
c#, Apples for Friends, Basic Concepts, 10.2 Practice is very confusing
I have attached the question and the "given broken program", can someone steer me in the right direction? Increment & Decrement Operators Tom, Bob and James have 2,3 and 4 apples accordingly. The given program should equally redistribute the apples among them, but something is wrong. Fix the program. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { //your code goes here int applesTom = 3; int applesBob = 2; int applesJames = 4; //change the code Console.WriteLine(--applesTom); Console.WriteLine(applesBob++); Console.WriteLine(applesJames--); } } }
4 Answers
+ 6
How many apples should they all have?
3+2+4=9
9/3=3 each
This means Tom already has the correct amount. No change needed for Tom.
Bob needs 1 more and James needs 1 less.
Remember the difference between pre and post increment/decrement.
For pre the value of the variable is first changed then that new value is used. For post the current value is first used and then it is changed.
+ 3
ChaoticDawg I got it!!! Oh God. That was so easy, I was waaaay over complicating it. I super appreciate it.
0
My code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
//your code goes here
int applesTom = 3;
int applesBob = 2;
int applesJames = 4;
//change the code
Console.WriteLine(applesTom);
Console.WriteLine(++applesBob);
Console.WriteLine(--applesJames);
}
}
}
0
I still don't understand what I am supposed to do. Am I just suppose to change the increment/decrement or am i suppose to make an if/else statement?