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

java经典编程题

发布时间:2022-07-08 21:09:15

java编程题求全部代码

classHW1{
publicstaticvoidmain(String[]args){
double[]test=newdouble[]{5.2,1.0,6.7,3.4,100.5,55.5};
BoundryValuesboundryValues=getBoundryValues(test);
System.out.println("MinValue="+boundryValues.getMin());
System.out.println("MaxValue="+boundryValues.getMax());
System.out.println("AveValue="+boundryValues.getAve());
}

{
privatedoublemax;
privatedoublemin;
privatedoubleave;

publicBoundryValues(){}

publicBoundryValues(doublemax,doublemin,doubleave){
this.max=max;
this.min=min;
this.ave=ave;
}

publicvoidsetMax(doublemax){
this.max=max;
}

publicdoublegetMax(){
returnmax;
}

publicvoidsetMin(doublemin){
this.min=min;
}

publicdoublegetMin(){
returnmin;
}

publicvoidsetAve(doubleave){
this.ave=ave;
}

publicdoublegetAve(){
returnave;
}
}

(double[]doubles){
BoundryValuesboundryValues=newBoundryValues();
double[]results=sort(doubles);
doubletotal=0.0;
for(inti=0;i<results.length;i++){
total+=results[i];
}
boundryValues.setMin(results[0]);
boundryValues.setMax(results[results.length-1]);
boundryValues.setAve(total/results.length);
returnboundryValues;
}

publicstaticdouble[]sort(double[]doubles){
for(inti=0;i<doubles.length;i++){
for(intj=0;j<doubles.length-i-1;j++){
if(doubles[j]>doubles[j+1]){
doubletemp=doubles[j];
doubles[j]=doubles[j+1];
doubles[j+1]=temp;
}
}
}
returndoubles;
}
}
importjava.util.*;

classHW2{
publicstaticvoidmain(String[]args){
Scannerscanner=newScanner(System.in);
doublea,b,c;
System.out.println("Entera,b,c:");
a=scanner.nextDouble();
b=scanner.nextDouble();
c=scanner.nextDouble();
Set<Double>sets=calculate(a,b,c);
for(Doubled:sets){
System.out.println("Valuesare:"+d+" ");
}
}

publicstaticSet<Double>calculate(doublea,doubleb,doublec){
Set<Double>sets=newHashSet<Double>();
if(Math.pow(b,2.0)-4*a*c<0){
System.err.println("Novalue");
}else{
sets.add((-b+Math.sqrt(Math.pow(b,2.0)-4*a*c))/2.0*a);
sets.add((-b-Math.sqrt(Math.pow(b,2.0)-4*a*c))/2.0*a);
}
returnsets;
}
}

下午接着写

② java编程题(写出代码)

代码如下:

classBox{

privateintlength;

privateintwidth;

privateintheight;

publicvoidsetBox(intl,intw,inth){
this.length=l;
this.width=w;
this.height=h;
}

publicintvolume(){
returnlength*width*height;
}
}

publicclassApp{

publicstaticvoidmain(String[]argv){

Boxb=newBox();

b.setBox(3,4,5);

System.out.println("体积:"+b.volume());
}
}

③ 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的编程题目,在线等,急急急

先做两个比较简单的先用:
import java.util.Arrays;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Function {
/**
* 设计一个方法,完成字符串的解析。方法定义为:public void myParseString(String inputStr);
* 对于给定的字符串,取得字符串内的各个整数(不考虑小数,),然后将取得的数排序,按从小到大依次打印出来。
* @param args
*/
public static void main(String[] args) {
String s = "aa789bB22cc345dd;5.a";
new Function().myParseString(s);
}
public void myParseString(String inputStr){
String mathregix="\\d+";//数字
Vector vector=new Vector();
Pattern fun=Pattern.compile(mathregix);
Matcher match = fun.matcher(inputStr);
while (match.find()) {
vector.add(match.group(0));
}
Object[] obj=vector.toArray();
int[] result=new int[obj.length];

for (int i = 0; i < obj.length; i++) {
result[i]=Integer.parseInt((String) obj[i]);
}
Arrays.sort(result);
for (int i = 0; i < result.length; i++) {
System.out.println(result[i]);
}
}
}

import java.util.Date;
/**
* 有一个学生类(Student),其有四个私有属性,分别为:
* 学号no,字符串型;
* 姓名name,字符串型;
* 年龄age,整型;
* 生日birthday,日期型;
* 请:
* 1) 按上面描述设计类;
* 2) 定义对每个属性进行取值,赋值的方法;
* 3) 要求学号不能为空,则学号的长度不能少于5位字符,否则抛异常;
* 4) 年龄范围必须在[1,500]之间,否则抛出异常;
*/
public class Student {
private String no;
private String name;
private int age;
private Date birthday;
public int getAge() {
return age;
}
public void setAge(int age) throws Exception {
if(age<1||age<500)throw new Exception("年龄不合法。");
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNo() {
return no;
}
public void setNo(String no) throws Exception {
if(no==null)throw new Exception("学号不能为空!");
if(no.length()<5)throw new Exception("学号不能少于五位!");
this.no = no;
}
}

二、三题
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

/**
* 2. 设计一个GUI界面,要求打界面如下:
*点击文件->打开,打开文件选择器,选择打开给定的java.txt文件,将文件内所有a-z,A-Z之间的字符显示在界面的文本区域内。
* 3. 设置一个GUI界面,界面如下:
* 点击文件->保存,打开文件选择窗体,选择一个保存路径,将本界面文本区域内输入的内容进行过滤,将所有非a-z,A-Z之间的字符保存到C盘下的java.txt文件内。
* Java.txt文件内容如下:
* 我们在2009年第2学期学习Java Programming Design,于2009-6-16考试。
*
*
*
*/
public class FileEditer extends javax.swing.JFrame implements ActionListener {
private JMenuBar menubar;
private JMenu file;
private JMenuItem open;
private JTextArea area;
private JMenuItem save;
private JFileChooser jfc;

/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
FileEditer inst = new FileEditer();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}

public FileEditer() {
super();
initGUI();
}

private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
area = new JTextArea();
getContentPane().add(area, BorderLayout.CENTER);
area.setText("文本区");
}
{
menubar = new JMenuBar();
setJMenuBar(menubar);
{
file = new JMenu();
menubar.add(file);
file.setText("文件");
file.setPreferredSize(new java.awt.Dimension(66, 21));
{
open = new JMenuItem();
file.add(open);
open.setText("打开");
open.setBorderPainted(false);
open.setActionCommand("open");
open.addActionListener(this);
}
{
save = new JMenuItem();
file.add(save);
save.setActionCommand("save");
save.setText("保存");
save.addActionListener(this);
}
}
}
{
jfc=new JFileChooser();
}
pack();
setSize(400, 300);
} catch (Exception e) {
e.printStackTrace();
}
}

public void actionPerformed(ActionEvent e) {
if("open".equals(e.getActionCommand())){
int result=jfc.showOpenDialog(this);
if(result==jfc.APPROVE_OPTION){
File file=jfc.getSelectedFile();
try {
byte[] b=new byte[1024];
FileInputStream fis=new FileInputStream(file);
fis.read(b);
fis.close();
String string=new String(b,"GBK");
string=string.replaceAll("[^a-zA-Z]*", "");
area.setText(string.trim());
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException es) {
// TODO Auto-generated catch block
es.printStackTrace();
}

}
}else if("save".equals(e.getActionCommand())){
int result=jfc.showSaveDialog(this);
if(result!=jfc.APPROVE_OPTION)return;
File file=jfc.getSelectedFile();
try {
if(!file.exists()){
file.createNewFile();
}
String string = area.getText();
string=string.replaceAll("[a-zA-Z]*", "");
byte[] b=string.getBytes();
FileOutputStream fis=new FileOutputStream(file);
fis.write(b);
fis.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException es) {
// TODO Auto-generated catch block
es.printStackTrace();
}

}
}

}

⑤ java题目编程题

首先初始化一个HashMap,存入一些用户名和电话号码的初始值,然后分别使用map的API来进行相应的map增删改查操作就行了。

增/改:put(K key,V value),这里可以初始化用户名为key,电话号码为value,前提是用户名要没有重复的,如果是相同的用户名会覆盖前一个,可以当做改功能来使用
删:remove(Object key)根据键,删值,如果之前的key存的用户名,那么这里就是根据用户名删值
查:get(Object key)根据键查值,相当于根据用户名查电话号码

⑥ 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经典编程题50道百度网盘地址

这是你要的东西。 拿好

⑧ 一道JAVA编程题

class
WangTi2
{
public
static
void
main(String[]
args)
{
long
start
=
System.currentTimeMillis();//看一下要运行多长时间
shuanShu();
long
end
=
System.currentTimeMillis();//看一下要运行多长时间
System.out.println("用时"+(end-start));
}
public
static
void
shuanShu()
{
int[]
arr
=
new
int[100];
arr[0]
=
1;
for(int
x=0;x<11213;x++)
//可以把11213改成100验证方法的正确性
{
//2^20=1048576
int
z
=
0;
/*
这个循环是记录乘2的结果
*/
for(int
y
=0;y<arr.length;y++)
{
arr[y]
=
arr[y]<<1;
arr[y]
=
arr[y]
+
z;
if
(arr[y]>9)
{
arr[y]
-=
10;
if(y!=arr.length-1)
z
=
1;
}
else
z
=
0;
}
}
arr[0]--;
//这个给最后一个位减1,这个值不会是负数。
System.out.println("这个数的最后100位是:");
for(int
x=arr.length-1;x>=0;x--)
{
System.out.print(arr[x]);
if(x%3==0&&x!=0)
System.out.print(",");
}
System.out.println();
}
}
思路是有的。定义数组,只存储最后100位。然后不停的乘2,大于9的向上一个数组加1。重复11213次。再把第一个数组减1。这样做是可以的。效率很低。求高人解答。。呵呵。

阅读全文

与java经典编程题相关的资料

热点内容
自己购买云主服务器推荐 浏览:419
个人所得税java 浏览:760
多余的服务器滑道还有什么用 浏览:189
pdf劈开合并 浏览:26
不能修改的pdf 浏览:750
同城公众源码 浏览:488
一个服务器2个端口怎么映射 浏览:297
java字符串ascii码 浏览:78
台湾云服务器怎么租服务器 浏览:475
旅游手机网站源码 浏览:332
android关联表 浏览:945
安卓导航无声音怎么维修 浏览:332
app怎么装视频 浏览:430
安卓系统下的软件怎么移到桌面 浏览:96
windows拷贝到linux 浏览:772
mdr软件解压和别人不一样 浏览:904
单片机串行通信有什么好处 浏览:340
游戏开发程序员书籍 浏览:860
pdf中图片修改 浏览:288
汇编编译后 浏览:491