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