鱼C论坛

 找回密码
 立即注册
楼主: 百日维新

[技术交流] #鱼C五周年嘉年华# 《JAVA程序设计&改错》# 第一章

[复制链接]
发表于 2015-1-28 12:41:55 | 显示全部楼层
wwwxinyu1990 发表于 2015-1-27 12:32
第八题求指教,知道原因,却想不到解决办法~

谢谢!我去看看{:1_1:}

点评

壮士,第二章更新了!  发表于 2015-1-28 13:00
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-1-29 09:50:11 | 显示全部楼层
支持一下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-1-30 16:17:49 | 显示全部楼层
  1. /*
  2. No01:程序设计,写一个程序验证一个整数是否是奇数
  3. */

  4. class Odd {
  5.         public static void main(String[] args) {
  6.                 int n = 1;
  7.                 if (n % 2 == 1)
  8.                         System.out.print(n+"是奇数");
  9.                 else
  10.                         System.out.print(n+"是偶数");
  11.         }
  12. }
复制代码

  1. /*
  2. No02:程序改错,计算1 - 20 的累加
  3. */

  4. public class Test {
  5.         public static void main(String args[]){
  6.           int t = 0;
  7.      
  8.           for(int i=0 ;i <= 20;i++){
  9.               t = t + i;
  10.           }
  11.      
  12.           System.out.println(t);
  13.      }

  14. }
复制代码

  1. /*
  2. No03:程序设计,输出倒立三角形 (5分)
  3. */

  4. class Triangle {
  5.         public static void main(String[] args) {
  6.                 for (int i = 0; i < 5; i++) {
  7.                         for (int j = 0; j < i; j++) {
  8.                                 System.out.print(' ');
  9.                         }
  10.                         for (int j = 5; j > i; j--) {
  11.                                 System.out.print("* ");
  12.                         }
  13.                         System.out.println();
  14.                 }
  15.         }
  16. }
复制代码

  1. /*
  2. No04:程序改错,输出minutes = 60
  3. */

  4. public class Clock {
  5.      public static void main(String[] args) {
  6.          int minutes = 0;
  7.          
  8.          for(int ms =0; ms < 60*60*1000; ms ++){
  9.              if(ms  %  (60*1000) == 0){
  10.                  minutes ++;
  11.              }
  12.          }
  13.          System.out.println(" minutes = "+minutes);

  14.      }
  15. }
复制代码

  1. /*
  2. No05:程序设计,实现字符串反转,例如输入“i love fishc" , "应输出chsif evol i"
  3. */

  4. class Reversal {
  5.         public static void main(String[] args) {
  6.                 String n = "i love fishc";
  7.                 char[] ch = n.toCharArray();
  8.                 for (int i = ch.length - 1; i >= 0; i--) {
  9.                         System.out.print(ch[i]);
  10.                 }
  11.                
  12.         }
  13. }
复制代码

  1. /*
  2. No06:程序改错
  3. */

  4. public class DoubleSub {

  5.      public static void main(String[] args) {
  6.              System.out.println((float)(2.0)  - (float)(1.1));

  7.      }
  8. }
复制代码

  1. /*
  2. No09:程序设计,x = 2014 ,y = 2015,用异或交换x , y的值
  3. */

  4. class Exchange {
  5.         public static void main(String[] args) {
  6.                 int x = 2014;
  7.                 int y = 2015;
  8.                 System.out.println("x = "+x+",y = "+y);
  9.                 x = x^y;
  10.                 y = x^y;
  11.                 x = x^y;
  12.                 System.out.println("x = "+x+",y = "+y);
  13.         }
  14. }
复制代码

  1. /*
  2. No10:程序设计,用最有效率的方法算出2乘以8等於几
  3. */

  4. class Calculation {
  5.         public static void main(String[] args) {
  6.                 System.out.println(2*8);
  7.                 System.out.println(2<<3);        //高效率
  8.         }
  9. }
复制代码



刚开始学还有很多不懂,7、8题没什么思路写来着,不知道这样写可不可以,不要见笑哈

点评

7,8题去百度查查就晓得了嘛,度娘很强大!  发表于 2015-2-18 00:39
+5 + 5 + 5 + 5 + 5 + 10 + 0 + 0 +15 + 15  发表于 2015-2-18 00:38
点那个,方便统计分数而已!答题就算参加了!  发表于 2015-1-30 17:14
在去做做第二章,这个待会我看看那些对了,不对你在修改!  发表于 2015-1-30 16:20
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-1-31 21:02:49 | 显示全部楼层
第八题是需要考虑他的Unicode编码是吗?
  1. /*
  2. No08:程序设计,打印字符串长度,"a\u0022.length()+\u0022b"
  3. */

  4. class PrintTest {
  5.         public static void main(String[] args) {
  6.                 String n = "a\u0022.length()+\u0022b";
  7.                 System.out.println(n.length());
  8.         }
  9. }
复制代码

点评

不要翻译里面的内容,全部都当作字符串  发表于 2015-1-31 21:12
+0 , 不对,不要想当然,去Eclipse下面跑下  发表于 2015-1-31 21:11
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-2-8 21:33:26 | 显示全部楼层
No01
public class _1_1
{

        public static void main(String[] args)
        {
          int test1 = 0;
          System.out.println(test1 + "是否是奇数:"+isOdd(test1));
          int test2 = 1;
          System.out.println(test2 + "是否是奇数:"+isOdd(test2));
          int test3 = 2;
          System.out.println(test3 + "是否是奇数:"+isOdd(test3));
          int test4 = -1;
          System.out.println(test4 + "是否是奇数:"+isOdd(test4));
          int test5 = -2;
          System.out.println(test5 + "是否是奇数:"+isOdd(test5));
        }

        public static boolean isOdd(int number)
        {
                //不能被2整除是奇数
         return (number & 1) != 0;
        }

}

点评

第一章全部通过,请再接再厉!  发表于 2015-2-18 00:27
+ 5  发表于 2015-2-18 00:27
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-2-8 21:36:58 | 显示全部楼层
No02:程序改错,计算1 - 20 的累加
package 小甲鱼活动;

public class _1_2
{

        public static void main(String[] args)
        {
                int t = 0;//改正一

                for (int i = 1; i <= 20; i++)//改正二
                {
                        t = t + i;
                }

                System.out.println(t);
        }

}

点评

+ 5  发表于 2015-2-18 00:23
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-2-8 21:37:39 | 显示全部楼层
No03:程序设计,输出倒立三角形
package 小甲鱼活动;

public class _1_3
{
    //输出倒立三角形
        public static void main(String[] args)
        {
                int bian = 11;
                for (int i = 0; i <= bian/2; i++)
                {
                        for(int k=0;k<i;k++){
                                System.out.print(" ");
                        }
                        for (int j = 0; j < bian - i*2; j++)
                        {
                                System.out.print("*");
                        }
                        System.out.println();
                }
        }

}

点评

+ 5  发表于 2015-2-18 00:23
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-2-8 21:38:10 | 显示全部楼层
No04:程序改错,输出minutes = 60
package 小甲鱼活动;

public class _1_4
{
        public static void main(String[] args)
        {
                int minutes = 0;

                for (int ms = 0; ms < 60 * 60 * 1000; ms++)
                {
                        if (ms % (60 * 1000) == 0)//加括号
                        {
                                minutes++;
                        }
                }
                System.out.println(" minutes = " + minutes);
        }

}

点评

+5  发表于 2015-2-18 00:22
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-2-8 21:38:50 | 显示全部楼层
No05:程序设计,实现字符串反转,例如输入“i love fishc" , "应输出chsif evol i"
package 小甲鱼活动;

import java.util.Scanner;

public class _1_5
{

        public static void main(String[] args)
        {
                Scanner scanner = new Scanner(System.in);
                String input = scanner.nextLine();
                StringBuffer inputString = new StringBuffer(input);
                System.out.print(inputString.reverse());
        }

}

点评

+5  发表于 2015-2-18 00:22
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-2-8 21:39:25 | 显示全部楼层
No06:程序改错  两数相减,输出0.9
package 小甲鱼活动;

import java.math.BigDecimal;

public class _1_6
{

        public static void main(String[] args)
        {
                System.out.println(new BigDecimal("2.0")
                                .subtract(new BigDecimal("1.1")));
        }

}

点评

+10  发表于 2015-2-18 00:21
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-2-8 21:40:09 | 显示全部楼层
No07:程序设计,有一个字符串,其中包含中文字符、英文字符和数字字符,请统计和打印出各个字符的个数
package 小甲鱼活动;

import java.util.HashMap;
import java.util.Map;

public class _1_7
{

        public static void main(String[] args)
        {
                String str = "甲鱼是23不是4¥¥鱼@#3";
                //System.out.println(str.length());
                Map<Character, Integer> result = new HashMap<>();
                int length = str.length();
                for (int i = 0; i < length; i++)
                {
                        char ch = str.charAt(i);
                        if (result.containsKey(ch))
                        {
                                result.put(ch, result.get(ch) + 1);
                        }
                        else
                                result.put(ch, 1);
                }
                for (char c : result.keySet())
                {
                        System.out.println(c + " 有 " + result.get(c) + "个");
                }
        }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-2-8 21:40:43 | 显示全部楼层
No07:程序设计,有一个字符串,其中包含中文字符、英文字符和数字字符,请统计和打印出各个字符的个数
package 小甲鱼活动;

import java.util.HashMap;
import java.util.Map;

public class _1_7
{

        public static void main(String[] args)
        {
                String str = "甲鱼是23不是4¥¥鱼@#3";
                //System.out.println(str.length());
                Map<Character, Integer> result = new HashMap<>();
                int length = str.length();
                for (int i = 0; i < length; i++)
                {
                        char ch = str.charAt(i);
                        if (result.containsKey(ch))
                        {
                                result.put(ch, result.get(ch) + 1);
                        }
                        else
                                result.put(ch, 1);
                }
                for (char c : result.keySet())
                {
                        System.out.println(c + " 有 " + result.get(c) + "个");
                }
        }
}

点评

+10  发表于 2015-2-18 00:20
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-2-8 21:41:16 | 显示全部楼层
No08:程序设计,打印字符串长度,"a\u0022.length()+\u0022b"
package 小甲鱼活动;

public class _1_8
{
        public static void main(String[] args)
        {
                String str = "a\\u0022.length() + \\u0022b";
                System.out.println(str.length());
        }
}

点评

+10  发表于 2015-2-18 00:18
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-2-8 21:42:08 | 显示全部楼层
No09:程序设计,x = 2014 ,y = 2015,用异或交换x , y的值
package 小甲鱼活动;

public class _1_9
{

        public static void main(String[] args)
        {
                int x = 2014, y = 2015;
                x = x ^ y;
                y = y ^ x;
                x = y ^ x;
                System.out.println("x=" + x);
                System.out.println("y=" + y);
        }

}

点评

+15分  发表于 2015-2-18 00:17
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-2-8 21:42:45 | 显示全部楼层
No10:程序设计,用最有效率的方法算出2乘以8等於几
package 小甲鱼活动;

public class _1_10
{

        public static void main(String[] args)
        {
                //口算最快
                System.out.println("2*8 = " + 16);
                //口算不行的话,就这样吧
                System.out.println("2*8 = " + (2 << 3));
        }

}

点评

+15分  发表于 2015-2-18 00:16
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-2-8 21:45:14 | 显示全部楼层
N07我不小心重复提交了一次。两次是一样的。知道了吗楼主?

点评

当然可以  发表于 2015-2-8 21:55
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-2-17 17:59:33 | 显示全部楼层
本帖最后由 月之吟 于 2015-2-17 18:18 编辑

No 01
  1. import java.util.Scanner;

  2. public class No01 {
  3.         public static void main(String[] args) {
  4.                 int i;
  5.                 Scanner sc = new Scanner(System.in);
  6.                 System.out.println("请输入需要判断的整数:");
  7.                 i = sc.nextInt();
  8.                 if (i % 2 != 0) {
  9.                         System.out.println("该整数是奇数");
  10.                 } else {
  11.                         System.out.println("该整数不是奇数");
  12.                 }
  13.                 sc.close();
  14.         }
  15. }
复制代码

No 02
for循环改为for(int i = 1; i <= 20; i++),把 t+i 强制转型为 short。把 t 定义成 int 型也行吧。
  1. public class No02 {
  2.         public static void main(String args[]) {
  3.                 short t = 0;
  4.                 for (int i = 1; i <= 20; i++) {
  5.                         t = (short) (t + i);
  6.                 }
  7.                 System.out.println(t);
  8.         }
  9. }
复制代码


No 03
  1. public class No03 {
  2.         public static void main(String[] args) {
  3.                 int num = 5;
  4.                 for (int i = 0; i < num; i++) {
  5.                         for (int m = 0; m < i; m++) {
  6.                                 System.out.print(" ");
  7.                         }
  8.                         for (int n = 0; n < (num - i) * 2 - 1; n++) {
  9.                                 System.out.print("*");
  10.                         }
  11.                         System.out.println();
  12.                 }
  13.         }
  14. }
复制代码

No 04
将 if 中的60*1000用括号括起来。
  1. public class No04 {
  2.         public static void main(String[] args) {
  3.                 int minutes = 0;

  4.                 for (int ms = 0; ms < 60 * 60 * 1000; ms++) {
  5.                         if (ms % (60 * 1000) == 0) {
  6.                                 minutes++;
  7.                         }
  8.                 }
  9.                 System.out.println(" minutes = " + minutes);

  10.         }
  11. }
复制代码


No 05
  1. import java.util.Scanner;

  2. public class No05_1 {
  3.         public static void main(String[] args) {
  4.                 Scanner sc = new Scanner(System.in);
  5.                 System.out.println("请输入需要反转的字符串:");
  6.                 String s1 = sc.nextLine();
  7.                 String s2 = new String();
  8.                 for (int i = s1.length() - 1; i >= 0; i--) {
  9.                         s2 += s1.charAt(i);
  10.                 }
  11.                 System.out.println("反转后的字符串为:\n" + s2);
  12.                 sc.close();
  13.         }
  14. }
复制代码

上面不知道一个字符一个字符加到s2上是不是不好……就又写了下面的……
  1. import java.util.Scanner;

  2. public class No05_2 {
  3.         public static void main(String[] args) {
  4.                 Scanner sc = new Scanner(System.in);
  5.                 System.out.println("请输入需要反转的字符串:");
  6.                 String s1 = sc.nextLine();
  7.                 char[] ch = new char[s1.length()];
  8.                 for (int i = 0; i < s1.length(); i++) {
  9.                         ch[i] = s1.charAt(s1.length() - i - 1);
  10.                 }
  11.                 String s2 = new String();
  12.                 s2 = String.valueOf(ch);
  13.                 System.out.println("反转后的字符串为:\n" + s2);
  14.                 sc.close();
  15.         }
  16. }
复制代码

不过如果直接用StringBuilder的reverse方法应该不算吧……

No 06
2.0和1.1后面都加f。只会这么改了- -
  1. public class No06 {
  2.         public static void main(String[] args) {
  3.                 System.out.println(2.0f - 1.1f);
  4.         }
  5. }
复制代码

No 07
  1. import java.util.Scanner;

  2. public class No07 {
  3.         public static void main(String[] args) {
  4.                 int word = 0, num = 0, cn = 0;
  5.                 Scanner sc = new Scanner(System.in);
  6.                 System.out.println("请输入需要统计的字符串:");
  7.                 String s = sc.nextLine();
  8.                 for (int i = 0; i < s.length(); i++) {
  9.                         if (s.charAt(i) >= 48 && s.charAt(i) <= 57) {
  10.                                 num++;
  11.                         }
  12.                         if (s.charAt(i) >= 65 && s.charAt(i) <= 90) {
  13.                                 word++;
  14.                         }
  15.                         if (s.charAt(i) >= 97 && s.charAt(i) <= 122) {
  16.                                 word++;
  17.                         }
  18.                         if (s.charAt(i) >= 19968 && s.charAt(i) <= 40869) {
  19.                                 cn++;
  20.                         }
  21.                 }
  22.                 System.out.println("有" + num + "个数字");
  23.                 System.out.println("有" + word + "个英文");
  24.                 System.out.println("有" + cn + "个汉字");
  25.                 sc.close();
  26.         }
  27. }
复制代码

No 08
  1. import java.util.Scanner;

  2. public class No08 {

  3.         public static void main(String[] args) {
  4.                 Scanner sc = new Scanner(System.in);
  5.                 System.out.println("请输入字符串:");
  6.                 String s = sc.nextLine();
  7.                 System.out.println(s + "字符串的长度为" + s.length());
  8.                 sc.close();
  9.                 System.out.println("a\\u0022.length()+\\u0022b的字符串长度为");
  10.                 System.out.println("a\\u0022.length()+\\u0022b".length());
  11.         }

  12. }
复制代码

上面这个的"a\u0022.length()+\u0022b"的长度也只会这么着了- -

No 09
  1. public class No09 {
  2.         public static void main(String[] args) {
  3.                 int x = 2014, y = 2015;
  4.                 System.out.println("交换前的值为:x=" + x + " y=" + y);
  5.                 x = x ^ y;
  6.                 y = x ^ y;
  7.                 x = x ^ y;
  8.                 System.out.println("交换后的值为:x=" + x + " y=" + y);
  9.         }
  10. }
复制代码

No 10
  1. public class No10 {
  2.         public static void main(String[] args) {
  3.                 System.out.println("2*8=" + (2 << 3));
  4.         }
  5. }
复制代码

有错或者不好也不要笑话我哦~










点评

+85分 , 全部通过,请继续努力!  发表于 2015-2-17 23:30
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-28 22:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表