導航:首頁 > 編程語言 > java程序例題

java程序例題

發布時間:2022-08-17 07:56:13

1. 一道簡單的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();
}
}

2. java 編程題

//People.java

publicclassPeople{
Stringname;
intage;
Stringgender;
doubleheight;

People(Stringn,inta,Stringg,doubleh){
this.name=n;
this.age=a;
this.gender=g;
this.height=h;
}

publicvoidspeak(Stringwords){
System.out.println(words);
}

publicvoidcalculate(inta,intb){
System.out.println(a+b);
}

publicvoidrename(Stringnew_name){
Stringprevious_name=this.name;
this.name=new_name;
System.out.println(previous_name+"改名為"+this.name);
}

}


//Main.java

publicclassMain{

publicstaticvoidmain(String[]args){

Peopleperson=newPeople("張三",18,"男",1.8);
person.speak("你好!");
person.calculate(23,45);
person.rename("李四");

}
}

這兩個類需放在不同的文件內。

3. 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());
}
}

4. java編程十道題

1.
public class TestRandomArray
{
public static void main(String[] args)
{
int maxline = (int) (Math.random()*100);
int[] array = new int[maxline];
for(int i=0;i<maxline;i++)
{
int temp = (int) (Math.random()*100);
if(temp>=10&&temp<=99)
{
array[i] = temp;
}
else
i--;
}
for(int j=0;j<maxline;j++)
{
int max = array[0];

if(array[j]>max)
{
max=array[j];
array[j] = array[0];
array[0] = max;

}
}
for(int k=0;k<maxline;k++)
{
System.out.print(array[k]+" ");
}
}
}

2.
public class TestNumber100
{
public static void main(String[] args)
{
for(int i=1;i<100;i++)
{
if(i%3==0&&(i%5!=0)&&(i%9!=0))
System.out.println(i+" ");
else
continue;
}
}
}

3.貌似沒結果
public class Test3
{
public static void main(String[] args)
{
/*for(int i=0;i<=9;i++)
{
for(int j=0;j<=9;j++)
{
int code = 10000+i*1000+j*100+23;
if((code%57)==0&&(code%67)==0&&i!=j)
{
System.out.println("x="+i+","+"y="+j);
}
else
System.out.println(i+" "+j);
}
}*/

for(int i=10023;i<=19923;i++)
if(i==0&&i==0)
System.out.println(i+" ");
}
}

4.
class Caculate {
int money;
Caculate(){
money = 10;
}
public int charge (int distance){
if (distance < 3){
money = 10;
}
if ((distance >= 3) && distance < 15)
{
money = money + 2*(distance-3);
}
if (distance > 15 )
{
money = money + 2*12 + 3*(distance - 15);
}

return money;
}
public static void main(String args[]) {

System.out.println("請輸入公里數");
int distance_example=0;
Scanner key = new Scanner(System.in);
int n = key.nextInt();
distance_example = n;
Caculate pay = new Caculate();
pay.money = pay.charge(distance_example);
System.out.println("You need to pay money:"+pay.money+" distance is:"+distance_example);

}
}

5.
public class Test4
{

public static void main (String args[])
{
int n=100;
while(n<=1000)
{
int i,j,k;
i=n/100;
j=(n-i*100)/10;
k=n%10;
if((Math.pow(i, 3)+Math.pow(j, 3)+Math.pow(k, 3))==n)

System.out.print(n+String.valueOf('\t'));
n++;
}
}
}

6.
public class TestMoney
{
public static void main(String[] args)
{
int one=1,two=2,five=5;
for(int i=1;i<100;i++)
for(int j=1;j<50;j++)
for(int k=1;k<20;k++)
{
if((i*one+j*two+k*five)==100)
System.out.println(i+"個一分,"+j+"個兩分,"+k+"個五分");
}

}

}

7.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TestJieCheng
{
public static void main (String args[])
{
int a=0,b=1;float sum=0;
System.out.print("請輸入要計算的階乘:");
try
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
a=Integer.parseInt(in.readLine());
}catch(IOException e){}
for(int i=1;i<=a;i++)
{
b=b*i;

}
System.out.print(a+"的階乘是"+b);
}
}

8.
public class Test4
{

public static void main (String args[])
{
int n=100;
while(n<=1000)
{
int i,j,k;
i=n/100;
j=(n-i*100)/10;
k=n%10;
if((Math.pow(i, 3)+Math.pow(j, 3)+Math.pow(k, 3))==n)

System.out.print(n+String.valueOf('\t'));
n++;
}
}
}

9.
public class TestNumber2
{
public static void main(String[] args)
{
final int maxline = 20;
double sum = 0;
double [] denominator = new double [maxline];
double [] numerator = new double [maxline];
for(int i=2;i<denominator.length;i++)
{
denominator[0]=1;
denominator[1]=2;
denominator[i]=denominator[i-1]+denominator[i-2];
}
for(int j=2;j<numerator.length;j++)
{
numerator[0]=2;
numerator[1]=3;
numerator[j]=numerator[j-1]+numerator[j-2];
}
for(int k=0;k<maxline;k++)
{
sum+=(numerator[k])/denominator[k];

}
System.out.println(sum);
}

}

10.
import java.io.*;

public class 根據輸入的三個數求一元二次方程
{
public static void main (String args[]) throws NumberFormatException, IOException
{
float a[]=new float [3];
for(int i=0;i<a.length;i++)
{
System.out.println("請輸入第"+(i+1)+"個數");
BufferedReader in =new BufferedReader(new InputStreamReader(System.in));
a[i]=Float.parseFloat(in.readLine());
}
float x1=0,x2=0;
x1=(float) ((((-1)*a[1])+(Math.sqrt(a[1]*a[1]-4*a[0]*a[2])))/(2*a[0]));
x2=(float) ((((-1)*a[1])-(Math.sqrt(a[1]*a[1]-4*a[0]*a[2])))/(2*a[0]));
System.out.print(a[0]+"X2"+a[1]+"X"+a[2]+"的兩個根是:");
System.out.print(x1+"和"+x2);
}
}

5. 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();
}
}

6. 有幾個java編程的題各位好心人有時間的能幫忙寫下嗎

沒那麼多時間,幫著寫個第1題吧

//編寫求一個整數數組A[10,15,12,9,7]中最小元素min和元素之和sum的

int[]a={10,15,15,9,7};

//最小元素

intmin=0;

//數組和

intsum=0;


for(inti=0;i<a.length;i++){

sum+=a[i];

if(i==0){

min=a[i];

}else{

if(a[i]<min){

min=a[i];

}

}

}


System.out.println("當前數組中最小的元素值是:"+min);

System.out.println("當前數組和是:"+sum);

7. 簡單的java程序題

public
class
student{
private
string
stuid;
private
string
stuname;
private
char
stusex;
private
int
stuage;
/**
*以下是各屬性的getter/setter方法
*/
public
void
setstuid(string
stuid){
this.stuid=stuid;
}
public
string
getstuid(){
return
stuid;
}
public
void
setstuname(string
stuname){
this.stuname=stuname;
}
public
string
getstuname(){
return
stuname;
}
public
void
setstusex(char
stusex){
this.stusex=stusex;
}
public
char
getstusex(){
return
stusex;
}
public
void
setstuage(int
stuage){
this.stuage=stuage;
}
public
int
getstuage(){
return
stuage;
}
/**
*構造方法,構造學生信息
*/
public
student(string
stuid,string
stuname,char
stusex,int
stuage){
this.stuid=stuid;
this.stuname=stuname;
this.stusex=stusex;
this.stuage=stuage;
}
public
string
tostring(){//覆蓋該類的tostring()方法
stringbuffer
buff=new
stringbuffer();
buff.append("學號:"+stuid);
buff.append("\n姓名:"+stuname);
buff.append("\n性別:"+stusex);
buff.append("\n年齡:"+stuage);
return
buff.tostring();
}
public
static
void
main(string[]
args){
student
stu=new
student("1000","zhangsan",'男',18);
system.out.println
(stu);//列印學生信息
system.out.println
("--修改姓名結果--");
stu.setstuname("lisi");
system.out.println
(stu);
}
}

8. java程序設計題

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
BufferedOutputStream out = null;
try {
out = new BufferedOutputStream(new FileOutputStream(new File("d:/info.txt")));
String line = "第一行文本\n第二行文本";
out.write(line.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
BufferedInputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream("d:/info.txt"));
StringBuffer buffer = new StringBuffer();
byte[] buff = new byte[in.available()];
while (in.read(buff) != -1) {
buffer.append(new String(buff));
}
System.out.println(buffer);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
in = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

9. 8道簡單的Java編程題

第一題
private static void tuzinum(){
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
//總數
int allnum = 1;
//成年數
int chengniannum = 0;
//出生數
int chushengnum = 0;
for(int i=0;i<12;i++){
for(int j=0;j<list.size();j++){
Integer age = list.get(j);
age++;
list.set(j, age);
if(age>3){
chengniannum++;
chushengnum++;
allnum++;
list.add(1);
}
}
System.out.println("第"+i+"個月兔子對數為:"+list.size());
}
}

第二題
private static void fenjie(){
int num = 90;

boolean canchu = true;
while(canchu){
int time = 0;
for(int i=2;i<num/2;i++){
if(num%i==0){
num = num/i;
System.out.println(i);

time++;
}
}
if(time == 0 ){
canchu = false;
System.out.println(num);
}
}
}

閱讀全文

與java程序例題相關的資料

熱點內容
java筆試編程題 瀏覽:741
win11什麼時候可以裝安卓 瀏覽:560
java不寫this 瀏覽:999
雲點播電影網php源碼 瀏覽:95
pythonclass使用方法 瀏覽:224
移動加密軟體去哪下載 瀏覽:294
php彈出alert 瀏覽:209
吉林文檔課件加密費用 瀏覽:136
感測器pdf下載 瀏覽:289
隨車拍app綁定什麼設備 瀏覽:898
方維團購系統源碼 瀏覽:993
linux反彈shell 瀏覽:159
列印機介面加密狗還能用嗎 瀏覽:301
二板股票源碼 瀏覽:448
度人經pdf 瀏覽:902
怎麼配置android遠程伺服器地址 瀏覽:960
java程序員看哪些書 瀏覽:943
什麼app可以免費和外國人聊天 瀏覽:797
pdf手寫筆 瀏覽:182
別永遠傷在童年pdf 瀏覽:990