本文目录一览:
- 1、一道简单的java编程题?
- 2、java编程题目
- 3、5道简单的JAVA编程题(高分悬赏)
- 4、java入门编程题:某班有十位同学,请顺序输入十位同学的学号,保存在数组中,并输出所有同学的学号?
- 5、JAVA编程题
- 6、有关java编程题目?
一道简单的java编程题?
import java.text.ParseException;
import java.text.SimpleDateFormat;
//日期类
public class Date {
private String year;
private String month;
private String day;
public Date(String year, String month, String day) {
this.year = year;
this.month = month;
this.day = day;
}
public void format(){
System.out.println(day + "/" + month + "/" + year);
}
public void calculate(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
try {
java.util.Date startDate = sdf.parse(year + "/" + "01" + "/" + "01");
java.util.Date inputDate = sdf.parse(year + "/" + month + "/" + day);
long resultDay = (inputDate.getTime() - startDate.getTime())/(24 * 1000 * 60 * 60);
System.out.println("第" + (resultDay + 1) + "天");
} catch (ParseException e) {
e.printStackTrace();
}
}
}
//测试类
public class Test {
public static void main(String[] args) {
Date date1 = new Date("2020","04","11");
Date date2 = new Date("2020","01","02");
date1.format();
date1.calculate();
date2.format();
date2.calculate();
}
}
java编程题目
这不都说的很清楚了么。。。。。。。。
自己写吧,也没啥难度。
是完全不知道这个题目再说什么么?
package spring5.source;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class D extends JFrame {
public static void main(String[] args) {
D d = new D();
d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
d.setSize(500, 500);
d.setLayout(new FlowLayout());
TextField t1 = new TextField();
TextField t2 = new TextField();
Button b = new Button("OK");
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String v1 = t1.getText();
try {
int n = Integer.parseInt(v1);
Double d = Math.pow(n, 2);
t2.setText(String.valueOf(d.intValue()));
} catch (Exception e2) {
e2.printStackTrace();
}
}
});
t1.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyChar() == KeyEvent.VK_ENTER) {
String v1 = t1.getText();
try {
int n = Integer.parseInt(v1);
Double d = Math.pow(n, 2);
t2.setText(String.valueOf(d.intValue()));
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
});
// KeyListener key_Listener = ;
d.add(t1);
d.add(t2);
d.add(b);
d.setVisible(true);
}
}
少了一个 d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 关闭窗口的
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(i10);
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入门编程题:某班有十位同学,请顺序输入十位同学的学号,保存在数组中,并输出所有同学的学号?
import java.util.Scanner;
public class Students {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] students=new String[10];
String No=null;
for (int i = 0; i 10 ; i++) {
System.out.println("请输入学号:");
No=in.next();
students[i]=No;
}
System.out.println("学号是:");
for (String a:students) {
System.out.print(a+" ");
}
}
}
JAVA编程题
//圆类Circle
import java.util.Scanner;
public class Circle {
private double radius;
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
//无参构造函数
public Circle(){
this.radius=0;
}
//带参构造函数
public Circle(double r ){
this.radius=r;
}
//获取面积
public double getArea(){
double r=this.radius;
double area=r*r*3.14;
return area;
}
//获取周长
public double getPerimeter(){
double perimeter=this.radius*2*3.14;
return perimeter;
}
//打印圆的信息
public void show(){
System.out.println("请输入圆的半径");
Scanner sc=new Scanner(System.in);
this.setRadius(sc.nextInt());
System.out.println("圆的半径"+this.getRadius());
System.out.println("圆的面积"+this.getArea());
System.out.println("圆的周长"+this.getPerimeter());
}
}
//圆柱类
public class Cylinder extends Circle {
//圆柱高
private double height;
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
//构造方法
public Cylinder (double r, double h){
super();
this.height=h;
this.setRadius(r);
}
public double getVolume( ) {
double volume=this.getArea()*this.height;
return volume;
}
//求体积
public void showVolume(){
System.out.println("圆柱的体积"+this.getVolume());
}
}
//主程序入口
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Circle circle=new Circle();
circle.show();
Cylinder cylinder=new Cylinder(2.0,8.5);
cylinder.showVolume();
}
}
PS:注释写的很详尽了,求采纳
有关java编程题目?
按照题目要求编写的圆,圆锥和测试类的Java程序如下
Test.java文件内容如下
class Circle{
private double r;
private String color;
public Circle(double r){
this.r=r;
}
public double area(){
return Math.PI*r*r;
}
public double perimeter(){
return Math.PI*2*r;
}
public double getR(){
return this.r;
}
public void setR(double r){
this.r=r;
}
public String getColor(){
return this.color;
}
public void setColor(String color){
this.color=color;
}
public String toString(){
return "圆的半径为"+r+",颜色为"+color;
}
}
class Cone{
private Circle c;
private double h;
private String color;
public Cone(Circle c,double h){
this.c=c;
this.h=h;
}
public double volume(){
return 1.0/3*c.area()*h;
}
public Circle getCircle(){
return this.c;
}
public void setCircle(Circle c){
this.c=c;
}
public double getH(){
return this.h;
}
public void setH(double h){
this.h=h;
}
public String getColor(){
return this.color;
}
public void setColor(String color){
this.color=color;
}
public String toString(){
return "圆锥的底面积为"+c.area()+",高为"+h+",颜色为"+color;
}
}
public class Test{
public static void main(String[] args){
Circle circle1=new Circle(2.5);
circle1.setColor("红色");
System.out.println(circle1.toString());
System.out.println("圆的面积为"+circle1.area());
System.out.println("圆的周长为"+circle1.perimeter());
Cone circlar1=new Cone(circle1,2.7);
circlar1.setColor("蓝色");
System.out.println(circlar1.toString());
System.out.println("圆锥的体积为"+circlar1.volume());
}
}