Java关于%判断符、String比较、switch和多重if的练习题
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Q1
// a) Determine whether 3x8=27.
System.out.print("--------------------------------------------------------\n1.\na)\n");
final int x = 3, y = 8;
if (x*y==27){
System.out.println("3 x 8 = 27");
}else{
System.out.print(x +"*" + y + "=" + x*y +"\n");
}
// b) Determine whether an input integer is an odd number or even number.
System.out.println("b)");
int number;
Scanner s = new Scanner(System.in);
System.out.print("Please input a random integer : ");
try{
number = s.nextInt();
if (number % 2 == 0){
System.out.println("You inputted an even number.");
}else{
System.out.println("You inputted an odd number.");
}
}catch(Exception e){
System.out.println("Wrong value, exiting...");
}
// c) Determine whether a character is a capital letter.
Scanner s1 = new Scanner(System.in);
System.out.println("c)");
System.out.print("Please input a character : ");
String character = s1.next();
if (Character.isUpperCase(character.charAt(0))){
System.out.println("Upper Case");
}else if (Character.isLowerCase(character.charAt(0))){
System.out.println("Lower Case");
}else{
System.out.println("It is not characters.");
}
// d) Display two strings in alphabetical order ignoring their case.
System.out.println("d)");
String string1 = "QbjewqjWNFI",string2 = "qaoei12mvao";
if (string1.compareToIgnoreCase(string2)<0){
System.out.println(string1 + ", " + string2);
}else{
System.out.println(string2 + "," + string1);
}
// e) A switch statement that display Sunday, Monday, .., Saturday if the input is 0, 1, …, 6.
System.out.println("e)");
Scanner s2 = new Scanner(System.in);
System.out.print("Please input a number : ");
switch (s2.nextInt()+1){
case 1:
System.out.println("Sunday");
break;
case 2:
System.out.println("Monday");
break;
case 3:
System.out.println("Tuesday");
break;
case 4:
System.out.println("Wednesday");
break;
case 5:
System.out.println("Thursday");
break;
case 6:
System.out.println("Friday");
break;
case 7:
System.out.println("Saturday");
break;
default:
System.out.println("Wrong number");
}
// Q2
System.out.println("--------------------------------------------------------\n2.");
// a)
System.out.print("a)\nThe first line should be /if (num1 == num2)/\n");
// b)
System.out.print("b)\nThe first line should be /if(x>y && y>z)/\n");
// c)
System.out.print("c)\nThe second line should be /if(s1.equals(s2))/\nThe fourth line should be /else if (!s1.equals(s2))/\n");
// d)
System.out.print("d)\nThe first line should be /if (x>0||y>0)\n");
// Q3
System.out.println("--------------------------------------------------------\n3.");
// a)
System.out.println("a)\n#####\n$$$$$");
// b)
System.out.println("b)\n#####\n$$$$$");
// c)
System.out.println("c)\n$$$$$");
// d)
System.out.println("d)\nThere is no output.");
// Q4 Write the java statements that used the if statement to find the biggest number among three given integers.
System.out.println("--------------------------------------------------------\n4.");
Random r = new Random();
final int MAX = 100;
int given1 = r.nextInt(MAX);
int given2 = r.nextInt(MAX);
int given3 = r.nextInt(MAX);
System.out.println("given1 = "+ given1 + "\ngiven2 = " + given2+"\ngiven3 = "+given3);
if (given1 >given2 && given2>given3){
System.out.println("The biggest number is given1 : "+ given1);
}else if (given1>given2 && given2<=given3){
if (given1 > given3){
System.out.println("The biggest number is given1 : " + given1);
}else if (given1 < given3){
System.out.println("The biggest number is given3 : "+ given3);
}else{
System.out.println("The biggest number is given1 or given3 : " +given1);
}
}else if (given1 < given2 && given2 > given3){
System.out.println("The biggest number is given2 : " + given2);
}else if (given1 <= given2 && given2 < given3){
System.out.println("The biggest number is given3 : " + given3);
}else{
System.out.println("The three numbers are the same : " + given1);
}
// Q5 Write the java statements that determine whether the Leap year. A Leap year is divisible by 4 but not by 100. However, a Leap year is also divisible by 400.
System.out.println("--------------------------------------------------------\n5.");
System.out.print("Please input a year : ");
Scanner s3 = new Scanner(System.in);
int year = s3.nextInt();
if (year % 4 == 0){
if (year %100 != 0){
System.out.println(year + " is a Leap year.");
}else{
if (year %400 ==0){
System.out.println(year + " is a Leap year.");
}else{
System.out.println(year + " is not a Leap year.");
}
}
}else{
System.out.println(year + " is not a Leap year.");
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Q1
// a) Determine whether 3x8=27.
System.out.print("--------------------------------------------------------\n1.\na)\n");
final int x = 3, y = 8;
if (x*y==27){
System.out.println("3 x 8 = 27");
}else{
System.out.print(x +"*" + y + "=" + x*y +"\n");
}
// b) Determine whether an input integer is an odd number or even number.
System.out.println("b)");
int number;
Scanner s = new Scanner(System.in);
System.out.print("Please input a random integer : ");
try{
number = s.nextInt();
if (number % 2 == 0){
System.out.println("You inputted an even number.");
}else{
System.out.println("You inputted an odd number.");
}
}catch(Exception e){
System.out.println("Wrong value, exiting...");
}
// c) Determine whether a character is a capital letter.
Scanner s1 = new Scanner(System.in);
System.out.println("c)");
System.out.print("Please input a character : ");
String character = s1.next();
if (Character.isUpperCase(character.charAt(0))){
System.out.println("Upper Case");
}else if (Character.isLowerCase(character.charAt(0))){
System.out.println("Lower Case");
}else{
System.out.println("It is not characters.");
}
// d) Display two strings in alphabetical order ignoring their case.
System.out.println("d)");
String string1 = "QbjewqjWNFI",string2 = "qaoei12mvao";
if (string1.compareToIgnoreCase(string2)<0){
System.out.println(string1 + ", " + string2);
}else{
System.out.println(string2 + "," + string1);
}
// e) A switch statement that display Sunday, Monday, .., Saturday if the input is 0, 1, …, 6.
System.out.println("e)");
Scanner s2 = new Scanner(System.in);
System.out.print("Please input a number : ");
switch (s2.nextInt()+1){
case 1:
System.out.println("Sunday");
break;
case 2:
System.out.println("Monday");
break;
case 3:
System.out.println("Tuesday");
break;
case 4:
System.out.println("Wednesday");
break;
case 5:
System.out.println("Thursday");
break;
case 6:
System.out.println("Friday");
break;
case 7:
System.out.println("Saturday");
break;
default:
System.out.println("Wrong number");
}
// Q2
System.out.println("--------------------------------------------------------\n2.");
// a)
System.out.print("a)\nThe first line should be /if (num1 == num2)/\n");
// b)
System.out.print("b)\nThe first line should be /if(x>y && y>z)/\n");
// c)
System.out.print("c)\nThe second line should be /if(s1.equals(s2))/\nThe fourth line should be /else if (!s1.equals(s2))/\n");
// d)
System.out.print("d)\nThe first line should be /if (x>0||y>0)\n");
// Q3
System.out.println("--------------------------------------------------------\n3.");
// a)
System.out.println("a)\n#####\n$$$$$");
// b)
System.out.println("b)\n#####\n$$$$$");
// c)
System.out.println("c)\n$$$$$");
// d)
System.out.println("d)\nThere is no output.");
// Q4 Write the java statements that used the if statement to find the biggest number among three given integers.
System.out.println("--------------------------------------------------------\n4.");
Random r = new Random();
final int MAX = 100;
int given1 = r.nextInt(MAX);
int given2 = r.nextInt(MAX);
int given3 = r.nextInt(MAX);
System.out.println("given1 = "+ given1 + "\ngiven2 = " + given2+"\ngiven3 = "+given3);
if (given1 >given2 && given2>given3){
System.out.println("The biggest number is given1 : "+ given1);
}else if (given1>given2 && given2<=given3){
if (given1 > given3){
System.out.println("The biggest number is given1 : " + given1);
}else if (given1 < given3){
System.out.println("The biggest number is given3 : "+ given3);
}else{
System.out.println("The biggest number is given1 or given3 : " +given1);
}
}else if (given1 < given2 && given2 > given3){
System.out.println("The biggest number is given2 : " + given2);
}else if (given1 <= given2 && given2 < given3){
System.out.println("The biggest number is given3 : " + given3);
}else{
System.out.println("The three numbers are the same : " + given1);
}
// Q5 Write the java statements that determine whether the Leap year. A Leap year is divisible by 4 but not by 100. However, a Leap year is also divisible by 400.
System.out.println("--------------------------------------------------------\n5.");
System.out.print("Please input a year : ");
Scanner s3 = new Scanner(System.in);
int year = s3.nextInt();
if (year % 4 == 0){
if (year %100 != 0){
System.out.println(year + " is a Leap year.");
}else{
if (year %400 ==0){
System.out.println(year + " is a Leap year.");
}else{
System.out.println(year + " is not a Leap year.");
}
}
}else{
System.out.println(year + " is not a Leap year.");
}
}
}