Java-icourse163课程总结(1-4week)

2018-05-14  本文已影响0人  Ericoool

第1周-计算

了解基本的数据输入、变量与计算。编程平台Eclipse。

Scanner in = new Scanner(System.in);    //用户输入
n = in.nextInt()                        //读取输入
int i = 0;            //定义变量,并初始化为0
final int i = 100;    //定义常量

第一周的编程作业-温度转换

写一个将华氏温度转换成摄氏温度的程序,转换的公式是:
    °F = (9/5)*°C + 32
其中C表示摄氏温度,F表示华氏温度。
程序的输入是一个整数,表示华氏温度。输出对应的摄氏温度,也是一个整数。
提示,为了把计算结果的浮点数转换成整数,需要使用下面的表达式:
    (int)x;
其中x是要转换的那个浮点数。
import java.util.Scanner;
public class Main {
    private static Scanner in;
    public static void main(String[] args) {
        in = new Scanner(System.in);
        int F;
        F = in.nextInt();
        System.out.println((int)((F-32)*5/9));
    }
}

第2周-判断

了解逻辑运算符、if-else和switch-case分支语句

//比较三个数的大小
Scanner in = new Scanner(System.in);
int a;
int b;
int c;
System.out.print("请输入三个整数:")
a = in,nenxtInt();
b = in,nenxtInt();
c = in,nenxtInt();
int max = 0;
if (a>b)
{
  if (a>c)
  {
    max = a;
  }
  else
  {
    max = c;
  }
}
else
{
  if (b>c)
  {
    max = b;
  }
  else
  {
    max = c;
  }
}
System.out.print("最大的数是"+max);
switch (控制表达式) {
case 常量:
  语句
  ...
case 常量:
  语句
  break;
default:
  语句
  ...
}

第2周的编程作业-时间换算

UTC是世界协调时,BJT是北京时间,UTC时间相当于BJT减去8。
现在,你的程序要读入一个整数,表示BJT的时和分。整数的个位和十位表示分,百位和千位表示小时。
如果小时小于10,则没有千位部分;如果小时是0,则没有百位部分;如果分小于10分,需要保留十位上的0。
如1124表示11点24分,而905表示9点5分,36表示0点36分,7表示0点7分。
有效的输入范围是0到2359,即你的程序不可能从测试服务器读到0到2359以外的输入数据。
你的程序要输出这个时间对应的UTC时间,输出的格式和输入的相同,即输出一个整数,表示UTC的时和分。
整数的个位和十位表示分,百位和千位表示小时。如果小时小于10,则没有千位部分;
如果小时是0,则没有百位部分;如果分小于10分,需要保留十位上的0。
提醒:要小心跨日的换算。
import java.util.Scanner;
public class Main 
{
    private static Scanner in;
    public static void main(String[] args)
    {
        in = new Scanner(System.in);
        int BJT;
        int UTC;
        BJT = in.nextInt();
        int decade = BJT/10%10;        //对输入的北京时间BJT除10后,在取10的余,得到十位数,即分钟的十位。
        if (decade <= 5)               //判断分钟的十位数应该不大于5,否则输入错误。
        {
            if (BJT < 2359 || BJT < 0)
            {
               UTC = BJT - 800;
               if (UTC < 0)
               {
                  UTC = UTC + 2400;
                  System.out.println(UTC);
               }
               else
               {
                 System.out.println(UTC);
               }
            }
            else
            {
                 System.out.println("format error");
            }
        }
        else
        {
            System.out.println("format error");
        }
    }
}

第2周的编程作业-信号报告

无线电台的RS制信号报告是由三两个部分组成的:
R(Readability) 信号可辨度即清晰度.
S(Strength)    信号强度即大小.
其中R位于报告第一位,共分5级,用1—5数字表示.
1---Unreadable
2---Barely readable, occasional words distinguishable
3---Readable with considerable difficulty
4---Readable with practically no difficulty
5---Perfectly readable
报告第二位是S,共分九个级别,用1—9中的一位数字表示
1---Faint signals, barely perceptible
2---Very weak signals
3---Weak signals
4---Fair signals
5---Fairly good signals
6---Good signals
7---Moderately strong signals
8---Strong signals
9---Extremely strong signals
现在,你的程序要读入一个信号报告的数字,然后输出对应的含义。如读到59,则输出:
Extremely strong signals, perfectly readable.
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int Readability;
        int Strength;
        int RS = in.nextInt();
        Readability = RS/10;
        Strength = RS - Readability*10;
        if (Readability > 5 || Readability <= 0) 
        {
            System.out.println("format error");
        }
        else {
            switch (Strength) 
            {
                case 1:System.out.print("Faint signals, barely perceptible");break;
                case 2:System.out.print("Very weak signals");break;
                case 3:System.out.print("Weak signals");break;
                case 4:System.out.print("Fair signals");break;
                case 5:System.out.print("Fairly good signals");break;
                case 6:System.out.print("Good signals");break;
                case 7:System.out.print("Moderately strong signals");break;
                case 8:System.out.print("Strong signals");break;
                case 9:System.out.print("Extremely strong signals");break;
            }
            System.out.print(", ");
            switch (Readability) 
            {
                case 1:System.out.print("unreadable.");break;
                case 2:System.out.print("barely readable, occasional words distinguishable.");break;
                case 3:System.out.print("readable with considerable difficulty.");break;
                case 4:System.out.print("readable with practically no difficulty.");break;
                case 5:System.out.print("perfectly readable.");break;
            }
        }
    }
}

第3周-循环

while循环和do-while循环。

//数用户输入的整数的位数
int number = in.nextInt();
int count = 0;
while (number > 0)
{
  number = number/10;
  count += 1;
}
System.out.println(count);

如果用do-while改写:

int number = in.nextInt();
int count = 0;
do
{
  number = number/10;
  count += 1;
} while (number > 0)        //由于判断在循环体结束后,这样count会多加1.
System.out.println(count-1);

第3周的编程作业-奇偶个数

你的程序要读入一系列正整数数据,输入-1表示输入结束,-1本身不是输入的数据。程序输出读到的数据中的奇数和偶数的个数。
输入格式:
一系列正整数,整数的范围是(0,100000)。如果输入-1则表示输入结束。
输出格式:
两个整数,第一个整数表示读入数据中的奇数的个数,第二个整数表示读入数据中的偶数的个数。两个整数之间以空格分隔。
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int number;
        int odd = 0;
        int even = 0;
        do {
            number = in.nextInt();
            if (number%2 == 0) {
                even += 1;
            }
            else {
                odd += 1; 
            }
        } while (number != -1 || number>=0 && number<=100000);
        System.out.print((odd-1) + " " + even);        //最后输入的-1,会进入循环体,odd的数值会多加一次。
    }
}

第3周的编程作业-数字特征值

对数字求特征值是常用的编码算法,奇偶特征是一种简单的特征值。
对于一个整数,从个位开始对每一位数字编号,个位是1号,十位是2号,以此类推。
这个整数在第n位上的数字记作x,如果x和n的奇偶性相同,则记下一个1,否则记下一个0。
按照整数的顺序把对应位的表示奇偶性的0和1都记录下来,就形成了一个二进制数字。
比如,对于342315,这个二进制数字就是001101。
这里的计算可以用下面的表格来表示:

数字 3 4 2 3 1 5
数位 6 5 4 3 2 1
数字奇偶
数位奇偶
奇偶一致 0 0 1 1 0 1
二进制位值 32 16 8 4 2 1

按照二进制位值将1的位的位值加起来就得到了结果13。
你的程序要读入一个非负整数,整数的范围是[0,100000],然后按照上述算法计算出表示奇偶性的那个二进制数字,输出它对应的十进制值。
提示:将整数从右向左分解,数位每次加1,而二进制值每次乘2。

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int number = in.nextInt();
        int x=0;                                //整数第n位上的数字
        int n=0;                                //整数的数位
        int count=0;
        if (number>=0 && number<=1000000) {
            while (number != 0) 
            {
                x = number%10;
                n += 1;
                if ((x+n)%2==0) {                                   //x和n之和是偶数时
                    count = (int) (count + Math.pow(2,n-1));        //2的(n-1)次方,2的0次方=1
                }
                number = number/10; 
            }
            System.out.println(count);
        }
        else {
            System.out.println("format error");
        }
    }
}

第4周-循环控制

for (int i=0; i<5; i++) {
  System.out.println(i);
}
int n = in.nextInt();
int factor = 1;
for (int i=1; i<=n; i++) {
  factor = factor * i;
}
Sytem.out.println(factor);
int i = 1;
while (i<=n) {
  factor = factor * i;
  i += 1;
}
//输出的100以内的素数
for (int n=2; n<100; n++) {
  int isPrime = 1;
  for (int i=2; i<n; i++) {
    if (n%i == 0) {
      isPrime = 0;
      break;
    }
  }
  if (isPrime == 1) {
    System.out.print(n+" ");
  }
}
Outer:
for (...) {
  for (...) {
    for (...) {
      break Outer;
    }
  }
}

第4周的编程作业-素数和

我们认为2是第一个素数,3是第二个素数,5是第三个素数,依次类推。
现在,给定两个整数n和m,0<n<=m<=200,你的程序要计算第n个素数到第m个素数之间所有的素数的和,包括第n个素数和第m个素数。
注意,是第n个素数到第m个素数之间的所有的素数,并不是n和m之间的所有的素数。
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        int primeSum = 0;
        int count = 0;
        if (n>0 && n<=m && m<=200) {
            while (count<=m) {
                for (int x=2; count<=m ; x++) {
                    int isPrime = 1;
                    for (int i=2; i<x; i++) {
                        if (x%i == 0) {
                            isPrime = 0;
                            break;
                        }
                    }
                    if (isPrime == 1) {
                        count += 1;
                        if (count>=n && count<=m) {
                            primeSum += x;
                        }
                    }
                }
            }
            System.out.println(primeSum);
        }
        else {
            System.out.println("format error");
        }
    }
}

第4周的编程作业-念整数

你的程序要读入一个整数,范围是[-100000,100000]。然后,用汉语拼音将这个整数的每一位输出出来。
如输入1234,则输出:
yi er san si
注意,每个字的拼音之间有一个空格,但是最后的字后面没有空格。当遇到负数时,在输出的开头加上“fu”,如-2341输出为:
fu er san si yi
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int num = in.nextInt();
        int count = 0;
        int x = num;
        if (num>-100000 && num<100000) {
            do {
                x = x/10;
                count += 1;
            } while(x != 0);

            if (num==0) {
                System.out.println("ling");
            }
            else {
                if (num<0) {
                    System.out.print("fu ");
                    num = -num;
                }
                int mode = (int)Math.pow(10,count-1);
                int fristNum = num/mode;
                switch (fristNum)
                {
                    case 0: System.out.print("ling");break;
                    case 1: System.out.print("yi");break;
                    case 2: System.out.print("er");break;
                    case 3: System.out.print("san");break;
                    case 4: System.out.print("si");break;
                    case 5: System.out.print("wu");break;
                    case 6: System.out.print("liu");break;
                    case 7: System.out.print("qi");break;
                    case 8: System.out.print("ba");break;
                    case 9: System.out.print("jiu");break;
                }
                int newMode = mode/10;
                int newNum = num-fristNum*mode;
                for (int i=0; i<(count-1); i++)
                {
                    int number = newNum/newMode;
                    newNum %= newMode;
                    newMode /= 10;
                    switch (number) 
                    {
                        case 0: System.out.print(" ling");break;
                        case 1: System.out.print(" yi");break;
                        case 2: System.out.print(" er");break;
                        case 3: System.out.print(" san");break;
                        case 4: System.out.print(" si");break;
                        case 5: System.out.print(" wu");break;
                        case 6: System.out.print(" liu");break;
                        case 7: System.out.print(" qi");break;
                        case 8: System.out.print(" ba");break;
                        case 9: System.out.print(" jiu");break;
                    }
                }
            }
        }
        else {
            System.out.println("format error!");
        }
    }
}

前面这4周学习的是编程基本知识,在其它语言中通用,java语法和swift比较相近,这个课程很系统,受益匪浅。

上一篇下一篇

猜你喜欢

热点阅读