用JAVA语言编写一个程序,要求如下:

Python017

用JAVA语言编写一个程序,要求如下:,第1张

import java.util.Random

import java.util.Scanner

public class T {

public static void main(String[] args) throws Exception {

Scanner in = new Scanner(System.in)

int difficulty//难度

int mode//运算类型

int answer//答案

int amount//挑战题目数量

int score = 0//得分

System.out.println("请输入难度(1:一位数、2:两位数、3:三位数):")

difficulty = in.nextInt()

System.out.println("请输入运算类型(1:加、2:减、3:乘、4:除):")

mode = in.nextInt()

System.out.println("请输入想要挑战的题目数量:")

amount = in.nextInt()

Random random = new Random()

for (int i = 0i <amounti++) {

if (difficulty == 1) {

if (mode == 1) {

int x = random.nextInt(10)

int y = random.nextInt(10)

System.out.println("第" + i + "题:")

System.out.print(x + " + " + y + " = ")

answer = in.nextInt()

if (answer == (x + y)) {

System.out.println("答对了\n")

score++

} else {

System.out.println("答错了,答案是:" + (x + y) + "\n")

}

} else if (mode == 2) {

int x = random.nextInt(10)

int y = random.nextInt(10)

System.out.println("第" + i + "题:")

System.out.print(x + " - " + y + " = ")

answer = in.nextInt()

if (answer == (x - y)) {

System.out.println("答对了\n")

score++

} else {

System.out.println("答错了,答案是:" + (x - y) + "\n")

}

} else if (mode == 3) {//乘法

} else if (mode == 4) {//除法 考虑小数的问题

} else {

throw new Exception("运算类型输入值不合法")

}

} else if (difficulty == 2) {

if (mode == 1) {

int x = random.nextInt(100)

int y = random.nextInt(100)

System.out.println("第" + i + "题:")

System.out.print(x + " + " + y + " = ")

answer = in.nextInt()

if (answer == (x + y)) {

System.out.println("答对了\n")

score++

} else {

System.out.println("答错了,答案是:" + (x + y) + "\n")

}

} else if (mode == 2) {

} else if (mode == 3) {//乘法

} else if (mode == 4) {//除法 考虑小数的问题

} else {

throw new Exception("运算类型输入值不合法")

}

} else if (difficulty == 3) {

if (mode == 1) {

int x = random.nextInt(1000)

int y = random.nextInt(1000)

System.out.println("第" + i + "题:")

System.out.print(x + " + " + y + " = ")

answer = in.nextInt()

if (answer == (x + y)) {

System.out.println("答对了\n")

score++

} else {

System.out.println("答错了,答案是:" + (x + y) + "\n")

}

} else if (mode == 2) {

} else if (mode == 3) {//乘法

} else if (mode == 4) {//除法 考虑小数的问题

} else {

throw new Exception("运算类型输入值不合法")

}

} else {

throw new Exception("难度输入值不合法")

}

}

System.out.println("挑战结束,您的分数为:" + score)

}

}

我就只举了加法的例子,其他运算的写法都是类似的,你照葫芦画瓢即可

运行结果:

有一个比较经典的多态实例:

有一个Animal类,它有Cat,和Dog两个子类,在Animal中有个say方法,当Cat调用这个方法的时候输出的是“小猫喵喵喵”,当Dog调用这个方法时,输出的是“小狗汪汪汪”,这就是Java多态的实现。

1、定义一种动物,该类型的动物有叫的属性。

2、分别定义猫,狗,鸟,都继承于该动物,都有叫的属性。

3、分别表示出各个具体小动物的叫声,例如猫的叫声:喵、狗的叫声:汪、鸟的叫声:咻,点是叫声,实现各个具体小动物用的叫声的方法是用的同一个函数名称,就是动物的叫声函数。

多态:

这个案例网上是没有的,属于无忌独创,当时在帮孩子辅导作业,小学科学,里面有一点内容是关于人的牙齿,说牙齿分为:门齿、犬齿臼齿

问闺女,为什么这么分呢?闺女说牙齿虽然都是用来咬食物,但是食物种类很多,咬碎需要的工具也不一样,门齿用来切割食物,如:苹果、梨;犬齿用来撕碎食物。

如肉类;臼齿用来磨碎食物,如犬齿撕碎的肉类,就需要再用臼齿来磨碎,然后送到食道,胃,小肠,大肠进行消化。我恍然大悟,这不就是Java面向对象里的多态吗?多完美啊。

这也很好说明了为什么会有多态出现,因为生活中就存在太多这种例子,所以需要多态来匹配解决。

package test

public class Student {

private String name

private String id

private String clazz

private int age

private String address

/**

* sayHello方法

*/

public void sayHello() {

System.out.println("学号为" + this.id + "的同学的具体信息如下:")

System.out.println("姓名:" + this.name)

System.out.println("班级:" + this.clazz)

System.out.println("年龄:" + this.age)

System.out.println("家庭住址:" + this.address)

}

/**

* 测试方法

*

* @param args

*/

public static void main(String[] args) {

// 第一问

Student student = new Student()

student.setAddress("百度知道")

student.setAge(1)

student.setClazz("一班")

student.setId("071251000")

student.setName("lsy605604013")

student.sayHello()

// 第二问

Student studentNew = new Student()

studentNew.setAddress("搜搜知道")

studentNew.setAge(2)

studentNew.setClazz("二班")

studentNew.setId("071251001")

studentNew.setName("lady")

if (student.getAge() <studentNew.getAge())

studentNew.sayHello()

else if (student.getAge() >studentNew.getAge())

student.sayHello()

else

System.out.println("两人一样大")

}

public String getName() {

return name

}

public void setName(String name) {

this.name = name

}

public String getId() {

return id

}

public void setId(String id) {

this.id = id

}

public String getClazz() {

return clazz

}

public void setClazz(String clazz) {

this.clazz = clazz

}

public int getAge() {

return age

}

public void setAge(int age) {

this.age = age

}

public String getAddress() {

return address

}

public void setAddress(String address) {

this.address = address

}

}