Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I'm still learning Java....
My task is to write a program which divides two doubles, but before it displays result of dividing, it has to say if they are dividible or not (without a rest). I HAVE TO use ternary operator.
code:
public class exercise {
public static void main(String[] args){
double x = 8.4;
double y = 4.2;
double z = (int)(x % y);
String result = (z>0) ? "not dividible" : "dividible";
System.out.println("This operation is: " + result);
System.out.println("The result of dividing is: " + (x/y));
}
}
Compiler says for the line "String result = (z>0) ?....." that it requires boolean, and that it found int. Of course, compilation failed.
You've missed the last closing bracket ()) of this statement.
System.out.println("The result of dividing is: " + (x/y);
It should be:
System.out.println("The result of dividing is: " + (x/y));
↑
All the other stuff compiles for me.
My compiler says that you dont have last parenthesis ) in this line:
System.out.println("The result of dividing is: " + (x/y);
Try this:
System.out.println("The result of dividing is: " + (x/y));
It compiles for me.
The problem comes from the syntax error, due you have forgotten the last closing bracket () of the last system.out line.
System.out.println("The result of dividing is: " + (x/y);
Should be:
System.out.println("The result of dividing is: " + (x/y));
If you solve this, then there is no problem.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
Trying to figure out if I wanted to run a number 17 digits long through 50 million divisions how long would it take on a decent i7 PC and what would you recommend for language? Also I would like to scale it up over time so need a language that can be flexible when I get to say 30ish digit long numbers. For now basically I begin with a 17 digit long number and as I go I only care about the a smaller number after each calculation so it will get smaller quick. I am only doing division and subtraction and not keeping any remainders. Thoughts?
I've knocked up a quick program to test how long it takes, here I believe I have used a 20 digit long number. I know it's not exactly the parameters that you have asked for, however it gives a good illustration to what kind of speed you can be expecting.
This was run on a i5-6200U # 2.30GHz
If you are interested on the code that I used here it is. It will not be able to divide 30 digits but a little tweaking will allow it to. This is written in C#.
using System;
namespace _50_Million
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter Number ");
string number = Console.ReadLine();
Console.Write("What to divide by ");
string divide = Console.ReadLine();
Console.Write("How many times ");
string d = Console.ReadLine();
decimal previousNumber = Convert.ToDecimal(number);
decimal times = Convert.ToDecimal(d);
decimal divideDecimal = Convert.ToDecimal(divide);
var watch = System.Diagnostics.Stopwatch.StartNew();
decimal newNumber = 0;
for (int i = 0; i < times; i++)
{
newNumber = previousNumber / divideDecimal;
previousNumber = newNumber;
}
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
Console.WriteLine("It has taken " + elapsedMs + " millisecounds to divide " + number + " by " + divide + ", " + d + " times.");
Console.WriteLine("The Answer is " + newNumber);
Console.ReadLine();
}
}
}
This question already has answers here:
Why does the division of two integers return 0.0 in Java? [duplicate]
(6 answers)
Closed 8 years ago.
So this is a portion of the code I'm using, we were only supposed to begin by fixing some logical errors because of wrong answers. I found a couple of syntax errors as well that wouldn't let the compiler finish up...I keep getting "0" for fToC...
//declaration statements
final int BOILING_IN_F = 212;
int fToC;
string output;
//there is other code between that has nothing
//to do with outputting the boiling point.
fToC = (5/9) * (BOILING_IN_F - 32);
output = BOILING_IN_F + " in Fahrenheit is " + fToC
+ " in Celsius.";
System.out.println(output); //this is outputting 0 for fToC
System.out.println();
Answers were useful, except I needed to declare fToC as a double before compiling and running the program. Thank you.
You are performing Java's integer division with 5/9, which must result in another integer -- 0, so the result is 0. Use double literals to force floating-point math.
fToC = (5.0/9.0) * (BOILING_IN_F - 32);
You'll need fToC to be a double (and change the typo string output to be String output).
(5/9) is 0 due to int division. Try (5.0/9)
The full expression should be :
fToC = (5.0/9) * (BOILING_IN_F - 32);
(5/9) You're diving two integers, so this is truncated, so it equals 0. And you're multiplying that 0 with another number.
Use instead (5.0f/9.0f) to precise the fact that your number are actually decimal numbers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I'm trying to write a simple program from this task.
The number of calories burned per hours by cycling , jogging, and swimming are 200, 475, and 275 respectively. A person loses 1 pound of weight for each 3500 calories burned. Write a program that declares 3 variables, one to store the number of hours spent jogging, the second to store the number of hours spent cycling and the third to store the number of hours spent swimming. Assign each of these variables values. Calculate and display the number of pounds worked off.
The code I have written is:
public class task2
{
public static void main(String[] args)
{
double c = 2; //2 hrs of cycling
double j = 1; //1 hr of jogging
double s = 2; //2 hrs of swimming
double cycle = c * 200; //400 calories
double jog = j * 475; //475 calories
double swim = s * 275; //550 calories
double sum = cycle + jog + swim / 3500; //1425 / 3500 is what should be in here
System.out.println("You've burned " + sum + " calories");
}
}
The answer I get back is:
"You've burned 0.40714286 calories"
but I get back:
"You've burned 875.15871428571428 calories".
I don't know where I went wrong. I want the output to be a double so it can show the answer if it's below 3500 calories.
You might meant: double sum = (cycle + jog + swim) / 3500;
You need () to group all the +'s so that the addition is carried out before the division:
double sum = (cycle + jog + swim) / 3500;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
This is actually one of my lab questions.
You can test to see if an integer, x, is even or odd using the Boolean expression (x / 2) * 2 == x. Integers that are even make this expression true, and odd integers make the expression false. Use a for loop to iterate five times. With each iteration, request an integer from the user. Print each integer the user types, and whether it is even or odd. Keep up with the number of even
and odd integers the user types, and print “Done” when finished, so the user won’t try to type another integer. Finally, print out the number of even and odd integers that were entered.
I basically know what this question requires me to do. However, I don’t quite understand why boolean expression, (x/2)*2 == x, can test whether the integer is an ever number or an odd number. Lets say my number is 59, which is an odd integer obviously. 59 divided by 2 is 29.5. 29.5 times 2, which equals 59. No matter what x is, (x/2)*2 always equals x. So how to make this expression false when the integer is an odd. Then I can determine what I should print.
Because you're dealing with integers, there's always a rounding down to the nearest round number.
59/2 = 29 when all elements are integers.
Multiplying the result by 2 gives 58, so since the 2 numbers aren't the same, we deduce that the number, 59, is odd.
This should be your code written in a beginners way, if u still don't understand something just ask. The formula x==(x/2)*2 is used because when you are dividing two integer variables the result is always a round number, which later if it's odd, won't give u the same initial number. (59/2=29 => 29*2=58 => false)...
import java.util.*;
public class EvenOrOdd{
public static void main( String [] args){
Scanner scan=new Scanner(System.in);
boolean result=true;
List<Integer> EvenNums = new ArrayList<Integer>();
List<Integer> OddNums = new ArrayList<Integer>();
for(int i=0; i<5; i++){
System.out.println("Enter a number: ");
int x=scan.nextInt();
if(x==(x/2)*2){
result=true;
System.out.println(result);
EvenNums.add(x);
continue;
}else if(x!=(x/2)*2){
result=false;
System.out.println(result);
OddNums.add(x);
continue;
}
}
System.out.println("Done");
System.out.println("Even numbers are: " + EvenNums);
System.out.println("Odd numbers are: " + OddNums);
}
}
This question already has answers here:
Integer division: How do you produce a double?
(11 answers)
Closed 7 years ago.
I am dividing two int values, and im expecting to get a double one. but it works very strange, it has the correct values just before division but it doesnt give the right answer.
public void Analyse() {
for (FlowPacket fp : this.flow.GetAll()) {
if (fp.direction==1){
this.sentPackets++;
this.sentLength = this.sentLength + fp.packetLength;
}
else{
this.receivedPackets++;
this.receivedLength = this.receivedLength + fp.packetLength;
}
}
if(this.receivedPackets==0)
this.receivedPackets = 1;
}
public double CalcRatio() {
return (this.sentPackets/this.receivedPackets);
}
----------------------------main--------------------------------
System.out.print("Sent packets: " + analyser.getTotalSentPackets() + " , ");
System.out.print("Received packets: " + analyser.getTotalReceivedPackets() + " , ");
System.out.print("ratio: " + analyser.CalcRatio() + " , ");
----------------------------outout------------------------------
Sent packets: 2694 , Received packets: 5753 , ratio: 0
(double)this.sentPackets/this.receivedPackets
... should fix it.
The result of the division is cast to a double AFTER integer division (with rounding down) is performed. Cast one of the integers to a double BEFORE dividing so that double division occurs.
When dividing an int by an int the answer will be an int. Therefor it will cut off any remainder there may be in the answer.
In order to get a double answer you must cast one of the ints to a double.
Cast at least one of the ints to (double) before dividing.
Need to cast to double...
public double CalcRatio() {
return ( (double) this.sentPackets/ (double) this.receivedPackets);
}
It is not only Java-specific behaviour. It works the same way in .NET, too.