Java习题作业

    xiaoxiao2022-06-29  77

    1.  Write a complete program that prints numbers from 1 to 50, but if numbers that are multiples of 5, print HiFive, else if numbers that are divisible by 2, print HiTwo, and else if numbers that are divisible by 3 or 7, print HiThreeOrSeven.

    public class Prints { public static void main(String[] args) { for (int i = 1;i <=50;i++) { if (i % 5 == 0) System.out.println("HiFive"); else if (i % 2 == 0) System.out.println("HiTwo"); else if (i % 3 == 0 || i % 7 == 0) System.out.println("HiThreeOrSeven"); else System.out.println(i + " "); } } }

    2.Write a loop that computes 100 / 1 + 99 / 2 + 98  / 3 + ... + 2 / 99 + 1 / 100.

    public class LoopComputes { public static void main(String[] args) { double sum = 0; for (int i = 0; i <= 99; i++) { sum += (100.0 - i) / (i + 1.0); } System.out.println("Sum is " + sum); } } 3. Write aprogram that counts the number of lowercase letters in a string. Your programshould prompt the user to enter the string and display the count.

    import java.util.Scanner; public class LowercaseCounts { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Please enter a string:"); String str = input.next(); int lowerNum = 0; for (int i = 0;i < str.length();i++) { if ((int)str.charAt(i) >= 97 && (int)str.charAt(i) <= 122) { lowerNum += 1; } } System.out.println("Lowercase counts:" + lowerNum); } }

    4.Write a method tocompute the following series:

    m(i) = (1 / 3) + (2 / 5) + ... + (i / (2 * i +1))

    i m(i) 1 0.3333 2 0.7333 . . . 19 8.7602 20 9.2480

    Here is the outline of the program:

    public class Test { public static void main(String[] args) { // Fill in the code here } public double m(int i) { // Fill in the code here } } import java.util.*; public class MethodCompute { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Please enter the number of items to be calculated:"); int num = input.nextInt(); double sum = MethodCompute.compute(num); System.out.println((int)(sum * 10000.0) / 10000.0); } private static double compute(int num) { double sum = 0.0; for (int i = 1;i <= num; i++) { sum += (double)i / (2.0 * i + 1.0); } return sum; } } 5.Implement the following method that returns a suffix ofan integer with a specified number of digits.

    /** For example, getSuffix(12345, 2) returns 45 andgetSuffix(1234567, 3) returns 567 

    import java.util.*; public class SpecifiedNumber { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter an integer and the number of bits required:"); // 输入一个整数和需要后几位数 int num = input.nextInt(); int count = input.nextInt(); int number = SpecifiedNumber.getSuffix(num, count); System.out.println(number); } public static int getSuffix(int value, int numberOfDigits) { String str = String.valueOf(value); // 整数转字符串 String str1 = str.substring(str.length()-numberOfDigits,str.length()); // 截取后几个字符 int i = Integer.parseInt(str1); // 截取到的字符串转整数 return i; } } 6.Write a program that reads the integers between1 and 50 and counts the occurrences of each. Assume the input ends with 0.

    /* * 计算小于等于50个整数之和,以0结束,输入一次回车一次 */ import java.util.*; public class IntegersSum { public static void main(String[] args) { int i = 1; int j = 0; int sum = 0; System.out.println("Calculation is less than the sum of 50 integers, with the end of 0, enter a carriage return once:"); while (i != 0 && j < 50) { Scanner input = new Scanner(System.in); i = input.nextInt(); sum += i; j++; } System.out.println(sum); } } 7.Write a program that prompts the user to enter10 integers and displays all combinations of picking two numbers from 10.

    import java.util.*; import org.omg.CORBA.INTERNAL; public class Combination { public static void main(String[] args) { int []num = new int[10]; Scanner input = new Scanner(System.in); System.out.println("Please enter 10 integers:"); for(int i =0; i<10;i++){ num[i]=input.nextInt(); } for (int i = 0; i < 9;i++) { for (int j = 1;j < 10;j++) { System.out.println(num[i]+""+num[j] + " " +num[j]+""+num[i]); } } } } 8.Write a method that returns a sorted string using thefollowing method:

      Public staticString sort(String s){}

      For example,sort(“acb”) returns abc

      Write a testprogram that prompts the user  to enter astring and displays the sorted string.

    import java.util.*; public class StringSort { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Please enter a string:"); String str = input.next(); String str1 = StringSort.sort(str); System.out.println("Sort after:" + str1); } public static String sort(String s){ char[] arrayCh = s.toCharArray(); // 字符串转字符数组 Arrays.sort(arrayCh); // 利用数组帮助类自动排序 return new String(arrayCh); } }

     

    转载请注明原文地址: https://ju.6miu.com/read-1124915.html

    最新回复(0)