您的位置:首页 > 编程语言 > Java开发

Java_每天学习一点点之Java开发实战经典第三章习题

2017-02-10 05:58 549 查看
特别声明:如下内容均来自于不同网络课程视频或书籍,整理合并完成。 如果有整理错误,忘及时指出,共同进步,不胜感激。

Java 开发实战经典(习题及答案)

Java 第三章

1. 顺序结构: 打印出1…10000范围中所有的“水仙花数”,所谓水仙花数指一个三位数,其各位数字和等于该数本身,例如: 153是一个水仙花数,因为153=(1的三次方加上5的三次方加上3的三次方加上)

   答案:

public class Practical_1 {

public static void main(String[] args) {
// TODO Auto-generated method stub

for(int i =1;i<=10000;i++)
{
if(i>=100 && i<=999)
{
if((Math.pow(i/100, 3)+Math.pow((i/10)%10, 3)+Math.pow(i%10, 3))==i)
{
System.out.println(i);
}
}
}

}


2. 通过代码完成两个数的内容交换

   答案:

public class Practical_2 {

public static void main(String[] args) {
// TODO Auto-generated method stub

int a = 10;
int b = 20;
int c;
c = a;
a = b;
b = c;
System.out.println(a+","+b);
}


3. 给定三个数,求三个数中最大值

   答案:

public class Practical_3 {

public static void main(String[] args) {
// TODO Auto-generated method stub

int a = 14;
int b = 13;
int c = 6;
int temp = 0;
int max = 0;

temp = (a > b)? a:b;
max = (temp > c)? temp:c;
System.out.print("最大值为: "+max);
}
}


4. 判断某数能否被3,5,7同时整除

   答案:

public class Practical_4 {

public static void main(String[] args) {
// TODO Auto-generated method stub

int a = 35;
if((a%3)==0 && (a%5)==0 && (a%7)==0)
{
System.out.println("This value can be divided by 3,5 and 7");
}
else
{
System.out.println("This value can not be divided by 3,5 and 7");
}
}
}


5. 分别用while,do…while,for求100到200的累加值

   答案:

public class Practical_5 {

public static void main(String[] args) {
// TODO Auto-generated method stub

// for loop
int sum_loop = 0;
for(int i = 100;i <= 200; i++)
{
sum_loop = i + sum_loop;
}
System.out.println("This is for method: "+sum_loop);

// while loop
int sum_while = 0;
int i = 100;
while(i <= 200)
{
sum_while = sum_while + i;
i++;
}
System.out.println("This is while method: "+sum_while);

// do while loop
int sum_do_while = 0;
int x = 100;
do
{
sum_do_while = sum_do_while + x;
x++;

}
while(x <= 200);
System.out.println("This is do...while method: "+sum_do_while);

}

}


6. 求13-23+33-43+…+973-983+993-1003的值

   答案:

public class Practical_6 {

public static void main(String[] args) {
// TODO Auto-generated method stub

int sum = 0;
for(int a = 13; a <= 1003; a = a+10)
{
if((a/10)%2 != 0)
{
sum = sum + a;
}
else
{
sum = sum - a;
}
}
System.out.println(sum);
}

}


7. 三个数中最大值

   答案:

public class Practical_7 {

public static void main(String[] args) {
// TODO Auto-generated method stub

String a = JOptionPane.showInputDialog("Please input a value");
String b = JOptionPane.showInputDialog("Please input a value");
String c = JOptionPane.showInputDialog("Please input a value");
int new_a = Integer.parseInt(a);
int new_b = Integer.parseInt(b);
int new_c = Integer.parseInt(c);
MaxValue(new_a,new_b,new_c);

}

public static void MaxValue(int a, int b, int c)
{
int Max;
int temp;
temp = (a > b)? a:b;
Max = (temp > c)? temp:c;
System.out.println(Max);

}
}


8. 1到100的累加

   答案:

public class Practical_8 {

public static void main(String[]args)
{
int sum = 0;
for(int x = 1; x <=100; x++)
{
sum += x;
}
System.out.println(sum);
}
}


9. 1到1000之间可以被3,5,7整除的数

   答案:

public class Practical_9 {

public static void main(String[] args) {
// TODO Auto-generated method stub

for(int i = 1; i<= 1000; i++)
{
if((i%3) == 0 && (i%5) == 0 && (i%7) ==0)
{
System.out.println(i);
}
}
}
}


10. 1!+2!+3!+….+20!的值

   答案:

public class Practical_10 {

public static void main(String[] args) {
// TODO Auto-generated method stub

int temp = 1;
int sum = 0;
for(int a = 1; a <=20; a++)
{
temp = temp*a;
sum = temp+sum;
}
System.out.println(sum);
}
}


11. for循环打印图形

   答案:

public class Practical_11 {

public static void main(String[] args) {
// TODO Auto-generated method stub

for(int i = 1; i <= 5; i++)
{
for(int x = 1; x <= i; x++)
{
System.out.print("*");
}
System.out.println();
}
}
}


未完待续。。。

更新时间 每周一,三,五,日。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java