1、有一个学生类的结构如下:
class Student {
private int no;
private String name;
private int score;
public Student(int _no, String _name, int _score) {
no = _no;
name = _name;
score = _score;
}
public int getNo() {return no;}
public String getName() {return name;}
public int getScore() {return score;}
public void print(){
System.out.println(no + " "+name+" "+score);
}
}
请构造main函数完成如下功能:
从键盘中读入三个学生的信息,比较他们的成绩,按照成绩由高到低排列输出
输入描述:
三个学生的学号、姓名、成绩
输出描述:
由高到低排列输出的三个学生信息
裁判测试程序样例:
/*你的代码被嵌在这里*/
class Student {
private int no;
private String name;
private int score;
public Student(int _no, String _name, int _score) {
no = _no;
name = _name;
score = _score;
}
public int getNo() {return no;}
public String getName() {return name;}
public int getScore() {return score;}
public void print(){
System.out.println(no + " "+name+" "+score);
}
}
输入样例:
在这里给出一组输入。例如:
1 wang 89
2 liu 78
3 ma 90
输出样例:
在这里给出相应的输出。例如:
3 ma 90
1 wang 89
2 liu 78
题解:(用简单的定义结构即可完成解答)
import java.util.Scanner;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
Student stu1 = new Student(sc.nextInt(),sc.next(),sc.nextInt());
Student stu2 =new Student(sc.nextInt(),sc.next(),sc.nextInt());
Student stu3 = new Student(sc.nextInt(),sc.next(),sc.nextInt());
if(stu1.getScore()>=stu2.getScore()&&stu1.getScore()>= stu3.getScore()) {
stu1.print();
if (stu2.getScore() >= stu3.getScore()) {
stu2.print();
stu3.print();
} else {
stu3.print();
stu2.print();
}
}
else if(stu2.getScore()>=stu1.getScore()&&stu2.getScore()>= stu3.getScore()){
stu2.print();
if(stu1.getScore()>= stu3.getScore()){
stu1.print();
stu3.print();
}
else{
stu3.print();
stu1.print();
}
}
else{
stu3.print();
if(stu1.getScore()>=stu2.getScore()){
stu1.print();
stu2.print();
}
else{
stu2.print();
stu1.print();
}
}
}
}
总结:含参函数需要传参,用Scanner即可完成。主要把if-else语句写清楚即可。
2.汽车类
编写汽车类,其功能有启动(start),停止(stop),加速(speedup)和减速(slowDown),启动和停止可以改变汽车的状态(on/off),初始时状态为off,速度为0,speedUp和slowDown可以调整汽车的速度,每调用一次汽车速度改变10(加速增10,减速减10),汽车启动后才能加减速,加速上限为160,减速下限为0,汽车速度减为0后才能停止,给出汽车类的定义。
Main函数中构造一个汽车对象,并对该对象进行操作,各个操作的编号为:
start
stop
speedup
slowdown
操作完成后打印出汽车的状态和速度。
输入描述:
操作
输出描述:
汽车的状态和速度
裁判测试程序样例:
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
Car c = new Car();
for (int i=0;i<n;i++) {
int a = s.nextInt();
switch (a) {
case 1: c.start(); break;
case 2: c.stop(); break;
case 3: c.speedUp(); break;
case 4: c.slowDown(); break;
}
}
System.out.print(c.status + " ");
System.out.println(c.speed);
}
}
/* 你的代码被嵌在这里 */
输入样例:
在这里给出一组输入。例如:
8
1 3 3 4 3 4 4 2
输出样例:
在这里给出相应的输出。例如:
off 0
题目分析得:只需编写一个汽车类,
题解如下:四个小函数即可解决。
class Car{
int speed =0;
String status ;
public void start(){
status ="on";
}
public void stop(){
status ="off";
}
public void speedUp(){
if(status =="on"&&speed<=160){
speed =speed+10;
}
}
public void slowDown(){
if(speed>0){
speed =speed-10;
}
}
}
3.游客类
定义游客类(其测试类已给出),要求如下:
一个景区根据游人的年龄收取不同价格的门票。请编写游客类,根据年龄段决定能够购买的门票价格并输出
当输入的年龄小于18或者大于60(不包括18,60)时,程序输出:
年龄为:XX,免费
当输入的年龄在18与60之间时,程序输出
年龄为:XX,价格为20元
裁判测试程序样例:
/* 请在这里填写答案 */
//使用该类创建使用对象的测试类如下:
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Tourist v = new Tourist();
v.age = input.nextInt();
v.show();
}
}
输入样例1:
18
输出样例1:
年龄为:18,价格为20元
输入样例2:
15
输出样例2:
年龄为:15,免费
题解:此题不难,很基础的类定义,只要注意输出逗号。
import java.util.Scanner;
class Tourist{
int age;
public void Tourist(){
int age;
}
public int age(){
return age;
}
public void show(){
if(age<18||age>60){
System.out.println("年龄为:"+age+",免费");
}
else if(age>=18&&age<=60){
System.out.println("年龄为:"+age+",价格为20元");
}
}
}
4.长方形类面积体积
设计一个名为Rectangle的类表示矩形。这个类包括:
两个名为width和height的double型数据域,它们分别表示矩形的宽和高。width和height的默认值都为1.
一个无参构造方法。
一个为width和height指定值的矩形构造方法。
一个名为getArea()的方法返回这个矩形的面积。
一个名为getPerimeter()的方法返回这个矩形的周长。
类名为:
Rectangle
裁判测试程序样例:
import java.util.Scanner;
/* 你的代码将被嵌入到这里 */
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double w = input.nextDouble();
double h = input.nextDouble();
Rectangle myRectangle = new Rectangle(w, h);
System.out.println(myRectangle.getArea());
System.out.println(myRectangle.getPerimeter());
input.close();
}
}
输入样例:
3.14 2.78
输出样例:
8.7292
11.84
题解:类定义,掌握无参构造,和有参构造即可完成本题
class Rectangle{
double width,heigth;
public void Rectangle(){
width=1;
heigth =1;
}
Rectangle(double width,double heigth){
this.width=width;
this.heigth=heigth;
}
double getArea(){
return width*heigth;
}
double getPerimeter(){
return 2*(width+heigth);
}
}
欢迎大家指正,提供更好的见解。