java练习题求完整代码

java练习题求完整代码,第1张

按照题目要求编写的用javaBean规范设计的学生类Student的Java程序如下

需要创建user.java.test包,把Student.java文件和Test.java文件放入包中,编译Student.java文件并且编译运行Test.java文件得到运行结果

Student.java文件代码如下

package user.java.test

import java.io.Serializable

public class Student implements Serializable{

private static final long serialVersionUID = 1L

private String no

private String name

private double score

public Student(){}

public Student(String no,String name,double score){

this.no=no

this.name=name

this.score=score

}

public String getNo(){ return no}

public void setNo(String no){ this.no=no}

public String getName(){ return name}

public void setName(String name){ this.name=name}

public double getScore(){ return score}

public void setScore(double score){ this.score=score}

public String toString(){

return "学号:"+no+",姓名:"+name+",成绩:"+score

}

public static double getAvg(Student[] sArray){

double sum=0,avg

for(int i=0i<sArray.lengthi++){

sum=sum+sArray[i].getScore()

}

avg=sum/sArray.length

return avg

}

}

Test.java文件代码如下

package user.java.test

public class Test{

public static void main(String[] args){

Student[] sArray=new Student[5]

sArray[0]=new Student("001","张三",89.5)

sArray[1]=new Student("002","李四",82.5)

sArray[2]=new Student("003","王五",93)

sArray[3]=new Student("004","赵六",73.5)

sArray[4]=new Student("005","孙七",66)

System.out.println("这些学生的平均分:"+Student.getAvg(sArray))

for(int i=0i<sArray.lengthi++){

System.out.println(sArray[i].toString())

}

}

}

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)

}

}

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

运行结果:

按照你的要求编写的Java程序如下(每次的运行结果会因有随机数而有所不同)

public class M{

public static void main(String[] args){

int a[][]=new int[8][8]

int b[][]=new int[8][2]

int i,j,row,col

boolean flag

for(i=0i<8i++){

flag=false

row=(int)(Math.random()*8)

col=(int)(Math.random()*8)

//去掉重复的row,col

for(j=0j<8j++){

  if(row==b[j][0]-1 &&col==b[j][1]-1){

   flag=true

   i--

   break

  }

}

if(flag==false){

  b[i][0]=row+1

  b[i][1]=col+1

  a[row][col]=1

}

}

for(i=0i<8i++){

for(j=0j<8j++){

  if(j==7){

   System.out.print(a[i][j])

  }else{

   System.out.print(a[i][j]+" ")

  }

}

System.out.println()

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7730584.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-09
下一篇 2023-04-09

发表评论

登录后才能评论

评论列表(0条)

保存