public class test
{
public static void main(String[] args)
{
double original = 2;
double root = Math.sqrt(original);
double rootSquared = root * root;
if (rootSquared == original)
{
System.out.println("They are the same");
} else {
System.out.println("rootSquared is " + rootSquared);
}
}
}
[vagrant@localhost new]$ java test rootSquared is 2.0000000000000004
public class test
{
public static void main(String[] args)
{
double original = 2;
double root = Math.sqrt(original);
double rootSquared = root * root;
final double EPSILON = 1E - 12;
if (Math.abs(rootSquared - original) < EPSILON)
{
System.out.println("They are the same");
} else {
System.out.println("rootSquared is " + rootSquared);
}
}
}
How long does it take to be millionare
public class test
{
public static void main(String[] args)
{
double balance = 100;
double target = 1000000;
double rate = 0.01;
int year = 0;
while (balance < target)
{
double interest = balance * rate;
balance = balance + interest;
year++;
System.out.printf("Year %d: %8.2f\n", year, balance);
}
}
}
sum is digit
int n = 365;
int sum = 0;
while (n > 0)
{
int digit = n % 10;
sum = sum + digit;
n = n / 10;
}
System.out.println(sum);
while loop
while (balance >= target)
{
double interest = balance * rate
balance = balance + interest;
year++;
System.out.printf("Year %d: %8.2 year, balance")
}
The for loop
int counter = 1;
while (counter <= 6)
{
System.out.println(counter + ". Sleep");
couter++;
}
[/java]