System.out.print("\n\nEnter an integer number:=");
int x = sc.nextInt();
if (x>=100)
{
System.out.println("\n x is greater or equal to 100\n");
System.out.println("\n You think better\n");
}
System.out.println("\n x is less than 100\n");
}
}
复制代码
Explanation: Here the statements after if expression are put in the braces. When the if condition is true, all the statements within the braces get executed; in case the condition is false, the statements within the braces are skipped. Regardless of true/falsity of if condition, the remaining statements are executed and so is the output. To execute only if-dependent statements, one will have to use if-else construct or terminate the program after the execution of if block. The second alternative is shown in the next program.
/*PROG 4.3 PROGRAM TO CHECK NUMBER IS EVEN OR ODD */
import java.io.*;
import java.util.*;
class JPS4
{
public static void main(String args[])
{
int x;
Scanner sc = new Scanner(System.in);
PrintStream pr = System.out;
pr.println("\n Enter an Integer number");
x = sc.nextInt();
if (x % 2 == 0)
pr.println("Number " + x + " is even");
else
pr.println("Number " + x + " is odd");
}
}
复制代码
Explanation: As discussed earlier, System.out is an object of class PrintStream so it is assigned to a reference pr of PrintStream type. Now instead of writingSystem.out.println, you can write pr.println. If the if condition x%2 = = 0 is true, the number is even, else the number is not even. The else part executes only when if part is false and vice-versa. In the program both if and else parts contain just one statement to be dependent on them so braces are not needed; however, if you put the braces there will not be any harm.
/*PROG 4.5 MAXIMUM OF THREE NUMBERS USING NESTING OF IF-ELSE STATEMENT */
import java.io.*;
import java.util.*;
class JPS6
{
public static void main(String args[])
{
int a, b, c, d = 0;
Scanner sc = new Scanner(System.in);
System.out.println("\nEnter three number");
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();
if ((a == b) && (a == c))
System.out.println("All three are equal\n");
else
{
if (a > b)
{
if (a > c)
d = a;
else
d = c;
}
else
{
if (b > c)
d = b;
else
d = c;
}
}
System.out.println("Maximum is " + d);
}
}
复制代码
Explanation:If the numbers are equal the first if condition is true else else block is executed. In the else part, there is one more if-else. If a > b is true then inside this if block, using one more if-else it is checked whether a > c, if this is so then a is greater else c is greater.
If the first if condition is false in the else block then else part of the inner if executes which means b > a. Inside this inner else one more if-else checks whether b > c, if it is so then b is greater else c is greater.
/*PROG 4.6 FINDING ROOTS OF THE QUADRATIC EQUATION */
import java.io.*;
import java.util.*;
class JPS8
{
public static void main(String args[])
{
double a, b, c, r1, r2, dis;
Scanner sc = new Scanner(System.in);
System.out.print("\n\nEnter value of A :=");
a = sc.nextDouble();
System.out.print("Enter value of B :=");
b = sc.nextDouble();
System.out.print("Enter value of C :=");
c = sc.nextDouble();
dis = b * b - 4 * a * c;
if (dis < 0)
System.out.print("\nRoots are imaginary\n");
else if (dis == 0)
{
System.out.print("\nRoots are equal\n");
r1 = -b / (2 * a);
r2 = b / (2 * a);
System.out.print("\nRoot1 or r1 := "
+r1+"\n");
System.out.print("\nRoot2 or r2 := "
+r2+"\n");
}
else
{
System.out.println("\n Roots are
unequal\n");
r1 = (-b + Math.sqrt(dis)) / (2.0 * a);
r2 = (-b + Math.sqrt(dis)) / (2.0 * a);
System.out.print("\nRoot1 or r1:= "
+r1+"\n");
System.out.print("\nRoot2 or r2:= "
+r2+"\n");
}
}
}
复制代码
Explanation:
The quadratic equation in mathematics is given as follows:
AX2 + BX + C = 0,
where, A, B and C are constants. The solution of the equation comprises two roots as power of X is 2.
R1 = (-B + sqrt (B * B - 4 * A * C)) / 2 * A
R2 = (-B - sqrt (B * B - 4 * A * C)) / 2 * A
The expression B * B - 4 * A * C is known as discriminant (say dis), and on the basis of its value the roots are determined as equal (dis = = 0) imaginary (dis < 0) or unequal (dis > 0) as shown in the above program.
In the program, the value of three constants are taken as output using A, B and C and the value of dis is calculated.
/*PROG 4.7 TO DETERMINE THE STATUS OF ENTERED CHARACTER */
import java.io.*;
class JPS9
{
public static void main(String args[])
{
char c;
try
{
DataInputStream input;
input=new DataInputStream(System.in);
System.out.println("Enter a character");
c=(char)(input.read());
if(c>=65 && c <=90)
System.out.println("You have entered
uppercase letter\n");
else if(c>=97&&c<=122)
System.out.println("You have entered
lowercase letter\n");
else if(c>=48 && c<=57)
System.out.println("You have eneterd a digit\n");
else
System.out.println("You have entered a
special symbol\n");
}
catch(Exception eobj)
{
System.out.println("ERROR!!!!");
}
}
}
复制代码
Explanation:
The ASCII/unicode values is from 97 to 122 (inclusive both) for lowercase alphabets and 65 to 90 (inclusive both) for uppercase alphabets. It is checked whether the entered character is within these two ranges using else-if ladder. Similarly ASCII/Unicode values for digits are from 48 to 57 (inclusive both), so character entered is also checked with this range. If all the three conditions fail then the character entered must be a special symbol.
Explanation:
There is no break statement in case 1, so both case 1 and case 2 are assumed to be true and so is the output. In fact, due to the absence of break statement in the second case, rest of the statements are considered part of the second case till a break is not found. Break in case 2 causes control to come out from switch. If break were not in case 2 also then default would have got executed too.
Explanation:
Any +ve number is taken as input. The number n is multiplied by 1 through 10 inside the loop and the loop counter; the number and value are displayed by formatting as to produce the desired output.