導航:首頁 > 編程語言 > 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經典編程題相關的資料

熱點內容
同城公眾源碼 瀏覽:472
一個伺服器2個埠怎麼映射 瀏覽:280
java字元串ascii碼 瀏覽:59
台灣雲伺服器怎麼租伺服器 瀏覽:458
旅遊手機網站源碼 瀏覽:312
android關聯表 瀏覽:927
安卓導航無聲音怎麼維修 瀏覽:318
app怎麼裝視頻 瀏覽:421
安卓系統下的軟體怎麼移到桌面 瀏覽:78
windows拷貝到linux 瀏覽:753
mdr軟體解壓和別人不一樣 瀏覽:886
單片機串列通信有什麼好處 瀏覽:322
游戲開發程序員書籍 瀏覽:846
pdf中圖片修改 瀏覽:273
匯編編譯後 瀏覽:478
php和java整合 瀏覽:833
js中執行php代碼 瀏覽:445
國產單片機廠商 瀏覽:60
蘋果手機怎麼設置不更新app軟體 瀏覽:287
轉行當程序員如何 瀏覽:496