语句结构
来源:互联网 发布:停车场画线软件 编辑:程序博客网 时间: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
- 语句结构
- 结构语句
- 选择结构语句---if语句,switch语句
- 表结构复制语句
- sql语句结构
- while语句循环结构
- 修改表结构语句
- C语言语句结构
- 控制语句结构
- 是什么结构语句?
- Lua控制结构语句
- lua控制结构语句
- SQL语句基本结构
- 选择结构(switch语句)
- 循环结构(for语句)
- 循环结构(while语句)
- 选择语句结构if
- 6、循环结构语句
- [iPhone]上[Uber]突破串号限制
- nyoj243 交换输出
- 基于Android源码的模块编译
- Python垃圾回收机制
- JSP方便路径
- 语句结构
- 【leetcode】27. Remove Element
- MySQL主从复制(Master-Slave)
- IOS学习笔记-OC初体验
- 将一些文档(.zip,.txt,.pdf)进行流处理,存储到mysql中blob字段中
- HEAD FIRST HTML&CSS 读书笔记(2)
- VS2012 使用BUG
- 调用返回风格(面向对象与主程序子程序)
- Labview软件的特点