根据输入的正整数y所代表的年份,计算输出该年份是否为闰年
闰年的判断标准:
能够被4整除且不能被100整除的年份
或者能够被400整除的年份
输入n取值范围是 【1..3000】
是闰年,输出 yes
非闰年,输出 no
在这里给出一组输入。例如:
100
在这里给出相应的输出。例如:
no
答案代码如下:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int leap=sc.nextInt();
if((leap%4==0&&leap%100!=0)||(leap%400==0)) {
System.out.println("yes");
}else {
System.out.println("no");
}
}
}
编写程序,提示用户输入月份和年份,然后显示这个月的天数。
输入任意符合范围(1月~12月)的月份和(1900年~9999年)年份,且两个值之间空格分隔。
输出给定年份和月份的天数。
2 2000
29
答案代码如下:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int month=sc.nextInt();
int year=sc.nextInt();
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) {
System.out.println(31);
}
if(month==4||month==6||month==9||month==11) {
System.out.println(30);
}
if(month==2) {
if((year%4==0&&year%100!=0)||(year%400==0)) {
System.out.println(29);}
else {
System.out.println(28);
}
}
}
}
编写一个应用程序,读取用户任意输入的3个非零数值,判断它们是否可以作为直角三角形的3条边,如果可以,则打印这3条边,计算并显示这个三角形的面积。
输入三个非零数值。
如果不是直角三角形的三条边,则输出0.0;否则,输出三角形的面积
3
4
5
6.0
答案代码如下:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
if(a*a+b*b==c*c||a*a+c*c==b*b||c*c+b*b==a*a) {
System.out.println(a*b/2);
} else {
System.out.println(0.0); }
}
}
本题要求编写程序,将输入的n个整数存入数组a中,然后计算这些数的平均值,再输出所有大于平均值的数。
输入在第1行中给出一个正整数n(1≤n≤10),第2行输入n个整数,其间以空格分隔。题目保证数据不超过长整型整数的范围。
输出在第1行给出平均值,保留2位小数。在第2行输出所有大于平均值的数,每个数的后面有一个空格;如果没有满足条件的数,则输出空行。
如果输入的n不在有效范围内,则在一行中输出"Invalid."。
10
55 23 8 11 22 89 0 -1 78 186
在这里给出相应的输出。例如:
47.10
55 89 78 186
0
在这里给出相应的输出。例如:
Invalid.
答案代码如下:
import java.util.Scanner;
public class Main {
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
if(n<1||n>10) {
System.out.println("Invalid.");
}
int a[]=new int[n];
double sum=0;
for(int i=0;iaver)
System.out.print(a[i]+" ");
}
}
}
定义一个有关人的Person
类,内含属性:String name
、int age
、boolean gender
、int id
,所有的变量必须为私有(private
)。
注意:属性顺序请严格按照上述顺序依次出现。
name,age,gender,id
格式输出依次对name,age,gender
赋值。
按照格式:类名 [name=, age=, gender=, id=]
输出。建议使用Eclipse自动生成.
逆序
输出。3
a 11 false
b 12 true
c 10 false
Person [name=c, age=10, gender=false, id=0]
Person [name=b, age=12, gender=true, id=0]
Person [name=a, age=11, gender=false, id=0]
This is constructor
null,0,false,0
Person [name=null, age=0, gender=false, id=0]
答案代码如下:
import java.util.Scanner;
class Person{
private String name;
private int age;
private boolean gender;
private int id;
public Person() {
System.out.println("This is constructor");
System.out.println(name+","+age+","+gender+","+id);
}
public Person(String name,int age,boolean gender) {
this.name=name;
this.age=age;
this.gender=gender;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", gender=" + gender + ", id=" + id + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isGender() {
return gender;
}
public void setGender(boolean gender) {
this.gender = gender;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
Person per[]=new Person[n];
for(int i=0;i=0;i--) {
System.out.println(per[i]);
}
Person per1=new Person();
System.out.println(per1.toString());
}
}
定义一个有关学生的Student类,内含类成员变量:
String name、String sex、int age,所有的变量必须为私有(private)。
1.编写有参构造函数:
能对name,sex,age赋值。
2.覆盖toString函数:
按照格式:类名 [name=, sex=, age=]输出。使用idea自动生成,然后在修改成该输出格式
3.对每个属性生成setter/getter方法
4.main方法中
•输入1行name age sex , 调用上面的有参构造函数新建对象。
输入样例:
tom 15 male
输出样例:
Student [name='tom', sex='male', age=15]
答案代码如下:
import java.util.Scanner;
class Student{
private String name;
private String sex;
private int age;
public Student(String name,int age,String sex) {
this.name=name;
this.sex=sex;
this.age=age;
}
@Override
public String toString() {
return "Student [name='" + name + "', sex='" + sex + "', age=" + age + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String name=sc.next();
int age=sc.nextInt();
String sex=sc.next();
Student stu=new Student(name,age,sex);
System.out.println(stu.toString());
}
}
设计一个BankAccount类,这个类包括:
(1)一个int型的balance表时账户余额。
(2)一个无参构造方法,将账户余额初始化为0。
(3)一个带一个参数的构造方法,将账户余额初始化为该输入的参数。
(4)一个getBlance()方法,返回账户余额。
(5)一个withdraw()方法:带一个amount参数,并从账户余额中提取amount指定的款额。
(6)一个deposit()方法:带一个amount参数,并将amount指定的款额存储到该银行账户上。
设计一个Main类进行测试,分别输入账户余额、提取额度以及存款额度,并分别输出账户余额。
依次输入账户余额、提取额度、存款额度
依次输出初始账户余额、提取amount额度后的账户余额、存入amount后的账户余额
在这里给出一组输入。例如:
700
70
7
在这里给出相应的输出。例如:
700
630
637
答案代码如下:
import java.util.Scanner;
class BankAccount{
int balance;
public BankAccount() {
this.balance=0;
}
public BankAccount(int balance) {
this.balance=balance;
}
public int getBlance() {
return this.balance;
}
public int withdraw(int amount) {
balance=balance-amount;
return balance;
}
public int deposit(int amount) {
balance=balance+amount;
return balance;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int balance=sc.nextInt();
int amount1=sc.nextInt();
int amount2=sc.nextInt();
BankAccount ba=new BankAccount(balance);
System.out.println(ba.getBlance());
System.out.println(ba.withdraw(amount1));
System.out.println(ba.deposit(amount2));
}
}
请补充以下代码,完成输出要求。
public class Main {
public Main(){
System.out.println("构造方法一被调用了");
}
public Main(int x){
this();
System.out.println("构造方法二被调用了");
}
public Main(boolean b){
this(1);
System.out.println("构造方法三被调用了");
}
public static void main(String[] args) {
}
}
输入格式:
无
输出以下三行:
构造方法一被调用了
构造方法二被调用了
构造方法三被调用了
无
构造方法一被调用了
构造方法二被调用了
构造方法三被调用了
答案代码如下:
Main a=new Main(true);
Java每个对象都继承自Object,都有equals、toString等方法。
现在需要定义PersonOverride
类并覆盖其toString
与equals
方法。
a. 属性:String name
、int age
、boolean gender
,所有的变量必须为私有(private)。
b. 有参构造方法,参数为name, age, gender
c. 无参构造方法,使用this(name, age,gender)
调用有参构造函数。参数值分别为"default",1,true
d.toString()
方法返回格式为:name-age-gender
e. equals
方法需比较name、age、gender,这三者内容都相同,才返回true
.
2.1 输入n1,使用无参构造函数创建n1个对象,放入数组persons1。
2.2 输入n2,然后指定name age gender
。每创建一个对象都使用equals方法比较该对象是否已经在数组中存在,如果不存在,才将该对象放入数组persons2。
2.3 输出persons1数组中的所有对象
2.4 输出persons2数组中的所有对象
2.5 输出persons2中实际包含的对象的数量
2.5 使用System.out.println(Arrays.toString(PersonOverride.class.getConstructors()));
输出PersonOverride的所有构造函数。
提示:使用ArrayList
代替数组大幅复简化代码,请尝试重构你的代码。
1
3
zhang 10 true
zhang 10 true
zhang 10 false
default-1-true
zhang-10-true
zhang-10-false
2
[public PersonOverride(), public PersonOverride(java.lang.String,int,boolean)]
答案代码如下:
import java.util.Arrays;
import java.util.Scanner;
class PersonOverride{
private String name;
private int age;
private boolean gender;
public PersonOverride(){
this("default",1,true);
}
public PersonOverride(String name,int age,boolean gender){
this.name=name;
this.age=age;
this.gender=gender;
}
@Override
public String toString() {
return name + "-" + age + "-" +gender;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + (gender ? 1231 : 1237);
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PersonOverride other = (PersonOverride) obj;
if (age != other.age)
return false;
if (gender != other.gender)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
public class Main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n1=sc.nextInt();
PersonOverride persons1[]=new PersonOverride[n1];
for(int i=0;i=count) {
persons2[count]=p;
count++;
}
}
for(int i=0;i
编写程序,实现图形类的继承,并定义相应类对象并进行测试。
public double getArea();//求图形面积
public double getVolume();//求球体积
public double getVolume();//求立方体体积
Constructing 类名
主方法内,主要实现四个功能(1-4):
从键盘输入1,则定义圆类,从键盘输入圆的半径后,主要输出圆的面积;
从键盘输入2,则定义矩形类,从键盘输入矩形的宽和长后,主要输出矩形的面积;
从键盘输入3,则定义球类,从键盘输入球的半径后,主要输出球的表面积和体积;
从键盘输入4,则定义立方体类,从键盘输入立方体的宽、长和高度后,主要输出立方体的表面积和体积;
假如数据输入非法(包括圆、矩形、球及立方体对象的属性不大于0和输入选择值非1-4),系统输出Wrong Format
共四种合法输入
按照以上需求提示依次输出
在这里给出一组输入。例如:
1 1.0
在这里给出相应的输出。例如:
Constructing Shape
Constructing Circle
Circle's area:3.14
在这里给出一组输入。例如:
4 3.6 2.1 0.01211
在这里给出相应的输出。例如:
Constructing Shape
Constructing Rectangle
Constructing Box
Box's surface area:15.26
Box's volume:0.09
在这里给出一组输入。例如:
2 -2.3 5.110
在这里给出相应的输出。例如:
Wrong Format
答案代码如下:
import java.util.Scanner;
class Shape{
public double getArea() {
return 0.0;
}
public Shape() {
System.out.println("Constructing Shape");
}
}
class Circle extends Shape{
private double radius;
public double getArea() {
return Math.PI*radius*radius;
}
public Circle() {
System.out.println("Constructing Circle");
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
}
class Rectangle extends Shape{
private double width;
private double length;
public double getArea() {
return width*length;
}
public Rectangle() {
System.out.println("Constructing Rectangle");
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
}
class Ball extends Circle{
public double getArea() {
return 4*super.getArea();
}
public double getVolume(){
double radius1=getRadius();
return Math.PI*radius1*radius1*radius1*4.0/3.0;
}
public Ball() {
System.out.println("Constructing Ball");
}
}
class Box extends Rectangle{
private double height;
public double getArea(){
double width=getWidth();
double length=getLength();
return 2*(width*length+height*width+length*height);
}
public double getVolume() {
return super.getArea()*height;
}
public Box() {
System.out.println("Constructing Box");
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
switch(n) {
case 1:
double radius=sc.nextDouble();
if(radius<=0) {
System.out.println("Wrong Format");
}else {
Circle cir=new Circle();
cir.setRadius(radius);
System.out.printf("Circle's area:%.2f",cir.getArea());}
break;
case 2:
double width=sc.nextDouble();
double length=sc.nextDouble();
if(width<=0||length<=0) {
System.out.println("Wrong Format");
}else {
Rectangle rec=new Rectangle();
rec.setLength(length);
rec.setWidth(width);
System.out.printf("Rectangle's area:%.2f",rec.getArea());}
break;
case 3:
double radius1=sc.nextDouble();
if(radius1<=0) {
System.out.println("Wrong Format");
}else {
Ball ba=new Ball();
ba.setRadius(radius1);
System.out.printf("Ball's surface area:%.2f\n",ba.getArea());
System.out.printf("Ball's volume:%.2f\n",ba.getVolume());
}
break;
case 4:
double width1=sc.nextDouble();
double length1=sc.nextDouble();
double height=sc.nextDouble();
if(width1<=0||length1<=0||height<=0) {
System.out.println("Wrong Format");
}
else {
Box bo=new Box();
bo.setWidth(width1);
bo.setHeight(height);
bo.setLength(length1);
System.out.printf("Box's surface area:%.2f\n",bo.getArea());
System.out.printf("Box's volume:%.2f\n",bo.getVolume());}
break;
default:
System.out.println("Wrong Format");
}
}
}
前面题目形状中我们看到,为了输出所有形状的周长与面积,需要建立多个数组进行多次循环。这次试验使用继承与多态来改进我们的设计。
1.定义抽象类Shape
属性:不可变静态常量double PI
,值为3.14
,
抽象方法:public double getPerimeter()
,public double getArea()
2.Rectangle与Circle类均继承自Shape类。
Rectangle类(属性:int width,length)、Circle类(属性:int radius)。
带参构造方法为Rectangle(int width,int length)
,Circle(int radius)
。toString
方法(Eclipse自动生成)
3.编写double sumAllArea
方法计算并返回传入的形状数组中所有对象的面积和与double sumAllPerimeter
方法计算并返回传入的形状数组中所有对象的周长和。
4.main方法
4.1 输入整型值n,然后建立n个不同的形状。如果输入rect,则依次输入宽、长。如果输入cir,则输入半径。
4.2 然后输出所有的形状的周长之和,面积之和。并将所有的形状信息以样例的格式输出。 提示:使用Arrays.toString
。
4.3 最后输出每个形状的类型与父类型.使用类似shape.getClass()
//获得类型, shape.getClass().getSuperclass()
//获得父类型;
注意:处理输入的时候使用混合使用nextInt
与nextLine
需注意行尾回车换行问题。
4
rect
3 1
rect
1 5
cir
1
cir
2
38.84
23.700000000000003
[Rectangle [width=3, length=1], Rectangle [width=1, length=5], Circle [radius=1], Circle [radius=2]]
class Rectangle,class Shape
class Rectangle,class Shape
class Circle,class Shape
class Circle,class Shape
答案代码如下:
import java.util.Scanner;
abstract class Shape{
final double PI=3.14;
public abstract double getPerimeter();
public abstract double getArea();
}
class Rectangle extends Shape{
public int width;
public int length;
Rectangle(int width,int length){
this.length=length;
this.width=width;
}
@Override
public String toString() {
return "Rectangle [width=" + width + ", length=" + length + "]";
}
public double getPerimeter() {
return (double)2.0*width+2*length;
}
public double getArea() {
return width*length;
}
}
class Circle extends Shape{
public int radius;
Circle(int radius){
this.radius=radius;
}
@Override
public String toString() {
return "Circle [radius=" + radius + "]";
}
public double getPerimeter() {
return (double)PI*2*radius;
}
public double getArea() {
return (double)PI*radius*radius;
}
}
public class Main{
static double sumAllArea(Shape []sha) {
double sum=0.0;
for(Shape i:sha) {
sum=sum+i.getArea();
}
return sum;
}
static double sumAllPerimeter(Shape []sha) {
double sum=0.0;
for(Shape i:sha) {
sum=sum+i.getPerimeter();
}
return sum;
}
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
Shape sha[]=new Shape[n];
for(int i=0;i
程序填空题。请补充以下代码,完成题目要求。(注意:需要提交完整代码) 有一个团队由5个人组成。他们每个人给指导老师一个分数,去掉最高分,去掉最低分,剩下的3个分数的平均分就是该团队对指导老师的评分。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] grade = new int[5];
for(int i=0; i
在一行中给出5个不超过10的正整数(从小到大排列)。
输出集体评分,保留小数点后两位。
1 2 4 6 9
4.00
答案代码如下:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] grade = new int[5];
for(int i=0; igrade[i]) {
min=grade[i];
}
sum=sum+grade[i];
}
aver=(sum-max-min)/3;
return aver;
}
}
输入在一行中给出2个整数
输出两个数的和、差
在这里给出一组输入。例如:
6 7
在这里给出相应的输出。例如:
13
-1
答案代码如下:
import java.util.Scanner;
interface ICompute{
int computer(int n,int m);
}
class Add implements ICompute{
public int computer(int n,int m) {
return m+n;
}
}
class Sub implements ICompute{
public int computer(int n,int m) {
return n-m;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
Add ad=new Add();
System.out.println(ad.computer(a,b));
Sub su=new Sub();
System.out.println(su.computer(a,b));
}
}
编写一个程序进行一个班某门课程成绩的录入,能够控制录入成绩总人数,对录入成绩统计其及格人数和不及格人数。设计一个异常类,当输入的成绩小0分或大于100分时,抛出该异常类对象,程序将捕捉这个异常对象,并调用执行该异常类对象的toString()方法,该方法获取当前无效分数值,并返回一个此分数无效的字符串。
从键盘中输入学生人数n
从键盘中输入第1个学生的成绩
从键盘中输入第2个学生的成绩
...
从键盘中输入第n个学生的成绩
(注:当输入的成绩无效时(即分数为小于0或大于100)可重新输入,且输出端会输出此分数无效的提醒。)
显示及格总人数
显示不及格总人数
在这里给出一组输入。例如:
3
100
30
60
在这里给出相应的输出。例如:
2
1
在这里给出一组输入。例如:
2
200
69
30
在这里给出相应的输出。例如:
200invalid!
1
1
答案代码如下:
import java.util.ArrayList;
import java.util.Scanner;
class MyException extends Exception{
public MyException(int grade) {
super(grade+"invalid!");
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList grade=new ArrayList<>();
int pass=0;
int unpass=0;
int n=sc.nextInt();
for(int i=0;i100) {
n++;
}
}
for(int i:grade) {
try {
if(i>=60&&i<=100) {
pass++;
}
if(i<60&&i>=0) {
unpass++;
}
if(i<0||i>100) {
throw new MyException(i);
}
}catch(MyException e){
System.out.println(e.getMessage());
}
}
System.out.println(pass);
System.out.println(unpass);
}
}
输入3个整数n、begin、end。
首先,使用如下代码:
for(int i=0;i
将从0到n-1的数字拼接为字符串str。如,n=12
,则拼接出来的字符串为01234567891011
最后截取字符串str从begin到end(包括begin,但不包括end)之间的字符串,并输出。
10
5
8
1000
800
900
567
0330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533
答案代码如下:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNextInt()) {
int n=sc.nextInt();
int begin=sc.nextInt();
int end=sc.nextInt();
StringBuilder str=new StringBuilder();
for(int i=0;i
设计一个对字符串中的单词查找替换方法,实现对英文字符串中所有待替换单词的查找与替换。
首行输入母字符串,第二行输入查询的单词,第三行输入替换后的单词。
完成查找替换后的完整字符串
在这里给出一组输入。例如:
Although I am without you, I will always be ou you
ou
with
在这里给出相应的输出。例如:
Although I am without you, I will always be with you
答案代码如下:
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str=sc.nextLine();
String check=sc.next();
String replace=sc.next();
String ss[]=str.split(" ");
StringBuffer sb=new StringBuffer();
for(int i=0;i
统计一行字符串中的英文字母个数、空格个数、数字个数、其他字符个数
通过键盘输入一行字符(任意字符)
统计一行字符串中的中英文字母个数、空格个数、数字个数、其他字符个数
rwrwewre2345asdJSJQI%^&(& *&sdf YY( 2342-k'
字母个数:22
数字个数:8
空格个数:5
其他字符个数:10
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str=sc.nextLine();
char ch[]=str.toCharArray();
int letter=0;
int space=0;
int number=0;
int other=0;
for(int i=0;i='a'&&ch[i]<='z')||(ch[i]>='A'&&ch[i]<='Z')) {
letter++;}else
if(ch[i]>=47&&ch[i]<57) {
number++;}else
if(ch[i]==32) {
space++;}else {
other++;}
}
System.out.println("字母个数:"+letter);
System.out.println("数字个数:"+number);
System.out.println("空格个数:"+space);
System.out.println("其他字符个数:"+other);
}
}
本习题主要用于练习如何使用ArrayList来替换数组。
新建1个ArrayList
用来存放字符串,然后进行如下操作。
提示: 查询Jdk文档中的ArrayList。
注意: 请使用System.out.println(strList)
输出列表元素。
输入: n个字符串,放入strList
。直到输入为!!end!!
时,结束输入。
在strList
头部新增一个begin
,尾部新增一个end
。
输出列表元素
输入: 字符串str
判断strList
中有无包含字符串str
,如包含输出true
,否则输出false
。并且输出下标,没包含返回-1。
在strList中从后往前找。返回其下标,找不到返回-1。
移除掉第1个(下标为0)元素,并输出。然后输出列表元素。
输入: 字符串str
将第2个(下标为1)元素设置为字符串str.
输出列表元素
输入: 字符串str
遍历strList,将字符串中包含str的元素放入另外一个ArrayList strList1
,然后输出strList1。
在strList中使用remove
方法,移除第一个和str相等的元素。
输出strList列表元素。
使用clear
方法,清空strList。然后输出strList的内容,size()
与isEmpty()
,3者之间用,
连接。
a1 b1 3b a2 b2 12b c d !!end!!
b1
second
b
[begin, a1, b1, 3b, a2, b2, 12b, c, d, end]
true
2
2
begin
[a1, b1, 3b, a2, b2, 12b, c, d, end]
[a1, second, 3b, a2, b2, 12b, c, d, end]
[3b, b2, 12b]
[a1, second, 3b, a2, b2, 12b, c, d, end]
[],0,true
答案代码如下:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
ArrayList strList = new ArrayList();
Scanner sc = new Scanner(System.in);
while (true) {
String s1 = sc.next();
if (!s1.equals("!!end!!")) {
strList.add(s1);
} else break;
}
String s1=new String();
strList.add(0, "begin");
strList.add("end");
System.out.println(strList);
String s2 = new String();
s1 = sc.next();
System.out.println(strList.contains(s1));
System.out.println(strList.indexOf(s1));
System.out.println(strList.lastIndexOf(s1));
System.out.println(strList.get(0));
strList.remove(0);
System.out.println(strList);
s1= sc.next();
strList.set(1, s1);
System.out.println(strList);
ArrayList strList1 = new ArrayList();
s1=sc.next();
for (int j = 0; j < strList.size(); j++)
{
if (((String) strList.get(j)).contains(s1)) {
strList1.add(strList.get(j));
}
}
System.out.println(strList1);
strList.remove(s1);
System.out.println(strList);
strList.clear();
System.out.println(strList+","+strList.size()+","+strList.isEmpty());
}
}
根据教师的花名册,找到出勤最多的人。
出勤记录单行给出,数据直接使用空格分割。
单行输出(若有多人,人名直接使用空格分割,结尾处没有空格)。
在这里给出一组输入。例如:
zs ls ww ml zs ls ml zs ww
在这里给出相应的输出。例如:
zs
答案代码如下:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str=sc.nextLine();
String str1[]=str.split(" ");
ArrayList list=new ArrayList<>(Arrays.asList(str1));
String max=list.get(0);
int maxnum=1;
for(int i=0;imaxnum) {
maxnum=count;
max=list.get(i);
}
}
}
System.out.println(max);
}
}
创建一个倒数计数线程。要求:1.该线程使用实现Runnable接口的写法;2.程序该线程每隔0.5秒打印输出一次倒数数值(数值为上一次数值减1)。
N(键盘输入一个整数)
每隔0.5秒打印输出一次剩余数
6
在这里给出相应的输出。例如:
6
5
4
3
2
1
0
答案代码如下:
import java.util.Scanner;
class run implements Runnable{
public void run() {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=n;i>=0;i--) {
System.out.println(i);
}
try {
Thread.sleep(500);
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}
public class Main {
public static void main(String[] args) {
Runnable r=new run();
Thread th=new Thread(r);
th.start();
}
}
定义一个USB接口,并通过Mouse和U盘类实现它,具体要求是:
1.接口名字为USB,里面包括两个抽象方法:
void work();描述可以工作
void stop(); 描述停止工作
2.完成类Mouse,实现接口USB,实现两个方法:
work方法输出“我点点点”;
stop方法输出 “我不能点了”;
3.完成类UPan,实现接口USB,实现两个方法:
work方法输出“我存存存”;
stop方法输出 “我走了”;
4测试类Main中,main方法中
定义接口变量usb1 ,存放鼠标对象,然后调用work和stop方法
定义接口数组usbs,包含两个元素,第0个元素存放一个Upan对象,第1个元素存放Mouse对象,循环数组,对每一个元素都调用work和stop方法。
输出方法调用的结果
在这里给出一组输入。例如:
在这里给出相应的输出。例如:
我点点点
我不能点了
我存存存
我走了
我点点点
我不能点了
答案代码如下:
import java.util.Scanner;
interface USB{
void work();
void stop();
}
class Mouse implements USB{
public void work() {
System.out.println("我点点点");
}
public void stop() {
System.out.println("我不能点了");
}
}
class UPan implements USB{
public void work() {
System.out.println("我存存存");
}
public void stop() {
System.out.println("我走了");
}
}
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
USB usb1=new Mouse();
usb1.work();
usb1.stop();
USB usbs[]=new USB[2];
usbs[0]=new UPan();
usbs[1]=new Mouse();
for(USB a:usbs){
a.work();
a.stop();
}
}
}
a 定义圆类Circle,其中包括:
b定义Main类,在main方法中,完成下列操作
### 输入格式: 从键盘输入一个整数半径
分别输出c1和c2对象的信息
在这里给出一组输入。例如:
4
5
-4
-2
在这里给出相应的输出。例如:
this is a constructor
Circle [radius=2]
c1:area=12
this is a constructor
Circle [radius=2]
Circle [radius=4]
c2:area=50
this is a constructor with para
Circle [radius=5]
c3:area=78
this is a constructor
Circle [radius=2]
c1:area=12
this is a constructor
Circle [radius=2]
Circle [radius=2]
c2:area=12
this is a constructor with para
Circle [radius=2]
c3:area=12
答案代码如下:
import java.util.Scanner;
class Circle{
private int radius;
public Circle() {
this.radius=2;
System.out.println("this is a constructor");
}
public Circle(int radius) {
if(radius<=0) {
this.radius=2;
}
else {
System.out.println("this is a constructor with para");
this.radius=radius;}
}
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
if(radius<=0) {
this.radius=2;}
else {
this.radius=radius;
}
}
public int getArea() {
return (int) (Math.PI*radius*radius);
}
@Override
public String toString() {
return "Circle [radius=" + radius + "]";
}
}
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int radius=sc.nextInt();
int radius1=sc.nextInt();
Circle c1=new Circle();
System.out.println(c1.toString());
System.out.println("c1:area="+c1.getArea());
Circle c2=new Circle();
System.out.println(c2);
c2.setRadius(radius);
System.out.println(c2.toString());
System.out.println("c2:area="+c2.getArea());
Circle c3=new Circle(radius1);
System.out.println(c3.toString());
System.out.println("c3:area="+c3.getArea());
}
}
定义一个Person类
2 在Main类的main方法中
输入格式:
输入姓名
输出姓名,一行一个对象
在这里给出一组输入。例如:
jerry
在这里给出相应的输出。例如:
name:none
name:jerry
答案代码如下:
import java.util.Scanner;
class Person{
String name;
public Person() {
this.name="none";
}
public Person(String name) {
this.name=name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString(){
return "name:"+name;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String name=sc.next();
Person per[]=new Person[2];
per[0]=new Person();
per[1]=new Person(name);
System.out.println(per[0]);
System.out.println(per[1]);
}
}
在一行中输入半径 和高。
对每一个圆柱体输出它的体积
在这里给出一组输入。例如:
2 3
在这里给出相应的输出。例如:
Constructor with para
37
Constructor with para
Constructor no para
12
答案代码如下:
import java.util.Scanner;
class Cylinder{
private int radius;
private int height;
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public Cylinder(int radius,int height) {
this.radius=radius;
this.height=height;
System.out.println("Constructor with para");
}
public Cylinder() {
this(2,1);
System.out.println("Constructor no para");
}
public int getVolume() {
return (int) (Math.PI*this.radius*this.radius*this.height);
}
}
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int radius=sc.nextInt();
int height=sc.nextInt();
Cylinder c1=new Cylinder(radius,height);
System.out.println(c1.getVolume());
Cylinder c2=new Cylinder();
System.out.println(c2.getVolume());
}
}
本题练习列表的使用。
先一行输入n表示对象个数,然后每行输入一个Person对象的name和age
一行输入一个人的姓名对其进行查询
对每一对象,在一行中输出对象的信息。
对查询的人员,查到输出该人的信息,否则输出此人不存在。
在这里给出一组输入。例如:
3
zhang 23
li 44
wang 33
li
3
zhang 23
li 44
wang 33
may
在这里给出相应的输出。例如:
Person [name=zhang, age=23]
Person [name=li, age=44]
Person [name=wang, age=33]
Person [name=li, age=44]
Person [name=zhang, age=23]
Person [name=li, age=44]
Person [name=wang, age=33]
此人不存在
答案代码如下
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
class Person{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Person(String name,int age) {
this.name=name;
this.age=age;
}
public Person() {
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
public class lin {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
ArrayList list = new ArrayList();
int n=sc.nextInt();
for(int i=0;i
长方形类-类名:Rectangle
,private属性:int width,length
圆形类-类名:Circle
,private属性:int radius
编写构造函数:
带参构造函数:Rectangle(width, length)
,Circle(radius)
编写方法:public int getPerimeter()
,求周长。public int getArea()
,求面积。toString
方法,使用Eclipse自动生成。
注意:
Math.PI
。int
再返回。Arrays.deepToString
分别输出上面建立的Rectangle数组与Circle数组思考:如果初次做该题会发现代码冗余严重。使用继承、多态思想可以大幅简化上述代码。
1 2
3 4
7
1
69
170
[Rectangle [width=1, length=2], Rectangle [width=3, length=4]]
[Circle [radius=7], Circle [radius=1]]
答案代码如下:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
class Rectangle
{
private int width;
private int length;
public Rectangle(int width,int length) {
this.length=length;
this.width=width;
}
public int getPerimeter() {
return 2*width+2*length;
}
public int getArea() {
return width*length;
}
@Override
public String toString() {
return "Rectangle [width=" + width + ", length=" + length + "]";
}
}
class Circle{
private int radius;
public Circle(int radius) {
this.radius=radius;
}
public int getPerimeter() {
return (int) (2*Math.PI*radius);
}
public int getArea() {
return (int) (Math.PI*radius*radius);
}
@Override
public String toString() {
return "Circle [radius=" + radius + "]";
}
}
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
ArrayList rec=new ArrayList();
ArrayList cir=new ArrayList();
rec.add(new Rectangle(sc.nextInt(),sc.nextInt()));
rec.add(new Rectangle(sc.nextInt(),sc.nextInt()));
cir.add(new Circle(sc.nextInt()));
cir.add(new Circle(sc.nextInt()));
int l=0;
int s=0;
for(Rectangle a:rec) {
l=a.getPerimeter()+l;
s=a.getArea()+s;
}
for(Circle b:cir) {
l=b.getPerimeter()+l;
s=b.getArea()+s;
}
System.out.println(l);
System.out.println(s);
System.out.println(rec);
System.out.println(cir);
}
}
欢迎大家批评指正!!!