Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区 » 学习心得/方法/资源  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 [学习总结]Java2实用教程(第三版)习题3
喝杯茶吃个包





发贴: 7
积分: 1
于 2008-09-12 01:54 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
习题3
1.  下列程序的输出结果是什么?
public class E
{
public static void main (String args[])
{
char x = '你', y = 'e', z = '吃';

if(x > 'A')
{
y = '爱';
z = '情';
}
else
{
y = '我';
}
z = '她';
System.out.println(" " + x + y + z);
}
}

答:
输出结果是:你爱她


2.  下列程序的输出结果是什么?

public class E {
  public static void main(String args[]) {
    char c = '\0';

    for (int i = 1; i <= 4; i++) {
      switch ( i ) {
      case 1:
        c = 'b';
        System.out.print( c );
      case 2:
        c = 'e';
        System.out.print( c );
        break;
      case 3:
        c = 'p';
        System.out.print( c );
      default:
        System.out.print("!");
      }
    }
  }
}

答:
输出结果是:beep!!


3.  编写应用程序,求1!+2!+….+10!。

// 目的: 计算1!+2!+...的前10项的和

class SumOfFactorial
{
public static void main (String args[])
{
long x = 1, sum = 0;

for(int i = 1; i < 11; i++)
{
x *= i;
sum += x;
if(i != 10) System.out.print(i + "!+");
else System.out.print(i + "!");
}
System.out.println("=" + sum);
}
}

运行程序,输出结果为:
1!+2!+3!+4!+5!+6!+7!+8!+9!+10!=4037913


4.  编写一个应用程序,求100以内的全部素数。

// 目的: 求100以内所有素数

public class PrimeNumber
{
public static void main (String args[])
{
int x, i;

for(x = 2; x <= 100; x++)
{
for(i = 2; i <= x / 2; i++)
{
if(x % i == 0) break;
}
if(i > x / 2) System.out.print(x + " ");
}
}
}

运行程序,输出结果是:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97


5.  分别用do-while和for循环计算1+1/2!+1/3!+1/4!+….的前20项和。

// 目的: 计算1+1/2!+1/3!+...前20项的和

public class SumOfSequence
{
public static void main (String args[])
{
double x = 1, sum = 0;

for(int i = 1; i <= 20; i++)
{
x *= 1.0 / i;
sum += x;
}
System.out.println("sum=" + sum);
}
}

运行程序,输出结果是:
sum=1.7182818284590455


6.  一个数如果恰好等于它的因子之和,这个数就称为“完数”。编写应用程序,求1000之内的所有完数。

// 目的: 求1000以内的完数

public class PerfectNum
{
public static void main (String args[])
{
int i, j ,sum;

for(i = 2; i < 1000; i++)
{
sum = 0;
for(j = 1; j < i - 1; j++)
{
if(i % j == 0) sum +=j;
}
if(i == sum) System.out.print(i + " ");
}
}
}

运行程序,输出结果是:
6 28 496


7.  编写应用程序,分别使用while循环和for循环计算8+88+888+…前10项之和。

// 目的: 计算8+88+888+...前10项的和

public class SumFactorial
{
public static void main (String args[])
{
long x = 8, sum = 0;

for(int i = 0; i < 10; i++)
{
sum += x;
x = x * 10 + 8;
}
System.out.println(sum);
}
}

运行程序,输出结果是:9876543200


8.  编写应用程序,输出满足1+2+3+…+n<8888的最大正数n。

// 目的: 输出满足1+2+3+...+n<8888的最大正数n

public class SumF {
  public static void main(String args[]) {
    int sum = 0, n = 1;

    while (sum < 8888) {
      sum += n;
      n++;
    }
    n -= 2;
    System.out.println( n );
  }
}

运行程序,输出结果是:132


总结:这次看书+上机调试+做题总共花了我5个小时又20分钟,其中从20:00到 22:20为看书+上机调试例程时间,22:20到第二天01:00为做题时间,中间出去上了个吃了个宵夜,花了我10来分钟,另外上网找什么是因子时,来上到雅虎网不小心点了一个标题很吸引人的链接,然后就被谋杀了10几分钟,心痛啊。本来不打算通宵的,但是精神状态实在太好了,于是就一鼓作气,在看完第三章后,把没做的题目全部做完了。
做题过程中遇到了一些小问题,如第5题的 x += 1.0/i假如这里不是使用1.0而是使用1/i则会导致输出结果永远是1.0,因为1/i除号两边都是整数,故所得结果永远是0,但是我们要的是整个小数,所以必须得将除号两边的操作数至少一个变为浮点数,这里我是直接用1.0代替1。还有其它的方法,细心的朋友完全可以看出来的。
我还遇到另外一个很郁闷的问题就是,做第4题时,我已经写出了正确的算法了,可是当我把它写成程序时,却把 if(x % i == 0) break;里的判断条件写成了 i % 2 == 0,怎么也看不出来是哪里错了,没法,我只好启动netbeans来单步调试监控各个关键变量,还是没有看出来,只好对照课本里相同的例子,一句句的比较,才发现判断条件写错了,我的妈呀。我再看看我写程序前的分析算法,我明明写对了的,可是却打错了,这种低级的错误以后希望少犯或者不犯。
今天很有成就,希望明天可以继续。

Smile

第2题和第8题的错误已经修改了,多谢楼下朋友。


喝杯茶吃个包 edited on 2008-09-20 12:12

作者 Re:[学习总结]Java2实用教程(第三版)习题3 [Re:喝杯茶吃个包]
zjlnnnnn





发贴: 2
积分: 0
于 2008-09-20 11:53 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
错误更正(经过本人测试过的):

2.输出结果应该是
b
e
p
!

5.补充
//1.用do-while计算1+1/2!+1/3!+1/4!+….的前20项和
double sum=0.0,temp=1.0;
  int i=1;
  do
  {
    temp/=i;
    sum+=temp;
    i++;
  }
  while(i<=20);
System.out.println("用do-while计算1+1/2!+1/3!+1/4!+….的前20项和,结果为:"+sum);

7.补充
//1.使用while循环计算8+88+888+…前10项之和。
long sum=0,i=1,temp=8;
  while(i<=10)
  {
    sum+=temp;
    temp=temp*10+8;
    i++;
  }  
System.out.println("使用while循环计算8+88+888+…前10项之和,结果为:"+sum);

8.正确答案应该是n=132

class Ex8
{
  public static void main(String args[])
  {
    //求满足1+2+3+…+n<8888的最大正数n
    int sum=0,i;
    for(i=1;i<=8888;i++)
    {
      sum+=i;
      if(sum>=8888)
      {
        break;      
      }
    }
    System.out.println("满足1+2+3+…+n<8888的最大正数n="+(i-1));
    
    //测试结果
    int sum2=0,j;
    for(j=1;j<=132;j++)
    {
      sum2+=j;
    }
    System.out.println("1+2+3+...+132="+sum2);
    
    int sum3=0,k;
    for(k=1;k<=133;k++)
    {
      sum3+=k;
    }
    System.out.println("1+2+3+...+133="+sum3);
  }
}



作者 Re:[学习总结]Java2实用教程(第三版)习题3 [Re:喝杯茶吃个包]
Twelve_Oak





发贴: 1
积分: 0
于 2008-09-22 16:20 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
上面作者发布的是正确的 下面错误更正的不正确,本来就是对的 你更正什么啊


作者 Re:[学习总结]Java2实用教程(第三版)习题3 [Re:喝杯茶吃个包]
喝杯茶吃个包





发贴: 7
积分: 1
于 2008-09-23 16:04 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
to Twelve_Oak
第2题确实错了,我原来写的是对的,但是帖出来后就变成错的了,可能是我不小心打错字母了,我到现在还保留有原来正确的源码呢。
第8题就是真的是理解题目不够深入弄错的了,把求1+2+3+…+n<8888的最大正数n,错误理解成了求1+2+3+…+n<8888的最大正数。

你可能没看到,我在习题最后已经说明改正过来的了。



作者 Re:[学习总结]Java2实用教程(第三版)习题3 [Re:喝杯茶吃个包]
JiafanZhou



版主


发贴: 736
积分: 61
于 2008-09-25 18:32 user profilesend a private message to usersend email to JiafanZhousearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
喝杯茶吃个包 wrote:
to Twelve_Oak
第2题确实错了,我原来写的是对的,但是帖出来后就变成错的了,可能是我不小心打错字母了,我到现在还保留有原来正确的源码呢。
第8题就是真的是理解题目不够深入弄错的了,把求1+2+3+…+n<8888的最大正数n,错误理解成了求1+2+3+…+n<8888的最大正数。

你可能没看到,我在习题最后已经说明改正过来的了。

And in that case, we wish to see that maybe you can take some considerations to update the code. Do you agree that it will help us more than looking at some error code? Smile

Jiafan


JiafanZhou edited on 2008-10-14 20:58

When I was a kid I used to pray every night for a new bike. Then I realized that The Lord doesn't work that way, so I stole one and asked him to forgive me.
作者 为什么我算第8题,答案是133啊?高手们帮忙看一下!! [Re:喝杯茶吃个包]
night





发贴: 3
积分: 0
于 2008-10-12 01:22 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
为什么我算第8题,答案是133啊?高手们帮忙看一下!!

// comments from Jiafan
This is because the loop iterate one more time after the number of 132.


class Nvalue
{
  public static void main(String[] args){
  long sum = 0;
  for(int i = 1;;i++) {
     sum += i;
     if(sum >= 8888 && (sum-i) <= 8888) {
       System.out.println(i);
       break;
     }  
  }
}  
}


JiafanZhou edited on 2008-10-14 21:04


flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923