Jump to content

Java: Different output than given in the result

Hip
Go to solution Solved by reniat,

I think this loop is MEANT to be division. some minor tweaks and:

//declare Variables
int dividend, divisor, remainder, quotient;

//initialize Variables
dividend = 23;
divisor = 5;
remainder = 0;
quotient = 0;

remainder = dividend;

while (remainder >= divisor)
{
  remainder = remainder - divisor;
  quotient++;
}

//Output
System.out.println(dividend + " " + divisor + " " + quotient + " " + remainder);

 

So perhaps the professor just had the conditional wrong in the while loop, and it should actually be help >= d2 instead of help >= d1? I don't see what the original could be trying to accomplish, since it would always terminate after only 1 loop assuming only positive numbers.

Hey guys,

 

I have the following code that I have given in a task sheet but in the results the output and the explanation of it is different than what the code outputs.

public class Main
{
	//create mainmethode
	public static void main(String[] args)
	{
		//declare Variables
		int d1, d2, help, count;
		
		//initialize Variables
		d1 = 23;
		d2 = 5;
		help = 0;
		count = 0;
		
		help = d1;
		
		while(help >= d1)
		{
			help = help - d2;
			count++;
		}
		
		//Output
		System.out.println(d1 + " " + d2 + " " + count + " " + help);	
	}
}

So the output is 

Quote

23 5 1 18

But the result should be (taking from the result of the task sheet)

Quote

23 5 4 3

...because, the program is dividing 23 with 5 and the output is the first number + number that it divides with + multiplier + rest.

 

At least this should be the output, but I copied the code 1:1 and when I go through the while loop I don't get the result.

Can you please explain me what is wrong?

 

Thanks in advance.

Link to comment
Share on other sites

Link to post
Share on other sites

12 minutes ago, Hip said:

...because, the program is dividing 23 with 5 and the output is the first number + number that it divides with + multiplier + rest.

Nowhere in your program are you doing any division.

 

The only variable you change is help, which you are subtracting d2 (5) from help (23), resulting in help equaling 18. Because 18 is less than 23, the loop only iterates once and exits, resulting in help equaling 18 and count only equaling 1.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Kavawuvi said:

Nowhere in your program are you doing any division.

 

The only variable you change is help, which you are subtracting d2 (5) from help (23), resulting in help equaling 18. Because 18 is less than 23, the loop only iterates once and exits, resulting in help equaling 18 and count only equaling 1.

Exactly! That's why I wonder where the Professor that created this task is taking his results from!

I am really confused by this.

 

Thanks for the fast reply.

Link to comment
Share on other sites

Link to post
Share on other sites

I think this loop is MEANT to be division. some minor tweaks and:

//declare Variables
int dividend, divisor, remainder, quotient;

//initialize Variables
dividend = 23;
divisor = 5;
remainder = 0;
quotient = 0;

remainder = dividend;

while (remainder >= divisor)
{
  remainder = remainder - divisor;
  quotient++;
}

//Output
System.out.println(dividend + " " + divisor + " " + quotient + " " + remainder);

 

So perhaps the professor just had the conditional wrong in the while loop, and it should actually be help >= d2 instead of help >= d1? I don't see what the original could be trying to accomplish, since it would always terminate after only 1 loop assuming only positive numbers.

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
Share on other sites

Link to post
Share on other sites

9 hours ago, reniat said:

I think this loop is MEANT to be division. some minor tweaks and:


//declare Variables
int dividend, divisor, remainder, quotient;

//initialize Variables
dividend = 23;
divisor = 5;
remainder = 0;
quotient = 0;

remainder = dividend;

while (remainder >= divisor)
{
  remainder = remainder - divisor;
  quotient++;
}

//Output
System.out.println(dividend + " " + divisor + " " + quotient + " " + remainder);

 

So perhaps the professor just had the conditional wrong in the while loop, and it should actually be help >= d2 instead of help >= d1? I don't see what the original could be trying to accomplish, since it would always terminate after only 1 loop assuming only positive numbers.

You could be right here, thanks for the reply.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×