java编程题花园修路,java考试可柳能性较高的编程题.doc

java考试可柳能性较高的编程题

PAGE

PAGE 16

2、编写程序将用户输入的一个0~100之间的分数,将其转化成1~5的5分计方法。

90~100: 5

80~90: 4

70~80: 3

60~70: 2

其他: 1

import java.util.Scanner;

public class ScoreClass {

public static void main(String[] args) {

Scanner s=new Scanner(System.in);//如何接收一个值

System.out.print("input a int(0-100):");

int score=s.nextInt(),n=-1;

if(score>100){//如何来分等级

System.out.println("Error");

}else if(score>=90){

n=5;

}else if(score>=80){

n=4;

}else if(score>=70){

n=3;

}else if(score>=60){

n=2;

}else if(score>=0){

n=1;

}else{

System.out.println("Error");

}

if(n!=-1){

System.out.println("n="+n);

}

}

}

3、使用Java.lang.Math类,生成100个0~99之间的随机数(整数),找出它们之中的最大者及最小者,并统计大于50的整数个数

public class MaxMin {

public static void main(String[] args) {

double p,temp,max=-1,min=1000;

int i,j=0;

for(i=0;i<100;i++){

p=Math.random()*100;

temp=Math.floor(p);//0~1

if(temp>max)

max=temp;

if(temp

min=temp;

if(temp>=50)

j++;

}

System.out.println("max="+max);

System.out.println("min="+min);

System.out.println("total="+j);

}

}

1、编程实现矩形类Rectangle,Rectangle拥有私有属性长(length)和宽(width),为Rectangle类提供至少两种构造方法,同时提供计算矩形周长(perimeter())和面积(area())的方法。使用两种构造方法,至少各创建一个矩形对象实例,并分别计算两个矩形的周长和面积。

public class Rectangle {

private double width;

private double length;

public Rectangle(){

this.width=5;

this.length=6;

}

public Rectangle(double w,double l){

this.width=w;

this.length=l;

}

public double area(){//求面积

return this.width*this.length;

}

public double perimeter(){//求周长

return 2*(this.width+this.length);

}

public static void main(String[] args) {

Rectangle r1=new Rectangle();

System.out.println("r1的周长:"+r1.perimeter());

System.out.println("r1的面积:"+r1.area());

System.out.println();

Rectangle r2=new Rectangle(10,20);

System.out.println("r2的周长:"+r2.perimeter());

System.out.println("r2的面积:"+r2.area());

}

public double getWidth() {

return width;

}

public void setWidth(double width) {

this.widt

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值