文档库 最新最全的文档下载
当前位置:文档库 › java编程题

java编程题

java编程题
java编程题

JavaSE初级—第一单元:JAVA语言概述,简单的JAVA

程序解析

1:java语言的特点是什么?

答:java语言有完全面向对象、夸平台:一处编译,随

处运行、封装、多肽、健壮、简单的、平台无关、多线程、分布式、安全、高性能、可靠的、解释型、自动垃圾回收等特点。

2:举例写出在java发展史上对java有深刻影响的公司名称?

答:Sum,IBM,Oracle公司

3:使用java实现输出 hello world!!

public class HelloWorld {

public static void main(String[] args) {

System.out.println("hello world!!");

}

}

4:问题:System.out.println()和System.out.print()有什么区别呢?

以下代码的运行效果是什么?

System.out.println("我的爱好:");

System.out.println("打网球");

System.out.print("我的爱好:");

System.out.print("打网球");

答:System.out.println()打印完括号里面的内容要换一行;而ystem.out.print()打印完括号里面的内容不用换一行,后面接着打印。

代码的运行效果是:我的爱好:

打网球

我的爱好:打网球

JavaSE初级—第二单元:变量、常量和基本数据类型1:列举java里面的8中基本数据类型?

答byte,boolean,char,int,short,long,float,double

2:每种基本数据类型都定义一个常量。(八种)

答:byte a=126;

boolean b=true;

char c=’c’;

int i=10;

short s=123;

Long L=123L;

float f=12.0f;

Double d=12.0d;

3:每种基本数据类型都定义一个变量。

答:byte a;boolean b;

char c;int i;

short s;long L;

float f;double d;

4:计算下列表达式的结果:

10/3 ; 10/5 ; 10%2 ; 10.5%3;

答:10/3 =3;10/5=2;10%2=0;10.5%3=1.5

JavaSE初级—第三单元:运算符,表达式及空语句

1:为抵抗洪水,战士连续作战89小时,编程计算共多少天零多少小时?

public class DayHour{

public static void main(String[] args){

int hour=89;

int day;

day=hour/24;

hour=hour%12;

System.out.println(day+"天零"+hour+"小时");

}

}

2:小明要到美国旅游,可是那里的温度是以华氏度为单位记录的。它需要一个程序将华氏温度(80度)转换为摄氏度,并以华氏度和摄氏度为单位分别显示该温

度。

提示:摄氏度与芈氏度的转换公式为:摄氏度 =

5/9.0*(华氏度-32)

public class Temperature{

public static void main(String[] args){

int hua=80;

double c;

c = 5/9.0*(hua-32);

System.out.println("此时温度为:华氏温度"+hua+"华氏度,"+"摄氏温度"+c+"摄氏度");

}

}

3:根据你的理解,说明一下“==”和“=”的区别。

答:“==”是等于号,判断两个数值是否相等;

“=”是赋值符号。

JavaSE初级—第四单元:程序结构设计顺序结构,选择结构

1:写一个方法,此方法实现判断一个整数,属于哪个范围:大于0;小于0;等于0

public class Judge{

public static void main(String[] args){

int i=-10;

if (i>0) {

System.out.println(i+"大于0");

}else if(i==0){

System.out.println(i+"等于0");

}else{

System.out.println(i+"小于0");

}

}

}

2:写一个方法,此方法实现判断一个整数是偶数还是奇数。

public class Judge{

public static void main(String[] args){

int i=9;

if (i%2==0) {

System.out.println(i+"是偶数");

}else{

System.out.println(i+"是奇数");

}

}

}

3:写一个方法,此方法实现对三个整数进行排序,输

出时按照从小到大的顺序输出。

import java.util.Arrays;

public class Sort{

public static void main(String[] args){

int arr[]={43,21,68};

Arrays.sort(arr);

System.out.println(Arrays.toString(arr));

}

}

JavaSE初级—第五单元:循环结构

1:求10以内的偶数的和。

public class Sum{

public static void main(String[] args){

int s=0;

for (int i = 0; i <=10; i++) {

if (i%2==0) {

s=s+i;

}

}

System.out.println(s);

}

}

2:求100以内的所有素数

public class Prime{

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

boolean b=true;

for (int j =2; j

if(i%j==0){

b=false;

break;

}

}

if(b){

System.out.println(i);

}

}

}

3:随机产生一个1-100之间的整数,看能几次猜中。要求:猜的次数不能超过7次,每次猜完之后都要提示“大了”或者“小了”。

import java.util.Scanner;

public class Guess{

public static void main(String[] args){

int b=(int)(100*Math.random())+1;

Scanner scanner=new Scanner(System.in);

for (int i = 1; i <=7; i++) {

System.out.println("请猜数字:");

int a=scanner.nextInt();

if (a>b) {

System.out.println("大了");

}else if (a

System.out.println("小了");

}else{

System.out.println("恭喜您猜对了,一共猜了"+i+"次");

break;

if (i==7) {

System.out.println("您已经猜了7次了");

}

}

}

}

4:写一个方法,此方法实现判断某年某月某日是这一年的第几天?(年月日通过方法的参数提供)

public class NYR {

public static void main(String[] args) {

int n,y,r;

getNYR nyr=new getNYR();

n=nyr.getN();

y=nyr.getY();

r=nyr.getR();

boolean b = n % 4 == 0 && n % 100 != 0 || n % 400 == 0;

if(y > 0 && y <= 12){

switch (y) {

case 1:

if (r > 0 && r <= 31) {

月"+r+"号是第"+r+"天。");

}

break;

case 2:

if (b) {

if(r > 0 && r <= 29){

System.out.println(n+"年"+y+"月"+r+"号是第"+(31+r)+"天。");

}

break;

} else {

if(r > 0 && r <= 28){

System.out.println(n+"年"+y+"月"+r+"号是第"+(31+r-1)+"天。");

}

break;

}

case 3:

if (r > 0 && r <= 31) {

if (b) {

System.out.println(n+"年"+y+"月"+r+"号是第"+(60+r)+"天。");

} else {

"+y+"月"+r+"号是第"+(60+r-1)+"天。");

}

}

break;

case 4:

if (r > 0 && r <= 30) {

if (b) {

System.out.println(n+"年"+y+"月"+r+"号是第"+(90+r)+"天。");

} else {

System.out.println(n+"年"+y+"月"+r+"号是第"+(90+r-1)+"天。");

}

}

break;

case 5:

if (r > 0 && r <= 31) {

if (b) {

System.out.println(n+"年"+y+"月"+r+"号是第"+(121+r)+"天。");

} else {

System.out.println(n+"年"+y+"月"+r+"号是第"+(121+r-1)+"天。");

}

}

break;

case 6:

if (r > 0 && r <= 30) {

if (b) {

System.out.println(n+"年"+y+"月"+r+"号是第"+(151+r)+"天。");

} else {

System.out.println(n+"年"+y+"月"+r+"号是第"+(151+r-1)+"天。");

}

}

break;

case 7:

if (r > 0 && r <= 31) {

if (b) {

System.out.println(n+"年"+y+"月"+r+"号是第"+(182+r)+"天。");

} else {

System.out.println(n+"年"+y+"月"+r+"号是第"+(182+r-1)+"天。");

}

}

break;

case 8:

if (r > 0 && r <= 31) {

if (b) {

System.out.println(n+"年"+y+"月"+r+"号是第"+(213+r)+"天。");

} else {

System.out.println(n+"年"+y+"月"+r+"号是第"+(213+r-1)+"天。");

}

}

break;

case 9:

if (r > 0 && r <= 30) {

if (b) {

System.out.println(n+"年"+y+"月"+r+"号是第"+(243+r)+"天。");

} else {

System.out.println(n+"年"+y+"月"+r+"号是第"+(243+r-1)+"天。");

}

}

break;

case 10:

if (r > 0 && r <= 31) {

if (b) {

System.out.println(n+"年"+y+"月"+r+"号是第"+(274+r)+"天。");

} else {

System.out.println(n+"年"+y+"月"+r+"号是第"+(274+r-1)+"天。");

}

}

break;

case 11:

if (r > 0 && r <= 30) {

if (b) {

System.out.println(n+"年"+y+"月"+r+"号是第"+(304+r)+"天。");

} else {

System.out.println(n+"年"+y+"月"+r+"号是第"+(304+r-1)+"天。");

}

}

break;

case 12:

if (r > 0 && r <= 31) {

if (b) {

System.out.println(n+"年"+y+"月"+r+"号是第"+(335+r)+"天。");

} else {

System.out.println(n+"年"+y+"月"+r+"号是第"+(335+r-1)+"天。");

}

}

break;

}

}

}

}

class getNYR{

int n=2015;

int y=9;

int r=1;

public int getN() {

return n;

}

public int getY() {

return y;

}

public int getR() {

return r;

}

}

JavaSE初级—第六单元:循环结构

1:实现双色球的彩票功能。规则:从36个红球中随机选择不重复的6个数,从15个篮球中随机选择1个组成一注彩票。可以选择买多注。

import java.util.Arrays;

public class Ticket {

public static void main(String[] args) {

int []a =new int[7];

a[0]=(int)(36*Math.random()+1);

int t;

for (int i = 1; i < 6; i++) {

t=(int)(6*Math.random()+1);

for(int j=0;j

if(t==a[j]){

i--;

break;

}else{

a[i]=t;

}

}

}

a[6]=(int)(36*Math.random()+1);

System.out.println(Arrays.toString(a));

}

}

2:输出1-100之间的不能被5整除的数,每5个一行。

public class Number{

public static void main(String[] args){

int t=0;

for (int i = 1; i <= 100; i++) {

if(i%5!=0){

System.out.print(i+"");

t++;

if(t%5==0){

System.out.println();

}

}

}

}

}

JavaSE初级—第七单元面向对象的基本概念

1:写一个人的类

属性:名字,性别,年龄

方法:(1)自我介绍的方法(2)吃饭的方法

创建一个对象“张三”

public class Person{

String name="张三";

char sex='男';

int age=21;

public static void main(String[]args){

Person 张三 = new Person();

张三.introduceMySelf();

张三.eat();

}

public void introduceMySelf(){

System.out.println("大家好,我叫"+name+",性别"+sex+",今年"+age+"岁");

}

public void eat(){

System.out.println("用筷子吃饭");

}

}

2:写一个汽车类:

属性:品牌;车长;颜色;价格;

方法:跑的方法

创建五个对象:“捷达”,“宝马”,“劳斯莱斯”,

“科鲁兹”,“迈锐宝”

public class Car{

public static void main(String[]args){

Message jieDa=new Message("捷达", 4.48, "

黑色",68000);

Message baoMa=new Message("宝马",5.17,"白色",654000);

Message laoSi=new Message("劳斯莱斯", 4.12, "棕色", 34000);

Message keLu=new Message("科鲁兹", 4.11, "白色", 45000);

Message maiKe=new Message("迈锐宝",4.23,"

雪白色",34500);

runWay();

}

public static void runWay(){

System.out.println("捷达横着跑");

System.out.println("宝马直线跑");

System.out.println("劳斯莱斯转弯跑");

System.out.println("科鲁兹上坡跑");

System.out.println("迈锐宝飞速跑");

}

}

class Message{

private String brand;

private double length;

private String color;

private double price;

public Message(String brand, double length, String color, double price) {

this.brand = brand;

this.length = length;

this.color = color;

this.price = price;//品牌;车长;颜色;价格;

System.out.println("品牌:"+brand+" 车长:"+length+" 颜色:"+color+" 价格:"+price);

}

}

相关文档