1、
解:有错误:Variable expected,改成变量进行运算即可。
public class Test01 {
public static void main(String[] args) {
int a = 6;
a = a--;
System.out.println(a);
}
}
2. 代码分析
分析以下代码的运⾏结果【重点复习原码 反码 补码】int a = 6 -- ;System . out . println ( a );System . out . println ( 5 & 9 );System . out . println ( 5 | 9 );System . out . println ( 5 ^9 );System . out . println ( ~ - 5 );System . out . println ( 5 << 2 );System . out . println ( - 5 << 2 );System . out . println ( - 5 >> 2 );System . out . println ( - 5 >>> 2 );System . out . println ( 5 >> 2 );System . out . println ( 5 >> 34 );System . out . println ( 97 == 'a' );System . out . println ( 5.0 == 5 );System . out . println ( 4 > 5 ^ 'c' > 'a' );System . out . println (( int )( char )( byte ) - 1 );
解:
public class Test02 { public static void main(String[] args) { System.out.println(5&9); /** * 0000 .... 0101 * 0000 .... 1001 * 0000 .... 0001 * ==> 1 */ System.out.println(5|9); System.out.println(5^9); /** * 0000 .... 0101 * 0000 .... 1001 * 0000 .... 1100 * ==> 12 */ System.out.println(~-5); /** * 1000 .... 0101 * 1111 11.. 1011 * 0000 00.. 0100 * ==> 4 */ System.out.println(5<<2); /** * 0000 .... 0101 * 00 00 .... 010100 * ==> 20 */ System.out.println(-5<<2); /** * 1000 .... 0101 * 1111 11.. 1011 * 11 1111 .. 101100 * 1000 .. 010100 * ==> -20 */ System.out.println(-5>>2); /** * 1000 .... 0101 * 1111 11.. 1011 * 1111 11.. 10 11 * 111111 11.. 10 * 100000 00.. 10 * ==> -2 */ System.out.println(-5>>>2); /** * 1000 .... 0101 * 1111 11.. 1011 * 1111 11.. 10 11 * 001111 11.. 10 * ==> 1073741822 */ System.out.println(5>>2); /** * 0000 .... 0101 * 0000 .... 01 01 * 000000 .... 01 * ==> 1 */ System.out.println(5>>34); System.out.println(97=='a'); System.out.println(5.0==5); System.out.println(4>5 ^ 'c'>'a'); /** * false ^ true * true */ System.out.println((byte)-1); System.out.println((char)(byte)-1); System.out.println((int)(char)(byte)-1); System.out.println((int)(char)(byte)-1); /** * 1000 .... 0001 * 1111 1111 byte -1补码 * 1111 1111 1111 1111 byte转char * 0000 0000 0000 0000 1111 1111 1111 1111 char无符号,前面补16个0 * 65535 */ } }
3、
指定位置设置为
1
问题描述
:
将参数
v
的第
n
位置设为
1
,然后返回该结果值。
public class Test3_SetOne {
// ⽅法声明如下:
public static int setBit(int v,int n){
// 提示:使⽤ | 及 <<
// 补全代码
return (v | 1<<(n-1));
}
public static void main(String[] args) {
int r = setBit(10,3);
/**
* 0000 .... 1010 (10)
* 00 0000 .... 0100 (|) 运算 使用1向左移n-1位,再做或运算
* ->0000 .... 1110 (14)
*/
System.out.println("r: " + r);
}
}
4、指定位置设置为0
问题描述
:
将参数
v
的第
n
位置为
0
,然后返回该结果值
public class Test4_SetZero {
// ⽅法声明如下:
public static int setZero(int v,int n){
// 提示:使⽤ ~ 及 <<
// 补全代码
v = (v & ~(1<<(n-1)));
return v;
}
public static void main(String[] args) {
int r = setZero(10,2);
/**
* 0000 .... 1010 (10)
* 0000 .... 0010
* 1111 11.. 1101
* 0000 00.. 1000 (8)
*/
System.out.println("r: " + r);
}
}
流程控制
6.
九九乘法表
问题描述
:
编写⼀个
Java
程序,实现如下效果的九九乘法表
public class Test6_MultiplicationTable {
public static void main(String[] args) {
// 请在此处补全代码
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + i * j + "\t");
}
System.out.println();
}
}
}
7.
素数问题
问题描述
:
请编写⼀个
Java
程序,判断
101-200
之间有多少个素数,
并输出所有素数
只能被
1
和它本身整除的⾃然数为素数
如
101
、
5
等
public class Test7_PrimeNumber {
public static void main(String[] args) {
// 请在此处补全代码
int count = 0;
for (int i = 101; i <= 200; i++) {
if (isPrime(i)) {
count++;
System.out.printf("%d\t", i);
if (count % 10 == 0){
System.out.println();
}
}
}
System.out.printf("\n101到200之间共有素数【%d】个: ", count);
}
public static boolean isPrime(int number) {
if (number <= 1) return false;
if (number == 2) return true;
if (number % 2 == 0) return false;
for (int i = 3; i * i <= number; i += 2) {
if (number % i == 0) {
return false;
}
}
return true;
}
}
8.
⽉份计算
问题描述:
请编写⼀个
Java
程序,实现从键盘录⼊⽉份,判断该⽉
份属于哪个季节
【要求】
如果⽉份不再
1-12
之间,则报错:输⼊⽉份有误
匹配⽉份,输出对应的季节
12-2
冬
3-5
春
6-8
夏
9-11
秋
使⽤
java.util.Scanner
类完成
import java.util.Scanner;
/**
* 请编写⼀个Java程序,实现从键盘录⼊⽉份,判断该⽉ 份属于哪个季节 【要求】 如果⽉份不再1-12之间,则报错:输⼊⽉份有误
*/
public class Test8_Season {
public static void main(String[] args) {
// 请继续向下补全代码
Scanner sc = new Scanner(System.in);
System.out.print("请输⼊⼀个月份:");
int month = sc.nextInt();
if (month < 1 || month > 12) {
System.out.println("月份输入有误,请输入一个介于1到12之间的整数。");
} else {
String season;
switch (month) {
case 12:
case 1:
case 2:
season = "冬季";
break;
case 3:
case 4:
case 5:
season = "春季";
break;
case 6:
case 7:
case 8:
season = "夏季";
break;
case 9:
case 10:
case 11:
season = "秋季";
break;
default:
season = "季节未找到";
break;
}
System.out.println("月份【" + month + "】属于【" + season + "】季节。");
}
sc.close();
}
}
9.
天数计算
问题描述
:
请编写⼀个
Java
程序,实现从键盘中输⼊年份 ⽉份 ⽇
期,输出这⼀天是该年的第⼏天
【要求】
输⼊的年份、⽉份、⽇期均为数字,不⾜
10
的⽤
0x
表
示,如:
1
⽉
1
⽇为
01 01
输⼊的年份、⽉份、⽇期之间⽤空格隔开,如:
2023
04 27
【示例】
输⼊:
2023 04 27
输出:
117
import java.util.Scanner;
public class Test9_DayOfYear {
public static void main(String[] args) {
// 请在此处补全代码,学会使⽤Scanner类
Scanner sc = new Scanner(System.in);
System.out.print("请输入年份 月份 日份:");
int year = sc.nextInt();
int month = sc.nextInt();
int day = sc.nextInt();
// 计算该天是该年的第几天
int days = 0;
for (int i = 1; i < month; i++) {//循环计算month月前每月有多少天
int daysInMonth = getDaysInMonth(i, isLeapYear(year));
System.out.println("daysInMonth==>" + daysInMonth);
days += daysInMonth;
}
days += day;//加上本月的天数
// 输出结果
System.out.printf("%d年%s月%s日是该年的第%d天\n", year, month, day, days);
}
// 判断是否是闰年
private static boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
// 获取指定月份的天数
private static int getDaysInMonth(int month, boolean isLeapYear) {
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
return isLeapYear ? 29 : 28;
default:
throw new IllegalArgumentException("无效的月份: " + month);
}
}
}
10.
完全数问题
问题描述
:
请编写⼀个
Java
程序,实现输⼊⼀个数,判断其是否为
完全数
若⼀个⾃然数,恰好与除去它本身以外的⼀切因数的
和相等,这种数叫做完全数。
如:
6
是完全数,因为,
6 = 1 + 2 + 3;
28
是完全数,因为,
28 = 1 + 2 + 4 + 7 + 14;
【示例
1
】
输⼊:
6
输出:
6
是完全数
【示例
2
】
输⼊:
11
输出:
11
不是完全数
import java.util.Scanner;
public class Test10_PerfectNumber {
public static void main(String[] args) {
// 补全代码
Scanner sc = new Scanner(System.in);
System.out.print("请输入一个数:");
int num = sc.nextInt();
// 计算num的因子和【除本身外】
int sum = 0;
int cnt = 0;
System.out.printf("%d除本身外的因子有\n", num);
for (int i = 1; i < num; i++) {
if (num % i == 0) {
++cnt;
sum += i;
System.out.printf("%d\t", i);
if (cnt % 5 == 0 ){
System.out.println();
}
}
}
System.out.printf("%d除本身外的因子和为==>%d\n", num, sum);
// 输出结果
if (sum == num) {
System.out.printf("%d是完全数\n", num);
} else {
System.out.printf("%d不是完全数\n", num);
}
}
}
11.
图形输出
问题描述:
请编写⼀个
Java
程序,实现下⾯图形的输出
【提示】
先输出上半部分【先输出空格
再输出
*
】
再输出下半部分
import java.util.Scanner;
public class Test11_PrintDiamond {
public static void main(String[] args) {
// 补全代码
Scanner sc = new Scanner(System.in);
System.out.print("请输入行数n:");
int n = sc.nextInt();
int m = n/2 + 1;
// 打印上半部分
for (int i = 1; i <= m; i++) {
// 打印空格
for (int j = 1; j <= m - i; j++) {
System.out.print(" ");
}
// 打印星号
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
// 打印下半部分
for (int i = m - 1; i >= 1; i--) {
// 打印空格
for (int j = 1; j <= m - i; j++) {
System.out.print(" ");
}
// 打印星号
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
12.
分解质因数
问题描述
:
请编写⼀个
Java
程序,实现输⼊⼀个
15
以内的正整数,
对该正整数进⾏分解质因数,输出该整数与质因数的等
式。
【示例
1
】
输⼊:
3
输出:
3 = 3
【示例
2
】
输⼊:
20
输出:输⼊数字不符合要求
【示例
3
】
输⼊:
8
输出:
8 = 2 * 2 * 2
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Test12_PrimeFactorization {
public static void main(String[] args) {
// 补全代码
Scanner sc = new Scanner(System.in);
List<Integer> numList = new ArrayList<>();
System.out.print("请输入一个15以内的正整数:");
int n = sc.nextInt();
if (n <= 1 || n > 15) {
System.out.println("输入数字不符合要求");
return;
}
System.out.print(n + " = ");
for (int i = 2; i <= n; ) {
if (n % i == 0) {
numList.add(i);
n /= i;
} else {
i++;
}
}
for (int i = 0; i < numList.size(); i++) {
System.out.printf("%d", numList.get(i));
if (i != numList.size()-1){
System.out.printf(" * ");
}
}
}
}