文章目录
前言
提示:该文章为自己的正确答案记录,本人也是初学者,答案并非最优方案。
翁恺老师的《零基础学Java语言》作业正确答案记录
一、第1周 计算
1.温度转换
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner temp = new Scanner (System.in);
double input = temp.nextInt();
//套公式即可
System.out.println((int)((input-32)/1.8));
}
}
二、第2周 判断
1.时间换算
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int BJT, UTC;
Scanner in = new Scanner(System.in);
BJT = in.nextInt();
//范围为0到2359
if (0 <= BJT && BJT <= 2359) {
//其中百位和个位代表分钟,不闹超过60
//对1000取余,再对100取余,即可得到两位数
if (BJT % 1000 % 100 <= 59) {
//如果小时大于8点则可以直接相减
if (BJT >= 800) {
UTC = BJT - 800;
//如果小于8点,减完要倒退到前一天
} else {
UTC = 2400 - (800 - BJT);
}
System.out.println(UTC);
} else {
System.out.println("错误的输入时间");
}
} else {
System.out.println("错误的输入时间");
}
}
}
2.信号报告
import java.util.Scanner;
public class Main {
public static void main (String[] args){
Scanner sc = new Scanner(System.in);
int rs = sc.nextInt();
//对10和100取余的到个位数和十位数,用switch语句匹配输出即可
int s=rs%10;
int r=rs/10%10;
if (r>=1 && r<=5 && s>=1 && s<=9){
switch (s){
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;
}
switch (r){
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;
}
}
else{
System.out.println("no!");
}
}
}
二、第3周 循环
1.奇偶个数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//定义两个变量表示奇数和偶数,while循环遍历即可,若偶数则o++,否则j++
int j = 0, o = 0;
while (true) {
int a = sc.nextInt();
if ( a==-1) {
break;
} else if (a%2 == 0) {
o++;
} else {
j++;
}
}
System.out.println(j + " " + o);
}
}
2.数字特征值
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int s = sc.nextInt();
int n = 0, m = 0, t = 1,r=0;
while (s != 0) {
m = s % 10; //得到尾数
s = s / 10; //去掉当前尾数,方便下一轮运算
n++;//数位加一
if ((m%2==0&&n%2==0)||(m%2!=0&&n%2!=0)) {//判断是否满足条件
r += t;//累加计算
}
t=2*t;
}
System.out.println(r);
}
}
二、第4周 循环控制
1.素数和
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
boolean t = true;
int wh = 2;
int n1 = 0;//代表素数个数
int wh1 = 0;//用来计算素数累加
/*
思路:
使用while无限循环,令wh从2开始自增寻找找素数,
找到一个素数则n1++,n1=n相等时,即为第n个素数,
令找到的素数相加(wh1+=wh)
当n1=m时,即为第m个素数
结束程序
*/
while (true) {
//判断是否素数
if (wh != 2 && wh % 2 == 0) {
t = false;
} else {
for (int i = 3; i < wh; i=i+2) {
if (wh % i == 0) {
t = false;
break;
}
}
}
//如果是素数
if (t) {
n1++;
if (n1 >= n && n1 <= m) {
wh1 += wh;
}
if (n1 > m) {
System.out.println(wh1);
break;
}
} else {
t = true;
}
wh++;
}
}
}
2.念整数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int w = 0,wei = 0,i = 100000;
if (n==0) {System.out.println("ling");}
if(n<0){System.out.print("fu ");n*=-1; }
for (int bb = n; bb != 0; wei++) {bb /= 10;}
for (int bb = 6-wei;bb!=0;bb--){ i = i / 10; }
while (i != 0) {
w = n / i % 10;
switch (w) {
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;
default:
System.out.print("jiu");
break;
}
if(i!=1){System.out.print(" ");}
i /= 10;
}
}
}
二、第5周 数组
1.多项式加法
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] $1 = new int[101];
int a = 0;
for (int i = 0; i < 2; ) {
a = sc.nextInt();
if (a < 0 || a > 100) {
break;
}
$1[a] += sc.nextInt();
if (a == 0) {
i++;
}
}
int sum = 0;
if (a >= 0 && a <= 100) {
for (int i = $1.length - 1, n = 0; i >= 0; i--) {
sum += $1[i];
if ($1[i] == 0) {
continue;
}
n++;
if ($1[i] > 0 && n > 1) {
System.out.print("+");
}
if ($1[i] != 1) {
System.out.print($1[i]);
}
if (i != 0) {
System.out.print("x");
}
if (i > 1) {
System.out.print(i);
}
}
if (sum == 0) {
System.out.print("0");
}
} else {
System.out.println("请输入[0,100]区间的整数");
}
}
}
二、第6周 使用对象
1.单词长度
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
lo:
while (true) {
String a = sc.next();
if (a.equals(".")){break lo;}
for (int i = 0; i < a.length(); i++) {
char b = a.charAt(i);
if (b == '.') {
System.out.println(a.length()-1);
break lo;
}
}
System.out.print(a.length()+" ");
}
}
}
2.GPS数据处理
NMEA-0183协议定义的语句非常多,
但是常用的或者说兼容性最广的语句只有
$GPGGA、$GPGSA、$GPGSV、$GPRMC、$GPVTG、$GPGLL等。
其中$GPRMC语句的格式如下:
字段0:$GPRMC,语句ID,表明该语句为Recommended Minimum Specific GPS/TRANSIT Data(RMC)推荐最小定位信息
这里,“*”为校验和识别符,
其后面的两位数为校验和,
代表了“$”和“*”之间所有字符的
异或值的十六进制值(不包括这两个字符)。
上面这条例句的校验和是十六进制的50,也就是十进制的80。
提示:
^运算符的作用是异或。
将$和*之间所有的字符做^运算
(第一个字符和第二个字符异或,结果再和第三个字符异或,依此类推)
之后的值对65536取余后的结果,
应该和*后面的两个十六进制数字的值相等,
否则的话说明这条语句在传输中发生了错误。
注意这个十六进制值中是会出现A-F的大写字母的。
另外,如果你需要的话,
可以用Integer.parseInt(s)
从String变量s中得到其所表达的整数数字;
而Integer.parseInt(s, 16)
从String变量s中得到其所表达的十六进制数字
你的程序一定会读到一条有效的$GPRMC语句。
输入格式:
输出格式:
其中,hh是两位数的小时,不足两位时前面补0;mm是两位数的分钟,不足两位时前面补0;ss是两位数的秒,不足两位时前面补0。
输入样例:
package com.weiminming.moocwork;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String result = null;
while (true) {
String a = sc.nextLine();
//END则退出
if (a.equals("END")) {
break;
//排除$GPRMC以外的语句
} else if (a.indexOf("$GPRMC") == -1) {
continue;
}
//输入示例:$GPRMC,024813.640,A,3158.4608,N,11848.3737,E,10.05,324.27,150706,,,A*50
//从右往左边找到'*',获取它的索引位置赋值给cc,如果没找到则跳过当前语句
if (a.lastIndexOf('*') == -1) {
continue;
}
int cc = a.lastIndexOf('*') + 1;
/*
注意'*'后面的就是十六进制的数字,它的范围是最大是FFFF,
不一定是两位数,所以不能直接获取倒数两位的字符串,要通过'*'号找
*/
//获取从cc开始到结束的子串,指向对象b
String b = a.substring(cc);
//将子串的十六进制值转为十进制;
int zichuan = Integer.parseInt(b, 16);
//定义sum为字符串第二个字符,注意括号内的1是索引,第一个字符的索引为0
char sum = a.charAt(1);
//计算第一个字符到*号位置前一个字符的 异或 计算累加
for (int i = 1; i < cc - 2; i++) {
sum = (char) (sum ^ a.charAt(i + 1));
}
//判断是否满足条件
if (sum % 65536 == zichuan && a.indexOf('A') != -1) {
//小时数此时是字符串,先转数字
int h = Integer.parseInt(a.substring(7, 9));
//BJT和UTC时间换算
h = (h + 8) % 24;
//如果h是个位数则要加个零
if (h < 9) {
result = ("0" + h + ":" + a.substring(9, 11) + ":" + a.substring(11, 13));
} else {
result = (h + ":" + a.substring(9, 11) + ":" + a.substring(11, 13));
}
}
}
//输出
System.out.println(result);
}
}
二、第7周 函数
1.分解质因数
import java.util.Scanner;
public class Main {
public static boolean isPrime(int n) {
boolean isPrime = true;
//除了2的偶数都不是质数,不参与循环
if (n != 2 && n % 2 == 0) {
isPrime = false;
} else {
for (int i = 3; i < n; i += 2) {
if (n % i == 0) {
isPrime = false;
break;
}
}
}
return isPrime;//ture or false
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int account = sc.nextInt();
int result = 0;
System.out.print(account + "=");
if (isPrime(account)) {
System.out.print(account);
} else {
lo: for (int i = 2; i < account; i++) {
if (isPrime(i)) {
while (true) {
result = account % i;
if (result == 0) {
System.out.print(i);
account /= i;
if (account != 1) {
System.out.print("x");
}
}
if (result != 0 && !isPrime(account)) {
break;
}
if (result != 0 && isPrime(account)) {
if (account == 1) {
break;
}
System.out.print(account);
break lo;
}
}
}
}
}
}
}
2.完数
import java.util.Scanner;
public class Main{
public static boolean isPrime(int n) {
boolean isPrime = true;
//除了2的偶数都不是质数,不参与循环
if (n != 2 && n % 2 == 0) {
isPrime = false;
} else {
for (int i = 3; i < n; i += 2) {
if (n % i == 0) {
isPrime = false;
break;
}
}
}
return isPrime;//ture or false
}
public static boolean isPrefect(int n) {
int sum = 0;
boolean a = false;
if (!isPrime(n)) {
for (int i = 1; i < n; i++) {
if (n % i == 0) {
sum += i;
}
}
if (sum == n) {
a = true;
}
}
return a;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[] per = new int[60];//截至2018年,相关研究者已经找到51个完全数
int count = 0;
for (int i = n; i <= m; i++) {
if (isPrime(i)) {
continue;
} else if (isPrefect(i)) {
per[count] = i;
count++;
}
}
for (int i = 0; i < count; i++) {
if (per[0] != 0) {
System.out.print(per[i]);
} else {
System.out.print("*");
break;
}
if(i<count-1){
System.out.print(" ");}
}
}
}
总结
学习java重在逻辑思考,解决问题的答案很多。
非科班,纯爱好。
本人也是初学者,欢迎交流,一起学习。