语句结构

来源:互联网 发布:停车场画线软件 编辑:程序博客网 时间:2024/06/05 15:42

目录

for循环
while循环
do while循环
switch
if else

for循环

求 1000以内的水仙花数

public class Follwer {    public static void main(String[] args) {        for (int a = 1; a < 10; a++) {            for (int b = 0; b < 10; b++) {                for (int c = 0; c < 10; c++) {                    if (a*a*a+b*b*b+c*c*c==a*100+b*10+c) {                        System.out.println("水仙花数为:"+a+b+c);                    }                }            }        }                //第二种方法        int i,j,k;        for (int a =100; a < 1000; a++) {            i=a/100;       //分解出百位             j=a/10%10;     //分解出十位             k=a%10;        //分解出个位             if(a==i*i*i+j*j*j+k*k*k)             {                 System.out.println("水仙花数位:"+a);            }         }     }}

while循环

输入一个二进制数,并求出又多少个1

public class One {    public static void main(String[]args){        System.out.println("请输入一个数:");        Scanner scan=new Scanner(System.in);        int a=scan.nextInt();        int count=0;        while (a!=0) {            if (a%2==1) {                count++;            }            a/=2;        }        System.out.println("1的个数为:"+count);    }}

do while循环

求1~1000的和

public class Data_sum {    public static void main(String[] args) {        //求1~1000的和        //用for循环实现        System.out.println("用for循环实现");        int sum=0;        for (int i = 0; i <=1000; i++) {            sum+=i;         }        System.out.println("1~1000的和为:"+sum);        //用while循环实现        System.out.println("用while循环实现");        int sum1=0;        int j=0;        while (j<=1000) {            sum1+=j;            j++;        }        System.out.println("1~1000的和为:"+sum1);        // 用do while实现循环        System.out.println("用do while实现循环");        int a=0;        int sum2=0;        do {            sum2+=a;            a++;        } while (a<=1000);        System.out.println("1~1000的和为:"+sum2);    }   }

分支结构

for循环

输出学生成绩信息

package HelloWorld;import java.util.Scanner;public class Score {    public static void main(String[]srgs){        Scanner sca=new Scanner(System.in);        int score=sca.nextInt();        //System.out.println("请输入学生成绩!");        if(score>100||score<0){            System.out.println("输入有误,请重新输入!");        }else if (score>=90) {            System.out.println("该生成绩为优秀!");        }else if (score>=80) {            System.out.println("该生成绩为良好!");        }else if (score>=70) {            System.out.println("该生成绩为中等!");        }else if (score>=60) {            System.out.println("该生成绩为及格!");        }else if (score<60) {            System.out.println("该生成绩不及格!");        }    }}

switch循环

输出日期

import java.util.Scanner;public class Score {    public static void main(String[] args) {        System.out.println("请输入一个10以内的数");        Scanner scan=new Scanner(System.in);                int week=scan.nextInt();            switch (week) {        case 1:            System.out.println("Monday");            break;        case 2:            System.out.println("Tuesday");            break;        case 3:            System.out.println("Wendesday");            break;        case 4:            System.out.println("Thursday");            break;        case 5:            System.out.println("Friday");            break;        case 6:            System.out.println("Staturday");            break;        case 7:            System.out.println("Sunday");            break;        default:            System.out.println("Soory,I don't konw!");            break;        }    }}
0 0
原创粉丝点击