题目详情请自行查看中国大学mook对应课程,下面附上源码:
import java.util.*;//引入库函数
public class 自动售票机 {
public static void main(String[] args) {
do {
Menu();// 程序功能目录
Scanner in = new Scanner(System.in);
int Type = in.nextInt();
if (Type == -1) {
System.out.println("退出成功!!!");
break;
}
switch (Type) {
case 1: {
Buy();
break;
}
case 2: {
Judge();
break;
}
case 3: {
Arge();
break;
}
case 4: {
Guess();
break;
}
case 5: {
guess();
break;
}
case 6: {
digit();
break;
}
case 7: {
IsPrime();
break;
}
case 8: {
OutPutPrime();
break;
}
case 9: {
Sum();
break;
}
case 10: {
gcd();
break;
}
case 11: {
Avag();
break;
}
case 12: {
TouPiao();
break;
}
case 13: {
game();
break;
}
case 14: {
Dxs();
break;
}
case 15: {
Word();
break;
}
case 16: {
devide();
break;
}
case 17: {
wanshu();
break;
}
case 18: {
Tem();
break;
}
case 19: {
Time();
break;
}
case 20: {
Img();
break;
}
case 21: {
numberOf();
break;
}
case 22: {
Cheracter();
break;
}
default: {
System.out.println("输入错误!");
break;
}
}
} while (true);
}
// 程序功能目录函数
static void Menu() {
System.out.println("*********************************");
System.out.println("******* 程序功能目录 ***********");
System.out.println(" 1.自动售票机 ");
System.out.println(" 2.计算成绩 ");
System.out.println(" 3.算平均数 ");
System.out.println(" 4.我来输入你来猜 ");
System.out.println(" 5.你来输入我来猜 ");
System.out.println(" 6.整数分解倒序输出 ");
System.out.println(" 7.判断素数 ");
System.out.println(" 8.N以内的素数 ");
System.out.println(" 9.从1加到1/n的和 ");
System.out.println(" 10.最大公约数 ");
System.out.println(" 11.输出比平均数大的数 ");
System.out.println(" 12.投票统计 ");
System.out.println(" 13.读数字 ");
System.out.println(" 14.多项式求和 ");
System.out.println(" 15.单词长度 ");
System.out.println(" 16.分解质因数 ");
System.out.println(" 17.完数 ");
System.out.println(" 18.温度转换 ");
System.out.println(" 19.时间换算 ");
System.out.println(" 20.信号报告 ");
System.out.println(" 21.奇偶个数 ");
System.out.println(" 22.数字特征值 ");
System.out.println("*********************************");
System.out.println("请输入你想实现的功能:(输入-1退出程序!)");
}
// 自动售票机函数
static int Buy() {
Scanner in = new Scanner(System.in);
int balance = 0;
while (true) {
System.out.println("请投币:(输入-1退出)");
int amount = in.nextInt();
balance += amount;
if (amount >= 10) {
System.out.println("*********************");
System.out.println(" Java城际铁路专线 ");
System.out.println(" 无指定座位票 ");
System.out.println(" 票价:10元 ");
System.out.println("*********************");
System.out.println("找零:" + (balance - 10) + "元!");
balance = 0;
} else if (amount == -1) {
return 0;
} else {
System.out.println("余额不足!");
}
}
}
// 判断成绩函数
static void Judge() {
Scanner in = new Scanner(System.in);
System.out.println("请输入你的成绩:");
int mark = in.nextInt();
switch (mark / 10) {
case 1:
case 2:
case 3:
case 4:
case 5:
System.out.println("不及格!");
break;
case 6:
System.out.println("恭喜及格!");
break;
case 7:
System.out.println("七十分,还有上升空间!");
break;
case 8:
System.out.println("八十分,很优秀了你!");
break;
case 9:
System.out.println("九十分!离满分只差一步!");
break;
case 10:
System.out.println("恭喜满分!无敌!");
break;
default:
System.out.println("百分制哦!");
break;
}
}
// 求平均数函数
static void Arge() {
Scanner in = new Scanner(System.in);
System.out.println("请输入你要输入的数字:(输入-1表示结束)");
int number = 0;
int sum = 0;
int count = 0;
do {
number = in.nextInt();
if (number != -1) {
sum += number;
count++;
}
} while (number != -1);
System.out.println("这" + count + "个数的平均数是:" + (double) (sum / count));
}
// 我来输入你来猜
static void Guess() {
Scanner in = new Scanner(System.in);
int number = (int) Math.random() * 100 + 1; // random函数随机产生一个【0,1)的数---->【0,100)----->【1,100)
int a = 0, count = 0;
System.out.println("来吧!小老弟,猜个数:");
do {
a = in.nextInt();
count++;
if (a < number) {
System.out.println("猜小了,兄弟!");
} else if (a > number) {
System.out.println("猜大了,兄弟!");
} else {
if (count == 1)
System.out.println("唉呀妈呀,一下就被你猜中了!兄弟你可以去买彩票了,别忘了奖金分我点儿呦");
else
System.out.println("老弟,你猜了" + count + "次!终于猜对了!恭喜恭喜!");
break;
}
} while (true);
}
// 你来输入我来猜
static void guess() {
Scanner in = new Scanner(System.in);
System.out.println("来吧,兄弟,我现在闭上眼睛,你输入一个数,(0到100),我保证在8次以内就猜出来!");
int number = in.nextInt();
int sum = 0, sum1 = 100, a, t = 0;
while (true) {
a = (sum + sum1) / 2;
if (a == number) {
System.out.println("你输入的数是" + a + "!,哈哈,厉害不厉害?猜对了吧!!!");
break;
}
System.out.println("兄弟你刚刚输入的是:" + a + "?");
System.out.println("输入1表示猜小了,输入2表示猜大了");
t = in.nextInt();
if (t == 1)
sum = a;
else
sum1 = a;
}
}
// 整数分解倒序输出
static void digit() {
Scanner in = new Scanner(System.in);
System.out.println("请输入一个整数:");
int number = in.nextInt();
System.out.print("倒序输出的结果:");
do {
if (number > 9) {
int a = number % 10;
if (a != 0)
System.out.print(a);
number /= 10;
} else {
System.out.print(number);
break;
}
} while (number != 0);
System.out.println();
}
static void IsPrime() {
Scanner in = new Scanner(System.in);
System.out.println("请输入一个数:");
int number = in.nextInt();
int flag = 1;
for (int i = 2; i < number; i++) {
if (number == 2) {
break;
}
if (number % i == 0) {
flag = 0;
break;
}
}
if (flag == 1) {
System.out.println(number + "是素数");
} else {
System.out.println(number + "不是素数");
}
}
static void OutPutPrime() {
Scanner in = new Scanner(System.in);
System.out.println("请输入一个数:");
int number = in.nextInt();
System.out.print(number + "以内的素数有: ");
int flag = 1;
for (int a = 2; a < number; a++) {
for (int i = 2; i < a; i++) {
if (a == 2) {
break;
}
if (a % i == 0) {
flag = 0;
break;
}
}
if (flag == 1) {
System.out.print(a + " ");
}
flag = 1;
}
System.out.println();
}
static void Sum() {
Scanner in = new Scanner(System.in);
System.out.println("请输入一个数:");
int number = in.nextInt();
double sum = 0.0;
System.out.println("从1加到1/" + number + "的和是:");
for (int i = 1; i <= number; i++)
sum += 1.0 / i;
System.out.printf("%.2f", sum);
}
static void gcd() {
Scanner in = new Scanner(System.in);
System.out.println("请输入2个数:");
int number1 = in.nextInt(), number2 = in.nextInt();
System.out.print(number1 + "和" + number2 + "的最大公约数是:");
int c = 0;
while (number2 != 0) {
c = number1 % number2;
number1 = number2;
number2 = c;
}
System.out.print(number1);
System.out.println();
}
static void Avag() {
Scanner in = new Scanner(System.in);
System.out.println("请输入一些数字:");
int[] a = new int[100];
int count = 0;
int x, i = 0;
int sum = 0;
double avg = 0.0;
x = in.nextInt();
while (x != -1) {
sum += x;
a[i] = x;
i++;
count++;
x = in.nextInt();
}
if (count != 0) {
avg = sum / count;
System.out.print("它们的平均数是:");
System.out.printf("%.2f", avg);
System.out.print("其中比平均数大的有: ");
for (int j = 0; j < a.length; j++) {
if (a[j] > avg)
System.out.print(a[j] + " ");
}
System.out.println();
}
}
static void TouPiao() {
Scanner in = new Scanner(System.in);
System.out.println("现有0-9号中国好声音选手,请为您喜欢的选手投票吧!输入若干次0到9之间的数x:(输入-1表示退出)");
int[] a = new int[10];
int t = in.nextInt();
int Max, j = 0;
while (t != -1) {
if (t >= 0 && t <= 9)
a[t]++;
t = in.nextInt();
}
Max = a[0];
j = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] > a[0]) {
Max = a[i];
j = i;
}
System.out.println("恭喜" + i + "号选手获得:" + a[i] + "票!");
}
System.out.println("恭喜" + j + "号选手获得:" + Max + "票!赢得本季度的总冠军!!!");
}
static void game() {
Scanner in = new Scanner(System.in);
String[] b = { "ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu" };
System.out.println("请输入一个数字:");
int number = in.nextInt();
int[] num = new int[100];
int t = Math.abs(number);
int cnt = 0, cnt2 = 0;
if (number > 0) {
for (int i = 0; t != 0; i++) {
t /= 10;
cnt++;
}
cnt2 = cnt;
if (number >= 0) {
do {
if (number > 9) {
num[cnt] = number % 10;
number /= 10;
cnt--;
} else {
num[cnt] = number;
break;
}
} while (number != 0);
for (int k = 1; k <= cnt2; k++) {
for (int j = 0; j < 10; j++) {
if (num[k] == j && k != cnt2) {
System.out.print(b[j] + " ");
break;
} else if (num[k] == j && k == cnt2) {
System.out.print(b[j]);
break;
}
}
}
}
} else if (number < 0) {
System.out.print("fu ");
number = Math.abs(number);
if (number > 0) {
for (int i = 0; t != 0; i++) {
t /= 10;
cnt++;
}
cnt2 = cnt;
if (number >= 0) {
do {
if (number > 9) {
num[cnt] = number % 10;
number /= 10;
cnt--;
} else {
num[cnt] = number;
break;
}
} while (number != 0);
for (int k = 1; k <= cnt2; k++) {
for (int j = 0; j < 10; j++) {
if (num[k] == j && k != cnt2) {
System.out.print(b[j] + " ");
break;
} else if (num[k] == j && k == cnt2) {
System.out.print(b[j]);
break;
}
}
}
}
}
} else {
System.out.println("ling");
}
System.out.println();
}
static void Dxs() {
Scanner in = new Scanner(System.in);
int[] Poly = new int[101];
int Pow, Coef;
boolean bCoef = false;// 判断是否为第一次输出
System.out.println("请输入两个多项式:");
System.out.println("输入格式:\r\n" + "\r\n" + "\r\n" + "总共要输入两个多项式,每个多项式的输入格式如下:\r\n" + "\r\n"
+ "每行输入两个数字,第一个表示幂次,第二个表示该幂次的系数,所有的系数都是整数。第一行一定是最高幂,最后一行一定是0次幂。\r\n" + "\r\n" + "");
for (int i = 0; i < 2; i++) {
do {
Pow = in.nextInt();
Coef = in.nextInt();
Poly[Pow] += Coef;
} while (Pow != 0);
}
//输出
for (int i = Poly.length - 1; i >= 0; i--) {
if (Poly[i] != 0) {
if (bCoef && Poly[i] > 0) {
System.out.print("+");
}
if (Poly[i] != 1) {// 系数不为1时,即输出系数
System.out.print(Poly[i]);
if (i > 0) {
System.out.print("x");
if (i != 1) {// 输出幂次方
System.out.print(i);
}
}
} else {// 系数为1时
System.out.print("x");
if (i != 1) {// 输出幂次方
System.out.print(i);
}
}
bCoef = true;
}
}
//特殊情况,输出为0
if (!bCoef) {
System.out.print(0);
}
in.close();
}
static void Word() {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
System.out.println("请输入一句话:");
String a = in.nextLine();
char[] b = a.toCharArray();
int[] c = new int[10000];
int j = 0;
for (int k = 0; k < b.length; k++) {
if (b[k] != 32 && b[k] != 46) {
c[j]++;
} else if (b[k] != 46) {
j++;
}
}
for (int k = 0; k <= j; k++) {
if (k != j && c[k] != 0)
System.out.print(c[k] + " ");
else if (c[k] != 0)
System.out.println(c[k]);
}
}
static void devide() {
Scanner in = new Scanner(System.in);
System.out.println("请输入一个数:");
int number = in.nextInt();
System.out.print(number + "=");
int flag = 1;
int j = number;
for (int i = 2; i < number; i++) {
if (number == 2) {
break;
}
if (number % i == 0) {
System.out.print(i + "x");
number /= i;
i = 1;
}
}
System.out.print(number);
System.out.println();
}
static void wanshu() {
Scanner in = new Scanner(System.in);
System.out.println("请输入两个数字:");
int beginnumber = in.nextInt();
int endnumber = in.nextInt();
int sum = 0, count = 0;
int[] a = new int[10000];
for (int number = beginnumber; number <= endnumber; number++) {
for (int i = 1; i < number; i++) {
if (number % i == 0) {
sum += i;
}
}
if (sum == number) {
count++;
a[count] = number;
}
sum = 0;
}
System.out.print("这两个数字之间的完数有:");
for (int j = 1; j <= count; j++) {
if (j != count)
System.out.print(a[j] + " ");
else
System.out.print(a[j]);
}
System.out.println();
}
static void Tem() {
// TODO 自动生成的方法存根
Scanner in = new Scanner(System.in);
System.out.println("请输入华氏温度:");
int F = in.nextInt();
double C;
C = (F - 32) * (5.0 / 9);
System.out.println("对应的摄氏温度为:");
System.out.print((int) C);
}
static void Time() {
System.out.println("时间换算");
System.out.println("输入格式:\r\n" + "\r\n" + "\r\n"
+ "一个整数,表示BJT的时和分。整数的个位和十位表示分,百位和千位表示小时。如果小时小于10,则没有千位部分;如果小时是0,则没有百位部分;如果小时不是0而且分小于10分,需要保留十位上的0。\r\n"
+ "\r\n" + "\r\n" + "\r\n" + "\r\n" + "输出格式:\r\n" + "\r\n" + "\r\n"
+ "一个整数,表示UTC的时和分。整数的个位和十位表示分,百位和千位表示小时。如果小时小于10,则没有千位部分;如果小时是0,则没有百位部分;如果小时不是0而且分小于10分,需要保留十位上的0。\r\n"
+ "\r\n" + "\r\n" + "\r\n" + "\r\n" + "输入样例:\r\n" + "\r\n" + "\r\n" + "933\r\n" + "\r\n" + "\r\n"
+ "\r\n" + "\r\n" + "输出样例:\r\n" + "\r\n" + "\r\n" + "133\r\n" + "\r\n" + "");
System.out.print("现在请你输入一个数:");
Scanner in = new Scanner(System.in);
int BJT = in.nextInt();
System.out.print("换算后的时间是:");
if (BJT >= 800) {
System.out.println(BJT - 800);
} else {
System.out.println(BJT - 800 + 2400);
}
System.out.println();
}
static void Img() {
System.out.println("信号报告:");
System.out.println("无线电台的RS制信号报告是由三两个部分组成的:\r\n" + "\r\n" + "R(Readability) 信号可辨度即清晰度.\r\n" + "\r\n"
+ "S(Strength) 信号强度即大小.\r\n" + "\r\n" + "其中R位于报告第一位,共分5级,用1—5数字表示.\r\n" + "\r\n"
+ "1---Unreadable\r\n" + "\r\n" + "2---Barely readable, occasional words distinguishable\r\n" + "\r\n"
+ "3---Readable with considerable difficulty\r\n" + "\r\n"
+ "4---Readable with practically no difficulty\r\n" + "\r\n" + "5---Perfectly readable\r\n" + "\r\n"
+ "报告第二位是S,共分九个级别,用1—9中的一位数字表示\r\n" + "\r\n" + "1---Faint signals, barely perceptible\r\n" + "\r\n"
+ "2---Very weak signals\r\n" + "\r\n" + "3---Weak signals\r\n" + "\r\n" + "4---Fair signals\r\n"
+ "\r\n" + "5---Fairly good signals\r\n" + "\r\n" + "6---Good signals\r\n" + "\r\n"
+ "7---Moderately strong signals\r\n" + "\r\n" + "8---Strong signals\r\n" + "\r\n"
+ "9---Extremely strong signals\r\n" + "\r\n" + "现在,你的程序要读入一个信号报告的数字,然后输出对应的含义。如读到59,则输出:\r\n" + "\r\n"
+ "Extremely strong signals, perfectly readable.\r\n" + "\r\n" + "\r\n" + "\r\n" + "\r\n" + "输入格式:\r\n"
+ "\r\n" + "\r\n" + "一个整数,信号报告。整数的十位部分表示可辨度,个位部分表示强度。输入的整数范围是[11,59]内有效的数字,这个范围外的数字不可能出现在测试数据中。\r\n"
+ "\r\n" + "\r\n" + "\r\n" + "\r\n" + "输出格式:\r\n" + "\r\n" + "\r\n"
+ "一句话,表示这个信号报告的意义。按照题目中的文字,先输出表示强度的文字,跟上逗号和空格,然后是表示可辨度的文字,跟上句号。注意可辨度的句子的第一个字母是小写的。注意这里的标点符号都是英文的。\r\n"
+ "\r\n" + "\r\n" + "\r\n" + "\r\n" + "输入样例:\r\n" + "\r\n" + "\r\n" + "33\r\n" + "\r\n" + "\r\n"
+ "\r\n" + "\r\n" + "输出样例:\r\n" + "\r\n" + "\r\n"
+ "Weak signals, readable with considerable difficulty.\r\n" + "\r\n" + "");
System.out.println("现在请你输入:");
Scanner in = new Scanner(System.in);
String[] R = { "unreadable", "barely readable, occasional words distinguishable",
"readable with considerable difficulty", "readable with practically no difficulty",
"perfectly readable" };
String[] S = { "Faint signals, barely perceptible", "Very weak signals", "Weak signals", "Fair signals",
"Fairly good signals", "Good signals", "Moderately strong signals", "Strong signals",
"Extremely strong signals" };
int a = in.nextInt();
System.out.print("输出为:");
System.out.print(S[a % 10 - 1] + ", " + R[(a / 10) - 1] + ".");
System.out.println();
}
static void numberOf() {
System.out.println("题目内容:\r\n" + "\r\n" + "你的程序要读入一系列正整数数据,输入-1表示输入结束,-1本身不是输入的数据。程序输出读到的数据中的奇数和偶数的个数。\r\n"
+ "\r\n" + "\r\n" + "\r\n" + "\r\n" + "输入格式:\r\n" + "\r\n" + "\r\n"
+ "一系列正整数,整数的范围是(0,100000)。如果输入-1则表示输入结束。\r\n" + "\r\n" + "\r\n" + "\r\n" + "\r\n" + "输出格式:\r\n"
+ "\r\n" + "\r\n" + "两个整数,第一个整数表示读入数据中的奇数的个数,第二个整数表示读入数据中的偶数的个数。两个整数之间以空格分隔。\r\n" + "\r\n" + "\r\n"
+ "\r\n" + "\r\n" + "输入样例:\r\n" + "\r\n" + "\r\n" + "9 3 4 2 5 7 -1 \r\n" + "\r\n" + "\r\n" + "\r\n"
+ "\r\n" + "输出样例:\r\n" + "\r\n" + "\r\n" + "4 2\r\n" + "\r\n" + "");
System.out.println("现在请输入:");
Scanner in = new Scanner(System.in);
int[] a = new int[100];
int i, n = 0;
int numberofa = 0, numberofb = 0;
i = in.nextInt();
while (i != -1) {
a[n] = i;
n++;
i = in.nextInt();
}
i = n - 1;
n = 0;
while (i > -1) {
if (a[n] % 2 == 1)
numberofa++;
else
numberofb++;
i--;
n++;
}
System.out.println(numberofa + " " + numberofb);
System.out.println();
}
static void Cheracter() {
System.out.println("题目内容:\r\n" + "\r\n" + "\r\n"
+ "对数字求特征值是常用的编码算法,奇偶特征是一种简单的特征值。对于一个整数,从个位开始对每一位数字编号,个位是1号,十位是2号,以此类推。这个整数在第n位上的数字记作x,如果x和n的奇偶性相同,则记下一个1,否则记下一个0。按照整数的顺序把对应位的表示奇偶性的0和1都记录下来,就形成了一个二进制数字。比如,对于342315,这个二进制数字就是001101。\r\n"
+ "按照二进制位值将1的位的位值加起来就得到了结果13。\r\n" + "\r\n" + "\r\n" + "\r\n" + "\r\n"
+ "你的程序要读入一个非负整数,整数的范围是[0,100000],然后按照上述算法计算出表示奇偶性的那个二进制数字,输出它对应的十进制值。\r\n" + "\r\n" + "\r\n" + "\r\n"
+ "\r\n" + "提示:将整数从右向左分解,数位每次加1,而二进制值每次乘2。\r\n" + "\r\n" + "\r\n" + "\r\n" + "输入格式:\r\n" + "\r\n"
+ "\r\n" + "一个非负整数,整数的范围是[0,1000000]。\r\n" + "\r\n" + "\r\n" + "\r\n" + "\r\n" + "输出格式:\r\n" + "\r\n"
+ "\r\n" + "一个整数,表示计算结果。\r\n" + "\r\n" + "\r\n" + "\r\n" + "\r\n" + "输入样例:\r\n" + "\r\n" + "\r\n"
+ "342315\r\n" + "\r\n" + "\r\n" + "\r\n" + "\r\n" + "输出样例:\r\n" + "\r\n" + "\r\n" + "13\r\n" + "\r\n"
+ "");
System.out.print("请输入:");
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int a, b = 0, c = 1;
int i = 0;
while (n > 0) {
a = n % 10;
if (a % 2 == c % 2)
b = (int) (1 * Math.pow(2, i) + b);
else
b = (int) (0 * Math.pow(10, i) + b);
n = n / 10;
i++;
c++;
}
System.out.println(b);
System.out.println();
}
}