Java上机操作练习题-助力期末
全栈自学社区 提供 更多精彩关注公众号1.运行时输入一个数n, 求1+2!+3!+…+n!的和;public static void main(String[] args) {Scanner scan = new Scanner(System.in);int n = scan.nextInt();int add = facadd(n);System.out.println(add);}public
·
全栈自学社区 提供 更多精彩关注公众号
1. 运行时输入一个数n, 求1+2!+3!+…+n!的和;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int add = facadd(n);
System.out.println(add);
}
public static int facadd(int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
int ret = 1;
for (int j = 1; j <= i; j++) {
ret *= j;
}
sum += ret;
}
return sum;
}
2. 使用for循环打印九九乘法表;
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
int sum = i * j;
System.out.print(j + "*" + i + "=" + sum + "" + "\t");
}
System.out.println();
}
}
3. 编写一个应用程序求100以内的全部素数;
public static void main(String args[]) {
int i, j;
for (i = 2; i <= 100; i++) {
for (j = 2; j <= i / 2; j++) {
if (i % j == 0)
break;
}
if (j > i / 2) {
System.out.println("" + i + "是素数");
}
}
}
4. 请实现对一整型数序列的排序操作算法(冒泡);
public static int[] bubbleSort(int[] array) {
if (array.length == 0)
return array;
for (int i = 0; i < array.length; i++)
for (int j = 0; j < array.length - 1 - i; j++)
if (array[j + 1] < array[j]) {
int temp = array[j + 1];
array[j + 1] = array[j];
array[j] = temp;
}
return array;
}
}
5. 使用面向对象思想编写猜数字游戏:
一个类A有一个成员变量v,设置一个随机初值。
定义一个类,对A类的成员变量v进行猜。
如果大了则提示大了,小了则提示小了。等于则提示猜测成功。
public class TestE {
class A {
int v = (int) (Math.random() * 10);
public int getV() {
return v;
}
public void setV(int v) {
this.v = v;
}
}
class B {
int c = (int) (Math.random() * 10);
A a = new TestE().new A();
void guss() {
if (c > a.getV()) {
System.out.println("猜大了");
} else if (c == a.getV()) {
System.out.println("成功");
} else {
System.out.println("猜小了");
}
}
}
public static void main(String[] args) {
B b = new TestE().new B();
b.guss();
}
}
6. 已知猫类和狗类:
属性:毛的颜色,腿的个数
行为:吃饭
猫特有行为:抓老鼠catchMouse
狗特有行为:看家lookHome
利用面向对象继承法来做
package four;
public class Animal {
String color;
int leg;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getLeg() {
return leg;
}
public void setLeg(int leg) {
this.leg = leg;
}
@Override
public String toString() {
return "Dog [color=" + color + ", leg=" + leg + "]";
}
void eat() {
System.out.println("吃饭");
}
}
Cat
public class Cat {
void catchMouse() {
System.out.println("抓老鼠");
}
}
Dog
public class Dog {
void lookHome() {
System.out.println("看家");
}
}
7. 编写一个程序,用于输出起始部分如下的Fibonacci数列1,1,2,3,5,8,13,21,34,55
public class TestF {
public static long fibonacci(long number) {
if ((number == 0) || (number == 1))
return number;
else
return fibonacci(number - 1) + fibonacci(number - 2);
}
public static void main(String[] args) {
for (int counter = 0; counter <= 10; counter++) {
System.out.printf("Fibonacci of %d is: %d\n", counter, fibonacci(counter));
}
}
}
8. 编写程序输入5学生的姓名和年龄并打印出来,同时求出平均年龄
Student
package four;
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
StudentMain
public class StudentMain {
public static void main(String[] args) {
Student[] myStu = new Student[5];
for (int i = 0; i < 5; i++) {
myStu[i] = new Student();
}
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 5; i++) {
System.out.println("请输入第" + (i + 1) + "个学生的姓名");
String s = sc.nextLine();
myStu[i].setName(s);
}
for (int i = 0; i < 5; i++) {
System.out.println("请输入第" + (i + 1) + "个学生的年龄");
String s = sc.nextLine();
int a = Integer.parseInt(s);
myStu[i].setAge(a);
}
for (int i = 0; i < 5; i++) {
System.out.println("第" + (i + 1) + "个学生的姓名是" + myStu[i].getName() + ",年龄是" + myStu[i].getAge());
System.out.println("平均年龄" + (myStu[0].getAge() + myStu[1].getAge() + myStu[2].getAge() + myStu[3].getAge()
+ myStu[4].getAge()) / 5);
}
}
}
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献5条内容
所有评论(0)