public class Main {
public static void main(String[] args) {
byte tinyNum = 100;
short smallNum = 20_000;
int num = 1<<20;
long largeNum = (long) 1<<50;
float decimal = 10.5050f;
double largeDecimal = 3.141592653589;
boolean trueOrFalse = true;
System.out.println(num);
System.out.println(largeNum);
System.out.println(decimal);
if (trueOrFalse)
System.out.println("true");
else
System.out.println("false");
}
}
1048576
1125899906842624
10.505
true
if and if else
ืชืจืืื: ืชื ืืื
Given an integer n , perform the following conditional actions:
If n is odd, print Strange
If n is even and in the inclusive range of 2 to 5 , print Not Strange
If n is even and in the inclusive range of 6 to 20, print Strange
If n is even and greater than 20, print Not Strange
Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.print("Enter an integer: ");
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
if (n%2==1)
System.out.println("Strange");
else if (n%2==0 && n>=2 && n<=5)
System.out.println("Not Strange");
else if (n%2==0 && n>=6 && n<=20)
System.out.println("Strange");
else if (n%2==0 && n>20)
System.out.println("Not Strange");
}
}
public class Main {
public static void main(String[] args) {
int x = 8, y = 13;
double d = y/x;
System.out.println("y/x = "+d);
d = (double)y/x;
System.out.println("y/x = "+d);
double a = 5, b = 18;
int c = (int)(a/b);
System.out.println("a/b = "+c);
}
}
public class Main {
public static void main(String[] args) {
System.out.println("Integer.MAX_VALUE = "+Integer.MAX_VALUE);
System.out.println("Integer.MIN_VALUE = "+Integer.MIN_VALUE);
System.out.println("Integer.SIZE = "+Integer.SIZE);
int x = Integer.MAX_VALUE + 1;
System.out.println("Integer.MAX_VALUE + 1 = "+x);
int a = 0, b=10, z;
z = b/a;
System.out.println("b/a = "+z);
}
}
Integer.MAX_VALUE = 2147483647
Integer.MIN_VALUE = -2147483648
Integer.SIZE = 32
Integer.MAX_VALUE + 1 = -2147483648
Exception in thread "main" java.lang.ArithmeticException: / by zero
at com.company.Main.main(Main.java:11)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.print("Enter an integer: ");
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
if (Short.MAX_VALUE>=n && Short.MIN_VALUE<=n)
System.out.println("n is a: short");
else if (Integer.MAX_VALUE>=n && Integer.MIN_VALUE<=n)
System.out.println("n is an: int");
else if (Long.MAX_VALUE>=n && Long.MIN_VALUE<=n)
System.out.println("n is a: long");
}
}
Enter an integer: 10.5
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at com.company.Main.main(Main.java:8)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.print("Enter a number: ");
Scanner scanner = new Scanner(System.in);
double n = scanner.nextDouble();
while (n!=0.) {
if (Short.MAX_VALUE >= n && Short.MIN_VALUE <= n && n % 1 == 0.) {
System.out.println(n + " can fit in a: ");
System.out.println("* short\n* int\n* long\n");
} else if (Integer.MAX_VALUE >= n && Integer.MIN_VALUE <= n && n % 1 == 0.) {
System.out.println(n + " can fit in a: ");
System.out.println("* int\n* long\n");
} else if (Long.MAX_VALUE >= n && Long.MIN_VALUE <= n && n % 1 == 0.)
System.out.println(n + " can fit in a: \n* long\n");
else if ((Float.MAX_VALUE >= n && Float.MIN_VALUE <= n) || (Double.MAX_VALUE >= n && Double.MIN_VALUE <= n))
System.out.println("n is either a: float, double\n");
//receive next input
System.out.print("Enter a number: ");
n = scanner.nextDouble();
}
scanner.close();
}
}
Enter a number: 10.5
n is either a: float, double
Enter a number: 1000000
1000000.0 can fit in a:
* int
* long
Enter a number: 0
ืื ืืืื ื ืืืื ืืขืฉืืช && ((short) n) == n ื ((int) n) == n ืืชื ืื