导航:首页 > 编程语言 > JAVA编程经典题目

JAVA编程经典题目

发布时间:2022-08-03 08:53:14

java编程题目

public class Student {

private String code;
private String name;
private int height;
private char sex;

public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String code, String name, int height, char sex) {
super();
this.code = code;
this.name = name;
this.height = height;
this.sex = sex;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}

public String show(){
return "Student [code=" + code + ", name=" + name + ", height=" + height + ", sex=" + sex + "]";

}

public int show(Student s){

return s.getHeight();

}
public void show(Student s,int sv){
if(s.getHeight()>=sv){
if(s.getSex()=='f'){
System.out.println(s.show());
}
}

}

}

public class Test {

public static void main(String[] args) {

Student student1 = new Student("A001", "zs1", 171, 'm');
Student student2 = new Student("A002", "zs2", 172, 'f');
Student student3 = new Student("A003", "zs3", 173, 'm');
Student student4 = new Student("A004", "zs4", 174, 'f');
Student student5 = new Student("A005", "zs5", 175, 'f');
System.out.println(student1.show());
System.out.println(student2.show());
System.out.println(student3.show());
System.out.println(student4.show());
System.out.println(student5.show());
//平均身高
int sv=(student1.show(student1)+student2.show(student2)+student3.show(student3)+
student4.show(student4)+student5.show(student5))/5;

System.out.println("超过平均年龄的女生详情:");
//超过平均年龄的女生
student1.show(student1, sv);
student2.show(student2, sv);
student3.show(student3, sv);
student4.show(student4, sv);
student5.show(student5, sv);

}

}

Ⅱ 5道简单的JAVA编程题(高分悬赏)

很详细的帮你写下,呵呵,所以要给分哦!
1、
(1)源程序如下:
public class One {

public static void main(String[] args) {
String name = "张三";
int age = 23;
char sex = '男';
String myclass = "某某专业2班";
System.out.println("姓名:" + name);
System.out.println("姓名:" + age);
System.out.println("姓名:" + sex);
System.out.println("姓名:" + myclass);

}

}

(2)

编写完程序的后缀名是.java,如本题,文件名就是One.java。
开始\运行\cmd,进入“命令提示符窗口”,然后用javac编译器编译.java文件,语句:javac One.java。

(3)
编译成功后,生成的文件名后缀是.class,叫做字节码文件。再用java解释器来运行改程序,语句:java One

2、编写程序,输出1到100间的所有偶数
(1)for语句
public class Two1 {

public static void main(String[] args) {
for(int i=2;i<=100;i+=2)
System.out.println(i);

}
}

(2)while语句
public class Two2 {

public static void main(String[] args) {
int i = 2;
while (i <= 100) {
System.out.println(i);
i += 2;
}
}
}

(3)do…while语句
public class Two3 {

public static void main(String[] args) {
int i = 2;
do {
System.out.println(i);
i += 2;
}while(i<=100);
}
}

3、编写程序,从10个数当中找出最大值。
(1)for循环
import java.util.*;

public class Three1 {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int max = 0;
for (int i = 0; i < 10; i++) {
System.out.print("输入第" + (i + 1) + "个数:");
number = input.nextInt();
if (max < number)
max = number;
}
System.out.println("最大值:" + max);
}
}

(2)while语句
import java.util.*;

public class Three2 {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int max = 0;
int i = 0;
while (i < 10) {
System.out.print("输入第" + (i + 1) + "个数:");
number = input.nextInt();
if (max < number)
max = number;
i++;
}
System.out.println("最大值:" + max);
}
}

(3)do…while语句
import java.util.*;

public class Three3 {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int max = 0;
int i = 0;
do {
System.out.print("输入第" + (i + 1) + "个数:");
number = input.nextInt();
if (max < number)
max = number;
i++;
}while(i<10);
System.out.println("最大值:" + max);
}
}

4、编写程序,计算从1到100之间的奇数之和。
(1)for循环

public class Four1 {

public static void main(String[] args) {
int sum=0;
for(int i = 1;i<=100;i+=2){
sum+=i;
}
System.out.println("1~100间奇数和:" + sum);
}
}

(2)while语句
public class Four2 {

public static void main(String[] args) {
int sum = 0;
int i = 1;
while (i <= 100) {
sum += i;
i += 2;
}
System.out.println("1~100间奇数和:" + sum);
}
}

(3)do…while语句
public class Four3 {

public static void main(String[] args) {
int sum = 0;
int i = 1;
do {
sum += i;
i += 2;
} while (i <= 100);
System.out.println("1~100间奇数和:" + sum);
}
}

5、
(1)什么是类的继承?什么是父类?什么是子类?举例说明。
继承:是面向对象软件技术当中的一个概念。如果一个类A继承自另一个类B,就把这个A称为"B的子类",而把B称为"A的父类"。继承可以使得子类具有父类的各种属性和方法,而不需要再次编写相同的代码。在令子类继承父类的同时,可以重新定义某些属性,并重写某些方法,即覆盖父类的原有属性和方法,使其获得与父类不同的功能。另外,为子类追加新的属性和方法也是常见的做法。继承需要关键字extends。举例:
class A{}
class B extends A{}
//成员我就不写了,本例中,A是父类,B是子类。

(2)编写一个继承的程序。
class Person {
public String name;
public int age;
public char sex;

public Person(String n, int a, char s) {
name = n;
age = a;
sex = s;
}

public void output1() {
System.out.println("姓名:" + name + "\n年龄:" + age + "\n性别:" + sex);
}
}

class StudentPerson extends Person {
String school, department, subject, myclass;

public StudentPerson(String sc, String d, String su, String m, String n,
int a, char s) {
super(n, a, s);
school = sc;
department = d;
subject = su;
myclass = m;
}

public void output2() {
super.output1();
System.out.println("学校:" + school + "\n系别:" + department + "\n专业:"
+ subject + "\n班级:" + myclass);
}
}

public class Five2 {

public static void main(String[] args) {
StudentPerson StudentPersonDemo = new StudentPerson("某某大学", "某某系别",
" 某专业", "某某班级", " 张三", 23, '男');
StudentPersonDemo.output2();
}
}

Ⅲ java编程题目

这个你需要首先定义整型数组,使用int[] arr = {};这样的语句。然后使用trycatch语句进行异常处理,就可以了,思路就是这样的

Ⅳ java经典编程案例有哪些

  1. java编程的记事本:

    import java.util.*;
    public class JieChengExample
    {
    public static void main(String args[])
    {
    Scanner input=new Scanner(System.in);
    int n,sum;
    Jiecheng jie=new Jiecheng();
    System.out.print("输入n的值:");//输入有几个阶乘相加
    n=input.nextInt();
    sum=0;
    for(int i=1;i<=n;i++)
    {
    sum=sum+jie.jiecheng(i);//这是n个阶乘相加
    }
    System.out.println("1!+2!+3!+....+n!的和是:"+sum);
    }
    }
    class Jiecheng
    {
    public int jiecheng(int temp)//算阶乘的方法
    {
    int sum=1;
    for(int i=1;i<=temp;i++)
    {
    sum=sum*i; //计算阶乘
    }
    return sum;//将一个阶乘返回
    }
    }

2.java赛马游戏:

import java.util.Random;
public class Test {
public static void main(String[] args) {
Competition c = new Competition();
Thread T = new Thread(c);
T.start();
}
}
class Competition implements Runnable{
int red = 0;
int green = 0;
int Speed [] = new int [2];
Competition(){

}
public void run(){
Random r = new Random();
for(int a= 0;a<500;a++){
for(int j = 0;j<2;j++){
Speed[j] = r.nextInt(2);
red = red + Speed[j];
Speed[j] = r.nextInt(2);
green = green + Speed[j];
}
System.out.println("red的速度为"+red);
System.out.println("green的速度为"+green);
while(red >=500 || green>=500){
if(red >=500){
System.out.println("red先抵达终点线");
}
if(green >= 500){
System.out.println("green先抵达终点线");
}
if(green ==500 && red ==500 ){
System.out.println("两个同时到达");
}
return;
}
}
/* if(red >green){
System.out.println("Redwin"+red);
}
if(red<green){
S...import java.util.Random;
public class Test {
public static void main(String[] args) {
Competition c = new Competition();
Thread T = new Thread(c);
T.start();
}
}
class Competition implements Runnable{
int red = 0;
int green = 0;
int Speed [] = new int [2];
Competition(){

}
public void run(){
Random r = new Random();
for(int a= 0;a<500;a++){
for(int j = 0;j<2;j++){
Speed[j] = r.nextInt(2);
red = red + Speed[j];
Speed[j] = r.nextInt(2);
green = green + Speed[j];
}
System.out.println("red的速度为"+red);
System.out.println("green的速度为"+green);
while(red >=500 || green>=500){
if(red >=500){
System.out.println("red先抵达终点线");
}
if(green >= 500){
System.out.println("green先抵达终点线");
}
if(green ==500 && red ==500 ){
System.out.println("两个同时到达");
}
return;
}
}
/* if(red >green){
System.out.println("Redwin"+red);
}
if(red<green){
System.out.println("Greenwin"+green);
}
if(red == green){
System.out.println("equal");*/

JAVA的介绍:

Java是一种可以撰写跨平台应用程序的面向对象的程序设计语言。Java技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于PC、数据中心、游戏控制台、科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。

Ⅳ java的编程题目

两个代码 一个学生类, 一个测试类 你自己把他们放不同包里面

publicclassStudent{
privateintSnum;
privateintGnum;
privateStringSname;
privateStringSsex;
privateintSage;
privatestaticintcount=0;
publicStudent(intsnum,intgnum,Stringsname,Stringssex,intsage){
super();
this.Snum=snum;
Gnum=gnum;
Sname=sname;
Ssex=ssex;
Sage=sage;
count++;
}
//get方法获取属性,set方法修改属性
publicintgetSnum(){
returnSnum;
}
publicvoidsetSnum(intsnum){
this.Snum=snum;
}
publicintgetGnum(){
returnGnum;
}
publicvoidsetGnum(intgnum){
Gnum=gnum;
}
publicStringgetSname(){
returnSname;
}
publicvoidsetSname(Stringsname){
Sname=sname;
}
publicStringgetSsex(){
returnSsex;
}
publicvoidsetSsex(Stringssex){
Ssex=ssex;
}
publicintgetSage(){
returnSage;
}
publicvoidsetSage(intsage){
Sage=sage;
}

publicStringtoString(){
return"学号:"+this.Snum+".班号:"+this.Gnum+
".姓名:"+this.Sname+".性别:"+this.Ssex
+".年龄:"+this.Sage;
}
}

第二个:

publicclassStudentTest{
publicstaticvoidmain(String[]args){
//创建对象
Students1=newStudent(190,20,"张三","男",20);
Students2=newStudent(193,40,"李四","女",22);

//输出对象信息
System.out.println("第一个学生:"+s1.toString());
System.out.println("第二个学生:"+s2.toString());

//修改姓名和年龄
s1.setSname("Tom");
s2.setSage(24);
System.out.println("修改后第一个学生:"+s1.toString());
System.out.println("修改后第二个学生:"+s2.toString());

//比较
if(s1.getSage()<s2.getSage()){
System.out.println("年龄较小的学生是:"+s1.toString());
}else{
System.out.println("年龄较小的学生是:"+s2.toString());
}
}
}

Ⅵ java的经典编程题目

题目:输入某年某月某日,判断这一天是这一年的第几天?
题目:判断101-200之间有多少个素数,并输出所有素数。

Ⅶ JAVA编程题目,在线等!!!!!高分

1
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
List<Integer> temp = new ArrayList<Integer>();
for (int i = 0; i < 21; i++) {
list.add(i + 1);
}
while (list.size() > 2) {
for (int i = 0; i < list.size(); i++) {
if ((i + 1) % 3 == 0) {
System.out.println(list.get(i));
temp.add(list.get(i));
}
}
//每一轮结束
System.out.println("-----------------------");
list.removeAll(temp);
temp.clear();
}
System.out.println(list);
}

2 第2题类有点多
学生类
public class Student implements Serializable, Comparable<Student> {

private int mathematics;
private int chinese;
private int english;

public int getChinese() {
return chinese;
}

public void setChinese(int chinese) {
this.chinese = chinese;
}

public int getEnglish() {
return english;
}

public void setEnglish(int english) {
this.english = english;
}

public int getMathematics() {
return mathematics;
}

public void setMathematics(int mathematics) {
this.mathematics = mathematics;
}

public int sum() {
return this.chinese + this.english + this.mathematics;
}

//默认按总分排序
public int compareTo(Student o) {
if (this.sum() > o.sum()) {
return 1;
} else if (this.sum() < o.sum()) {
return -1;
} else {
return 0;
}
}
}
比较器 如果你要按其他的分数排序 可以自己写
/**
*
* 自己写比较器
*/
public class MyComparator implements Comparator<Student> {

public int compare(Student o1, Student o2) {
if (o1.getChinese() > o2.getChinese()) {
return 1;
} else if (o1.getChinese() < o2.getChinese()) {
return -1;
} else {
return 0;
}
}
}
测试类 你要先序列好学生对象才行
public static void main(String[] args) throws IOException, ClassNotFoundException {
InputStream is = new FileInputStream("文件地址");
ObjectInput input = new ObjectInputStream(is);
List<Student> list = (List) input.readObject();
//按指定的分数排序
Collections.sort(list, new MyComparator());
//默认按总分排序
Collections.sort(list);
}

3 4题 不好用程序来搞 不想写了 睡觉去了
明天还要看jsf icefaces累死人了
写了半天了 分给我吧

阅读全文

与JAVA编程经典题目相关的资料

热点内容
程序员大咖java 浏览:62
苹果手机文档安卓上怎么打开 浏览:527
如何做淘宝代理服务器 浏览:664
gz压缩文件夹 浏览:179
字母h从右往左跑的c语言编程 浏览:127
安卓手机如何拥有苹果手机横条 浏览:765
业余编程语言哪个好学 浏览:137
按照文件夹分个压缩 浏览:104
航空工业出版社单片机原理及应用 浏览:758
如何在电信app上绑定亲情号 浏览:376
安卓的怎么用原相机拍月亮 浏览:805
配音秀为什么显示服务器去配音了 浏览:755
c盘清理压缩旧文件 浏览:325
app怎么交付 浏览:343
图虫app怎么才能转到金币 浏览:175
如何做征文app 浏览:446
用什么app管理斐讯 浏览:169
安卓如何下载宝可梦剑盾 浏览:166
编译器开发属于哪个方向 浏览:940
megawin单片机 浏览:687