Q2.1

Write a Java program that compares two Strings. The two Strings should be inputted using user input.
(i): If the two Strings are exactly the same, a message should be printed to the screen which reads “Exactly the same: True”, otherwise, a message should be printed to the screen which reads “Exactly the same: False”.
(ii): If and only if the first comparison was false, a comparison should be done that ignores case sensitivity. If they are the same, while ignoring the case, a message should be printed to the screen which reads “Similar: True”, otherwise, a message should be printed to the screen which reads “Similar: False”.

package A2018;

import java.util.*;

public class Q21 {
    public static void main(String[] args) {
        // 1
        Scanner in = new Scanner(System.in);
        String str1 = in.next();
        String str2 = in.next();
        in.close();
        if (str1.equals(str2)) {
            System.out.println("Exactly the same: True");
        } else {
            System.out.println("Exactly the same: False");
            // 2
            if (str1.equalsIgnoreCase(str2)) {
                System.out.println("Similar: True");
            } else {
                System.out.println("Similar: False");
            }
        }
    }
}

Q2.2

Write a Java program that will: (i): Create a 2D array of a size that is decided by user input – the first number relates to the number of rows and the second number relates to the number of columns. (ii): Get the user to populate the array with integers. (iii): Print to the screen the total number of odd numbers in the array. (iv): Calculate the sum of all the odd numbers in the array and print the answer to the screen. (v): Print the contents of the array to the screen with each row printed on a line of its own. (v): Determine which row has the largest sum and print this sum to the screen along with the row number.

package A2018;

import java.util.*;

public class Q22 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 1
        int row = in.nextInt();
        int col = in.nextInt();
        int[][] array = new int[row][col];
        // 2
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                array[i][j] = in.nextInt();
            }
        }
        // 3/4
        int sum = 0;
        int numood = 0;
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                if (array[i][j] % 2 != 0) {
                    sum += array[i][j];
                    numood++;
                }
            }
        }
        System.out.println(numood);
        System.out.println(sum);
        // 5
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(" " + array[i][j]);
            }
            System.out.println();
        }
        // 6
        int max = 0;
        for (int i = 0; i < array.length; i++) {
            int lowsum = 0;
            for (int j = 0; j < array[i].length; j++) {
                lowsum += array[i][j];
            }
            if (lowsum > max) {
                max = lowsum;
            }
        }
        System.out.println(max);
    }
}

Q2.3

Write a Java program that: (i): Asks the user to enter an integer number. (ii): Determines and displays a message to indicate if the given input is an even number or an odd number. (iii): Determines and displays if the number is a prime number. A prime number is a number that is divisible ONLY by itself and 1. If the number is not a prime number the program should output one number that divides in to the inputted number, otherwise it should print a message saying it is not prime and state that it is only divisible by itself and one.
For example, some sample outputs from your program would be as follows:
6 is not a prime number. It is divisible 3.
12 is not a prime number. It is divisible 6.
7 is a prime number as it is only divisible by 1 and 7.
13 is a prime number as it is only divisible by 1 and 13.

package A2018;
import java.util.*;

public class Q23 {
    public static void main(String[] args) {
        // 1
        Scanner in = new Scanner(System.in);
        int num = in.nextInt();
        in.close();
        // 2
        if (num % 2 == 0) {
            System.out.println("even");
        } else {
            System.out.println("odd");
        }
        // 3,内部迭代
        boolean flag = false;
        for (int i = num-1; i >=0; i--) {//素数判断注意从2开始,不能包括本身
            if (num % i == 0) {
                System.out.println(num + " is not a prime number.It is divisible " + i);
                flag = true;
                break;
            }
        }
        if (flag = false) {//这样谢还避免了1的情况
            System.out.println(num + " is a prime number as it is only divisible by 1 and " + num);
        }
    }
}
最后修改日期:2019年12月28日

作者

留言

撰写回覆或留言

发布留言必须填写的电子邮件地址不会公开。