1.根据键盘录入的数值1,2,3,…7输出对应的星期一,星期二,星期三…星期日。(用switch语句实现)
代码:
import java.util.Scanner;
public class exercise01 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String s;
switch (n){
case 1:
s = "星期一";
break;
case 2:
s = "星期二";
break;
case 3:
s = "星期三";
break;
case 4:
s = "星期四";
break;
case 5:
s = "星期五";
break;
case 6:
s = "星期六";
break;
case 7:
s = "星期日";
break;
default:
s = "请输入一个1到7之间的整数!";
break;
}
System.out.println(s);
}
}
运行结果:
2.判断一个5位数是否是回文数(比如12321,个位等于万位,十位等于千位)
代码:
import java.util.Scanner;
public class exercise02 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] array = new int[5];
for(int i=0;i<5;i++){
int m = n;
array[i] = m % 10;
m /= 10;
}
if(array[0] == array[4] && array[1] == array[3]){
System.out.println(n + "是一个回文数");
}
else{
System.out.println(n + "不是一个回文数");
}
}
}
运行结果:
3.利用switch语句,实现对学生分数评级的功能。
程序的输入是一个int类型的变量score,代表学生的分数取值范围[0,100]
当分数范围为 0=< score <60 输出 不及格
当分数范围为 60=< score <70 输出 及格
当分数范围为 70=< score <80 输出 中
当分数范围为 80=< score <90 输出 良
当分数范围为 90=< score <=100 输出 优
提示:可合理利用case穿越
代码:
import java.util.Scanner;
public class exercise03 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的成绩([0,100]):");
int score = sc.nextInt();
score /= 10;
String s;
switch (score){
case 10:
case 9:
s = "优";
break;
case 8:
s = "良";
break;
case 7:
s = "中";
break;
case 6:
s = "及格";
break;
default:
s = "不及格";
break;
}
System.out.println("你的成绩是:" + s);
}
}
运行结果:
4.在给定的字符数(只包含ascii码表中包含的128个字符)组中找到,第一个只出现一次的字符(假设所给字符数组中一定存在只出现一次的字符,第一个是指从左到右的字符出现的顺序)。
代码:
import java.util.Scanner;
public class addtion01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String s = sc.nextLine();
char[] str = s.toCharArray();//转化为字符串数组
int[] count = new int[128];//计数数组,统计字符(ascii码表中包含的128个字符)出现的次数
for(int i=0;i<128;i++){ //初始化为0
count[i] = 0;
}
for(int i=0;i<str.length;i++){//遍历字符数组,统计字符出现的次数
count[str[i]]++;
}
for(int i = 0;i <str.length;i++){//遍历字符数组,找到第一个出现次数为1的字符
if(count[str[i]] == 1) {
System.out.println("第一个只出现一次的字符是" + str[i]);
break;
}
}
}
}
运行结果:
5.在一个整形数组里,除了两个数字之外,其他数字都出现了2次。请写程序找到这两个只出现了一次的数字。
代码:
public class addtion02 {
public static void main(String[] args) {
int array[] = {1, 5, 6, 5, 1, 2};
int tag[] = {1, 1, 1, 1, 1, 1}; //tag数组为标记数组,初始值为1
//若两元素相等,则把它们的tag值标记为0。
for(int i = 0;i < array.length-1;i++){
for(int j = i + 1 ;j < array.length;j++){
if(array[i] == array[j]){
tag[i] = 0;
tag[j] = 0;
}
}
}
//tag值为1的元素即为只出现一次的元素
for(int i = 0;i < tag.length;i++){
if(tag[i] == 1){
System.out.println(array[i]);
}
}
}
}
运行结果:
6.定义方法完成,打印 nxn乘法表(n的取值范围是[1-9])
代码:
import java.util.Scanner;
public class Printnxn {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入你想得到的乘法表大小(取值范围是[1-9]):");
int n = sc.nextInt();
multiply(n);
}
public static void multiply(int n){
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
System.out.print(i + "x" + j + "=" + i * j + " ");
}
System.out.println();
}
}
}
运行结果:
7.小芳的妈妈每天给她2.5元钱,她都会存起来,但是, 每当这一天是存钱的第5天或者5的倍数的话,她都会花去6元钱, 请问,至少经过多少天,小芳可以存到100元
代码:
public class SaveMoney {
public static void main(String[] args){
double money = 0;
int days = 0;
while(money<100){
days++;
money += 2.5;
if(days % 5 == 0)
money -= 6;
}
System.out.println("至少经过" + days +"天,小芳可以存到100元");
}
}
运行结果:
8.正整数n若是它平方数的尾部,则称n为同构数,比如5是5 * 5=25右边的数,25是25 * 25=625右边的数 求1-99范围内的所有同构数
代码:
//求1-99的同构数
public class IsomorphismNumber {
public static void main(String[] args){
for(int i=1;i<=99;i++){
if(i<10){
if(i == (i*i%10))
System.out.print(i + " ");
}
else {
if(i == (i*i%100))
System.out.print(i + " ");
}
}
}
}
运行结果:
#个人学习记录,如发现有错误之处,欢迎与我交流