package leif.tests;
import java.util.Scanner;
import org.junit.Test;
public class LoopExercises {
/*
* 实现一个课程名称和课程代号的转换器:输入下表中的课程代号,输出课程的名称。用户可以循环进行输入,如果输入n就退出系统。(使用do-while循环实现)
* 课程名称和课程代号对照表
* 课程名称 课程代码
* 使用Java语言理解程序逻辑 1
* 使用HTML语言开发商业站点 2
* 使用SQL Server管理和查询数据 3
* 使用C#开发数据库应用程序 4
*/
@Test
public void test1() {
Scanner scanner = new Scanner(System.in);
String string = null;
do {
string = scanner.next();
switch (string) {
case "1":
System.out.println("使用Java语言理解程序逻辑");
break;
case "2":
System.out.println("使用HTML语言开发商业站点");
break;
case "3":
System.out.println("使用SQL Server管理和查询数据");
break;
case "4":
System.out.println("使用C#开发数据库应用程序");
break;
}
} while (!"n".equals(string));
System.out.println("退出系统");
scanner.close();
}
/*
* 本金10000元存入银行,年利率是千分之三。每过1年,将本金和利息相加作为新的本金。计算5年后,获得的本金是多少。(使用for循环实现)
*/
@Test
public void test2() {
double money = 10000;
for (int i = 0; i < 5; i++) {
money += money * 0.003;
}
System.out.println(money);
}
/*
* 求整数1~100的累加值,但要求跳过所有个位为3的数。(使用for循环实现)
*/
@Test
public void test3() {
int sum = 0;
for (int i = 1; i <= 100; i++) {
if (i % 10 != 3) {
sum += i;
}
}
System.out.println(sum);
}
/*
* 在屏幕上打印出n行的金字塔图案,如n=5,则图案如下:
* *
* ***
* *****
* *******
* *********
*/
@Test
public void test4() {
Scanner scanner = new Scanner(System.in);
int row = scanner.nextInt();
for (int i = 1; i <= row; i++) {
for (int colIndex = 1; colIndex <= row - i; colIndex++) {
System.out.print(" ");
}
for (int rowIndex = 0; rowIndex < 2 * i - 1; rowIndex++) {
System.out.print("*");
}
System.out.println();
}
scanner.close();
}
/*
* 幸运猜猜猜:游戏随机给出一个0~99(包括0和99)的数字,然后让你猜是什么数字,你可以随便猜一个数字,游戏会提示太大还是太小,从而缩小结果范围,经过几次猜测与提示后,最终推出答案。在猜测过程中,游戏会记录你最终猜对时所用的次数,并在结束时公布结果。
* 积分对照表
* 次数 结果
* 1 你太有才了!
* 2~7 这么快就猜出来了,很聪明么!
* 大于7 猜了半天才猜出来,小同志,尚需努力啊!
* 猜测次数最多20次。
* 提示:
* 1、产生0~99之间的随机数字:int number = (int)(Math.random()*100)
* 2、使用for循环结构,其中循环计数器counter会记录你猜测的次数
* 3、计算积分可以使用switch结构
*/
@Test
public void test5() {
Scanner scanner = new Scanner(System.in);
int number = (int)(Math.random() * 100);
int counter = 1;
for (; counter <= 20; counter++) {
int i = scanner.nextInt();
if (i > number) {
System.out.println("太大");
continue;
} else if (i < number) {
System.out.println("太小");
continue;
} else {
System.out.println("猜对了" + number);
break;
}
}
switch (counter) {
case 1:
System.out.println("你太有才了!");
break;
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
System.out.println("这么快就猜出来了,很聪明么!");
break;
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
case 16:
case 17:
case 18:
case 19:
case 20:
System.out.println("猜了半天才猜出来,小同志,尚需努力啊!");
break;
default:
System.out.println("正确答案:" + number);
}
scanner.close();
}
/*
* 打印9*9乘法表
* 1*1=1
* 1*2=2 2*2=4
* 1*3=3 2*3=6 3*3=9
* 1*4=4 2*4=8 3*4=12 4*4=16
* 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
* 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
* 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
* 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
* 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
*/
@Test
public void test6() {
for (int rowIndex = 1; rowIndex <= 9; rowIndex++) {
for (int colIndex = 1; colIndex <= rowIndex; colIndex++) {
int result = rowIndex * colIndex;
System.out.print(colIndex + "*" + rowIndex + "=" + result + "\t");
}
System.out.println();
}
}
/*
* 某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。
*/
@Test
public void test7() {
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
int a = i / 1000;
int b = i % 1000 / 100;
int c = i % 100 / 10;
int d = i % 10;
a = (a + 5) % 10;
b = (b + 5) % 10;
c = (c + 5) % 10;
d = (d + 5) % 10;
System.out.print(d);
System.out.print(c);
System.out.print(b);
System.out.print(a);
scanner.close();
}
}
循环练习题
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...