1. 在IDE中输入以下代码,观察运行结果,并说明该段程序的作用。

public class MultiplicationTable {

         public static void main(String[] args) {

                   System.out.println(“                           Multiplication Table”);

                   System.out.print(“            ”);

                   for(int j = 1; j <= 9; j++)

                            System.out.print(“            ” + j);

                   System.out.println(“\n---------------------------------------------------------------------”);

 

                   for(int i = 1; j <= 9; i++){

                            System.out.print(i + “ | ”);

                            for(int j = 1; j <= 9; j++){

                                     System.out.printf(“%4d”, i * j);

}

System.out.println();

}

         }

}

 

2. 在IDE中输入以下代码,观察运行结果,并说明该段程序的作用。

public class TestSum{

         public static void main(String[] args){

                   float sum = 0;

                   for(float i = 0.01f; i <= 1,0f; i = i +0.01f)

                            sum += i;

                   System.out.println(“The sum is ” + sum);

         }

}

 

3. 在IDE中输入以下代码,观察运行结果,并说明该段程序的作用。

import java.util.Scanner;

public class GreatestCommonDivisor

{

   public static void main(String[] args){

       Scanner input = new Scanner(System.in);

       System.out.println("Please enter first integer:");

       int n1 = input.nextInt();

       System.out.println("Please enter second integer:");

       int n2 = input.nextInt();

       int gcd = 1;

       int k = 2;

       while(k <= n1 && k <= n2){

           if(n1 % k == 0 && n2 % k == 0)

                gcd = k;

           k++;

       }

       System.out.println("The greatest common divisor for " + n1 + " and " + n2 + " is " + gcd);

   }

}

 

4. 编写程序:

提示用户输入两个圆的中心坐标和各自的半径值,然后决定第二个圆是否在第一个圆内,还是和第一个圆重叠,如下图所示:

     Java语言程序设计课程实验题目 第三次实验

 

5. 编写程序:

计算下面数列的和值。

Java语言程序设计课程实验题目 第三次实验

 

6. 编写程序:

如果一个正整数等于除它本身之外其他所有除数之和,就称之为完全数。例如:6是完全数,因为6 = 1+2+3;下一个完全数是28 = 14+7+4+2+1。

10000以下的完全数有四个,请通过编写的程序找出其他的完全数。

 

7. 编写程序:

给定输入的字符串,将字符串中的单词顺序颠倒,但要保持单词的字符顺序。例如:给定input=“changchun university of science and technology”,输出output=“technology and science of university changchun”。解答过程中,要注意对特殊情况的处理,例如,输入为“   ”,即多个空格时,要输出“ ”,即一个空格。

 

 

1、

package org.cust.test3;

public class MultiplicationTable {
    public static void main(String[] args) {
        System.out.println("Multiplication Table");
        System.out.print(" ");
        for (int j = 1; j <= 9; j++)
            System.out.print(" " + j);
        System.out.println("\n---------------------------------------------------------------------");

        for (int i = 1; i<= 9; i++) {//此处变量命名错误,应该改为i
            System.out.print(i + " | ");
            for (int j = 1; j <= 9; j++) {
                System.out.printf("%4d", i * j);
            }
            System.out.println();
        }
    }
}
//此程序是九九乘法表表格

2、

package org.cust.test3;

public class TestSum{
    public static void main(String[] args){
        float sum = 0;
        for(float i = 0.01f; i <= 10f; i = i +0.01f)
            sum += i;
        /*
         * 此程序是定义i为0.01的浮点数
         * 当i是<=10的浮点数时,就自增0.01
         * 然而10f中1与0之间多了个,
         * 程序结果就是0.01到10的浮点数等差为0.01的数列的累加结果
         */
        System.out.println("The sum is " + sum);
    }
}
 

3、

package org.cust.test3;
import java.util.Scanner;

public class GreatestCommonDivisor {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Please enter first integer:");
        int n1 = input.nextInt();
        System.out.println("Please enter second integer:");
        int n2 = input.nextInt();
        int gcd = 1;
        int k = 2;
        while (k <= n1 && k <= n2) {
            if (n1 % k == 0 && n2 % k == 0)
                gcd = k;
            k++;
        }
        System.out.println("The greatest common divisor for " + n1 + " and " + n2 + " is " + gcd);
    }
    //此段程序是求输入的两个数的最大公约数
}
 

4、

package org.cust.test3;

import java.util.Scanner;

public class Test_1 {
    private double x1, x2, y1, y2, r1, r2;

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        Test_1 t = new Test_1();
        while (true) {
            System.out.println("请依次输入第一个圆的中心坐标(x1,y1)与半径值r1:");
            t.x1 = in.nextDouble();
            t.y1 = in.nextDouble();
            t.r1 = in.nextDouble();
            System.out.println("请依次输入第二个圆的中心坐标(x2,y2)与半径值r2:");
            t.x2 = in.nextDouble();
            t.y2 = in.nextDouble();
            t.r2 = in.nextDouble();
            t.judge(t);

        }
    }

    public void judge(Test_1 t) {
        double sqr = 0;
        sqr = Math.sqrt(Math.pow((t.x1 - t.x2), 2) + Math.pow((t.y1 - t.y2), 2));
        if (sqr > (t.r1 + t.r2)) {
            System.out.println("此二圆的关系是:相离");
        } else if (sqr == (t.r1 + t.r2)) {
            System.out.println("此二圆的关系是:相切");
        } else if (sqr < (t.r1 + t.r2)) {
            System.out.println("此二圆的关系是:相交(重叠)");
        } else if (sqr == Math.abs(t.r1 - t.r2)) {
            System.out.println("此二圆的关系是:内切");
        } else {
            System.out.println("此二圆的关系是:内含(圆内)");
        }

    }
}
 

5、

package org.cust.test3;

public class Test_2 {

    public static void main(String[] args) {
        double sum = 0;
        for (double i = 1; i <= 99; i += 2) {
            sum +=(i / (i + 2));
            i += 2;
        }
        System.out.println("数列的和值是:" + sum);
    }
}
 

6、

package org.cust.test3;

import java.util.Scanner;

public class Test_3 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入要求完全数的范围:");
        int n = in.nextInt();
        System.out.println(n + "以内的完全数有如下:");
        judge(n);
    }

    public static void judge(int n) {
        for (int i = 1; i <= n; i++) {
            int sum = 0;
            for (int j = 1; j < i; j++) {
                if (i % j == 0) {
                    sum += j;
                }
            }
            if (sum == i) {
                System.out.print(sum + " ");
            }
        }
    }
}
 

7、

package org.cust.test3;

import java.util.Scanner;

public class Test_4 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print(" input=");
        String s = in.nextLine();
        System.out.print("output=");
        if (s.trim().isEmpty()) {
            System.out.print(" ");
            System.out.println("\n" + "全空格输出一个空格,此行为输出验证");
        } else {
            s = s.replaceAll(" +", " ");
            System.out.println(s);
            String[] sPart = s.split(" ");

            for (int i = 1; i <= sPart.length; i++) {
                System.out.print(sPart[sPart.length - i] + " ");
            }
        }
    }

}
 

相关文章:

  • 2021-05-16
  • 2021-10-01
  • 2021-09-21
  • 2021-11-04
  • 2022-02-10
  • 2022-01-13
  • 2022-12-23
  • 2021-08-21
猜你喜欢
  • 2021-11-09
  • 2021-12-01
  • 2022-01-20
  • 2021-12-23
  • 2021-08-02
  • 2021-05-30
  • 2021-12-17
相关资源
相似解决方案