美文网首页
while循环 for循环

while循环 for循环

作者: 我的好昵称 | 来源:发表于2019-01-13 18:52 被阅读0次

循环语句while
i=1
while i<=20:
if i%5==0:
print(i)
else:
print(i,end=”)
i+=1

输入开始和结尾两个数,每5个输出一行
x,y=int(input()),int(input())
while x<=y:
print(x,end=”)
if x%5==0:
print()
x+=1
输入任意整数
输出1-n的和
n=int(input(‘输入任意整数’))
i=1
data2=0
data=0
while i<=n:
data2=data2+i
i+=1
print(‘1+%d=%d’%(n,data2)

    显示1到100的数,每行显示5个。
for (int i=1;i<=100;i++){
if (i%5==0){
    System.out.print("\t"+i+"\n");
}else{
    System.out.print("\t"+i);
    }
  }




      显示三位数中可以被8整除的数,每行显示8个。
        int j=0;
        for (int i=100;i<1000;i++){
        if (i%8==0){
        System.out.print("\t"+i);
        j++;
        if (j==8){
            j=0;
            System.out.println();
            }
        }
    }

    输入10个数,统计偶数个数及其平均数。
  Scanner sc=new Scanner(System.in);
int j=0,z=0;
int temp=0;
double count1=0,count2=0;
  System.out.println("请输入10个数:");
    for (int i=0;i<10;i++){
    try {
        System.out.print("输入第"+(i+1)+"个数:");
        temp=sc.nextInt();
    }catch (Exception e){
        System.out.println("程序错误");
        break;
    }
  if (temp<0){
        System.out.println("请输入正整数!\n程序结束!");
        break;
    }else if (temp%2==0){
        count1=count1+temp;
        j++;
      }else{
    count2=count2+temp;
        z++;
    }  
}
if (j==0){
  System.out.println("共有"+j+"个奇数,平均值为:0");
}else{
   System.out.println("共有"+j+"个偶数,平均值位:"+(count1/j));
    }
if (z==0){
}else{
  System.out.println("共有"+z+"个奇数,平均值为:"+(count2/z));
  }

打印乘法口诀表
for (int i=1;i<10;i++){
    for (int j=1;j<=i;j++){
        System.out.print(j+"*"+i+"="+(i*j)+"\t");
    }
    System.out.println();
}
  计算1到100相加的和。
int sum=0;
for (int i=1;i<=100;i++){
    sum+=i;
}
  System.out.println(sum);
计算从1累加到和为3003时的次数
int sum=0;
for (int i=1;;i++){
    sum+=i;
    if (sum==3003){
        System.out.println(i+"--"+sum);
        break;
    }
}

相关文章

  • 循环结构

    for循环 for in循环 while循环 do while循环

  • shell流程控制-while循环语句

    shell流程控制-while循环语句 while循环介绍 while循环语法 while实战 一、while循环...

  • Java循环结构

    Java中有三种主要的循环结构: while 循环do…while 循环for 循环一、while 循环while...

  • 从零开始Swift之控制流

    循环 For-In 循环 For-In 遍历数组 while 循环 repeat-while循环, "while"...

  • JavaScript-循环语句

    循环语句有两种 for 和 while 。 while 循环 for 循环 do...while 循环 这个循环和...

  • C语言学习 day4

    循环语句 while循环 //在while循环中,break用于永久地终止循环//在while循环中,contin...

  • C++循环

    C++中的循环主要包含while循环、for循环、do…while循环以及嵌套循环,while循环就是当给定条件为...

  • 小白从JavaScript变身的过程5

    根据前言,开始了循环语句“while循环语句”、“for循环语句”, 先从while循环语句开始, while()...

  • While笔记

    #While循环 1.语法 while(循环条件) { 循环...

  • 循环语法

    1、for循环 2、while循环 和 do while循环

网友评论

      本文标题:while循环 for循环

      本文链接:https://www.haomeiwen.com/subject/vceldqtx.html