您的位置:首页 > 编程语言 > Java开发

从零开始学习Java——基础知识之循环语句练习题(第四天)

2017-01-16 23:51 656 查看

一、循环语句(while语句)

1、while语句

初始条件;

while(条件表达式){

   功能代码;

}

注意:一般用于不确定终止的情况下

演示代码:

/**
@author sfbaobao
测试while循环语句
*/
public class TestWhereProcess{
public static void main(String[] args){
int sum = 0;
int i =1;
while(i <= 100){
sum += i;
i++;
}
System.out.println(sum);
}
}// out>>>5050
// 一般用于不确定终止次数的情况


2、do while语句

do{

  功能代码;

}while(条件表达式);

注意:

do while跟while语句的区别是do - - - while会先执行功能代码后一次后再进行判断;

do while语句结尾后有分号;

演示代码:

/**
@author sfbaobao
测试do while语句
*/
public class TestDoWhileProcess{
public static void main(String[] args){
int i = 3;
do{
System.out.println("哈哈哈哈哈啊哈哈~");// 会先执行一次,再进行条件判断
}while(i<3);
}
}// out>>>哈哈哈哈哈啊哈哈~


3、关于输出语句的小细节

int i = 20;
int j = 30;
System.out.println(" " + i + j);// out>>>  2030
System.out.println(i + j + " ");// out>>>50


第一个输出是先i转换为字符串后与” “做字符串拼接,然后j再转换为字符串跟前面的” 20”字符串拼接,最终拼接为” 2030”

第二个输出是先i 与 j做数值相加计算,然后再转换为字符串与” “拼接,最终得到”50 ”

注意:

只要表达式中有字符串,最后结果为字符串

2、表达式是从左往右计算的

二、循环语句程序案例

1、案例1

/**
@author sfbaobao
1.输出图型
*
**
***
****
*****
******
*/
public class Test01{
public static void main(String[] args){
for(int i=1; i<=6; i++){
for(int j=1; j<=i; j++){
System.out.print("*");
}
System.out.println();
}
}
}


2、案例2

/**
@author sfbaobao
2  输出图型
*******
******
*****
****
***
**
*
*/
public class Test02{
public static void main(String[] args){
for(int i=6; i>=1; i--){
for(int j=1; j<=i; j++){
System.out.print("*");
}
System.out.println();
}
}
}


3、案例3

/**
@author sfbaobao
3、把12题的两个图型合成一个。
*
**
***
****
*****
******
*******
******
*****
****
***
**
*
*/
public class Test03{
public static void main(String[] args){
for(int i=-6; i<=6; i++){
// 调用求绝对值的方法
for(int j=6; j>=(Math.abs(i)); j--){
System.out.print("*");
}
System.out.println();
}
}
}


4、打印菱形

/**
@author sfbaobao
打出如下星星
*
***
*****
*******
*****
***
*
*/
public class Test07{
public static void main(String[] args){
for(int i=-3; i<=3; i++){
for(int k=0; k<Math.abs(i); k++){
System.out.print("  ");
}
for(int j=0; j<7-2*Math.abs(i); j++){
System.out.print("*");
}
System.out.println();
}
}
}


5、打印空心菱形

/**
@author
8、打出以下图形
*
* *
*   *
*     *
*   *
* *
*
*/
public  class Test08{
public static void main(String[] args){
for(int i=-3; i<=3; i++){
for(int k=0; k<Math.abs(
c49c
i); k++){
System.out.print("  ");
}
for(int j=0; j<7-2*Math.abs(i); j++){
if(j == 0 || j==7-2*Math.abs(i)-1){// 每行首尾打出*
System.out.print("*");
}else{
System.out.print("  ");
}
}
System.out.println();
}
}
}


打图案,一般先分析图形的结构,外层循环控制行数,内层循环控制每行的星数,找出他们之间有对应的关系。先打印出星数,再分析空格数,最终得到相应的图案。

6、求素数

/**
@author sfbaobao
9.求300-400之间的素数
*/
public class Test09{
public static void main(String[] args){
boolean flag = true;// 引用标志
for(int i=301; i<400; i++){
for (int j=2; j<=Math.sqrt(i)+1; j++){
if(i % j == 0){
flag = false;// 如果条件成立则不是素数,改变标志
break;
}
}
if(flag){// 根据标志是否改变来判断师傅为素数
System.out.println(i + "是素数!");
}else{
flag = true;
}
}
}
}// out>>>307是素数!311是素数!313是素数!317是素数!331是素数!337是素数!347是素数!349是素数!353是素数!359是素数!367是素数!373是素数!379是素数!383是素数!389是素数!397是素数!


7、求斐波那契数列

/**
@author sfbaobao
15有个人想知道,一年之内一对兔子能繁殖多少对?于是就筑了一道围墙
把一对兔子关在里面。已知一对兔子每个月可以生一对小兔子,而一对兔子
从出生后第3个月起每月生一对小兔子。假如一年内没有发生死亡现象,那么,
一对兔子一年内(12个月)能繁殖成多少对?  分析:兔子的规律为数列
1,1,2,3,5,8,13,21,34,55,89,144……
*/
public class Test15{
public static void main(String[] args){
int start1 = 1;
int start2 = 1;
int result = 2;
for(int i=3; i<=12; i++){
result = start1 + start2;
start1 = start2;// 重新赋值
start2 = result;// 重新赋值
}
System.out.println("兔子一年内(12个月)能繁殖成"+ result +"对");
}
}// out>>>兔子一年内(12个月)能繁殖成144对


8、水仙花数

/**
@author sfbaobao
16. 水仙花数(Narcissistic number)也被称为超完全数字不变数、自恋数、自幂数
水仙花数是指一个 n 位数(n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身
(例如:1^3 + 5^3+ 3^3 = 153),请通过程序找出所有的3位数的水仙花数(穷举法)
*/
public class Test16{
public static void main(String[] args){
for(int i=100; i<1000; i++){
int a = i / 100;
int b = i / 10 % 10;
int c = i % 10;
// Math.pow()返回的是double类型
double result = Math.pow(a, 3) + Math.pow(b, 3) + Math.pow(c, 3);
if(result == i){
System.out.println(i + "是水仙花数!");
}
}
}
}
// out>>>153是水仙花数!;370是水仙花数!;371是水仙花数!;407是水仙花数!


9、完全数

/**
@author sfbaobao
11判断一个数是否是完全数(完数指的是一个数的所有因子数的和等于这个数本身,
例如 6=1+2+3,即6就是完全数)
*/
import java.util.Scanner;
public class Test11{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个数字:");
int num = scanner.nextInt();
int result = num;
for(int i=1; i<num; i++){
if(num % i == 0){
result = result - i;// 当result=0时,说明该数字为完全数
}
}
if(result == 0){
System.out.println(num + "是完全数!");
}else{
System.out.println(num + "不是完全数!");
}
}
}// 一百以内大的完全数为6,28


三、作业:

1、while与for循环的区别?

一般情况下for循环和while语句循环可以想换转换。

如果已知循环次数一般用for循环较多,如果不知道循环次数一般使用while循环较多。

在一些特定的输出,例如:迭代器输出一般使用while()来判断是否有下一行内容可输出比较方便。

而数组和集合的输出大多是使用for或者foreach语句循环来输出比较方便

>>”不将就·能创新·耐得住——网络人”
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java