java求职宝典习题4.docx
《java求职宝典习题4.docx》由会员分享,可在线阅读,更多相关《java求职宝典习题4.docx(15页珍藏版)》请在冰豆网上搜索。
java求职宝典习题4
1.结构化程序设计有哪三种流程?
他们分别对应Java中那些语句。
结构化程序设计有三种基本流程:
循环、分支和顺序。
Java程序中的分支语句包含if语句、switch语句;循环语句包括了while语句,do-while语句、for语句;其他语句如变量、对象定义、赋值语句、方法调用语句、以及上面的循环结构、分支结构等按照上下文排列都是顺序语句。
2.在一个循环中使用break、continue和return有什么不同?
break用于跳出整个循环语句,在循环结构中一旦遇到break语句,不管循环条件如何,程序立即退出所在的循环体。
continue用于跳过本次循环中尚未执行的语句,但是仍然继续执行下一次循环中的语句。
在循环中使用return语句,将终止当前方法调用,同时终止循环,使流程返回到调用语句的下一个语句执行。
3.面代码将输出:
________
a=9;b=18;c=4;d=14;e=-14;f=-2
g=18.4;h=2.3999999999999986;i=5;j=3;k=5
public class test3{
public static void main(String args[]){
int a=5+4;
int b=a*2;
int c=b/4;
int d=b-c;
int e=-d;
int f=e%4;
double g=18.4;
double h=g%4;
int i=3;
int j=i++;
int k=++i;
System.out.println(“a=”+a+”;b=”+b+”;c=”+c+”;d=”+d+”;e=”+e+”;f=”+f);
System.out.println(“g=”+g+”;h=”+h+”;i=”+i+”;j=”+j+”;k=”+k);
}
}
4.下面代码将输出:
________
25<3=false
3!
=0&&25/3>5=true
0!
=0&&25/0>5=false
public class LogicTest{
public static void main(String args[]){
int a=25,b=3;
boolean d=a
System.out.println(a+”<“+b+”=”+d);//=;
int e=3;
d=(e!
=0&&a/e>5);
System.out.println(e+”!
=0&&”+a+”/”+e+”>5=”+d);
int f=0;
d=(f!
=0&&a/f>5);
System.out.println(f+”!
=0&&”+a+”/”+f+”>5=”+d);
}
}
5.编写程序,求两个整数的最大公约数。
import java.util.Scanner;
public class Gcd_Lcm{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println(“输入2个数:
以‘,’ 隔开“);
String []str = sc.next().split(“,”);
int m = Integer.parseInt(str[0]);
int n = Integer.parseInt(str[1]);
int min = m>n?
n:
m;
int max = m>n?
m:
n;
int num1 =1;
int num2 = max;
for (int i = min; i>0; i–) {
if (m%i==0&&n%i==0) {
num1 = i;break;
}
}
while (true) {
if (num2%m==0&&num2%n==0) {
break;
}
num2 = m*n>num2*2?
num2*2:
m*n;
}
System.out.println(“最大公约数:
“+num1+” 最小公倍数:
”+num2);
}
}
6.编写程序,打印出如下九九乘法表。
* | 1 2 3 4 5 6 7 8 9
——-|——————————————————-
1 | 1
2 | 2 4
3 | 3 6 9
4 | 4 8 12 16
5 | 5 10 15 20 25
6 | 6 12 18 24 30 36
7 | 7 14 21 28 35 42 49
8 | 8 16 24 32 40 48 56 64
9 | 9 18 27 36 45 54 63 72 81
public class NineByNineMul{
public static void main(String args[]){
System.out.print(“ * |”);
for(int i=1;i<=9;i++){
System.out.print(“ ”+i+” ”);
}
System.out.println();
System.out.print(“——-|—–”);
for(int i=1;i<=9;i++){
System.out.print(“—–”);
}
System.out.println();
for(int i=1;i<=9;i++){
System.out.print(“ ”+i+” | ”);
for(int j=1;j<=i;j++){
System.out.print(i*j+” ”);
}
System.out.println();
}
}
}
7.下面代码将输出:
one two default
int i = 1;
switch (i) {
case 0:
System.out.println(“zero”);
break;
case 1:
System.out.println(“one”);
case 2:
System.out.println(“two”);
default:
System.out.println(“default”);
}
8.下面代码将输出:
Equal
class EqualsTest {
public static void main(String[] args) {
char a=’\u0005′;
String s=a==0x0005L?
“Equal”:
”Not Equal”;
System.out.println(s);
}
}
9.编写程序,对A[]={30,1,-9,70,25}数组由小到大排序。
public class booktest {
public static void main(String[] args) {
int a[]={30,1,-9,70,25};
System.out.print(“数组原始顺序:
“);
for (int i=0;ifor (int i = 0; i < a.length; i++) {
int lowerIndex = i;
for (int j = i + 1; j < a.length; j++)
if (a[j] < a[lowerIndex]) lowerIndex = j;
int temp = a[i];
a[i] = a[lowerIndex];
a[lowerIndex] = temp;
}
System.out.print(“\n数组排序后的顺序:
“);
for (int i=0;i}
}
10.运行下面代码将输出什么内容?
one
int i=1;
switch(i){
case 0:
System.out.println(“zero”);
break;
case 1:
System.out.println(“one”);
break;
case 2:
System.out.println(“two”);
break;
default:
System.out.println(“default”);
}
11.编写程序,求2-1000内的所有素数,并按每行5列的格式输出。
public class PrimeTest{
public static void main(String args[]) {
int num=2;
System.out.print(2 + ” ”);
for(int i=3;i<=1000;i+=2){
boolean f = true;
for (int j=2;j
if(i % j == 0){
f= false;
break;
}
}
if(!
f) {continue;}
System.out.print(i + ” ”);
if(num++%5 == 0)System.out.println();
}
}
}
12.编写程序,生成100个1~6之间的随机数,统计1~6每个数字出现的概率。
public class RandomTest {
public static void main(String[]args){
int[] randomnum=new int[100];
int[] n=new int[6];
double a;
for(int i=0;i<100;i++){
a = Math.random()*6;
a = Math.ceil(a);
randomnum[i] = new Double(a).intValue();
System.out.print(randomnum[i]);
switch (randomnum[i]){
case 1:
n[0]++; break;
case 2:
n[1]++; break;
case 3:
n[2]++; break;
case 4:
n[3]++; break;
case 5:
n[4]++; break;
case 6:
n[5]++; break;
}
}
System.out.println();//以下可改为循环输出
System.out.println(“ 数字1出现的概率=”+(n[0]/100.0)*100+”%”);
System.out.println(“ 数字2出现的概率=”+(n[1]/100.0)*100+”%”);
System.out.println(“ 数字3出现的概率=”+(n[2]/100.0)*100+”%”);
System.out.println(“ 数字4出现的概率=”+(n[3]/100.0)*100+”%”);
System.out.println(“ 数字5出现的概率=”+(n[4]/100.0)*100+”%”);
System.out.println(“ 数字6出现的概率=”+(n[5]/100.0)*100+”%”);
}
}
13.编写程序,求1!
+2!
+3!
+…+15!
。
public class Fact