Java学习第二十天<各类循环控制>
作者:互联网
循环控制
For
package chapter04.D4循环控制;
public class ForDetail {
public static void main(String[] args) {
int i=1;
for ( ;i<=10;){
System.out.println("你好"+i);
i++;
}
System.out.println(i);
/*
int j=1;
for (;;){
System.out.println("ok"+(j++));//死循环 control+d退出
}*/
}
}
package chapter04.D4循环控制;
public class ForExercise {
public static void main(String[] args) {
/*
化繁为简
1 完成输出1-100的值
2 输出过程中进行过滤,只输出9的倍数 i%9==0
3 统计个数 定义一个变量 int count = 0; 当条件满足时 count++;
4 总和 int sum=0;当条件满足时 sum+=i;
先死后活
1 为了适应更好的需求,定义范围的开始,结束 int start=10; int end=200; for(int i=start;i<=end;i++)
2 把9的倍数也做成变量 int t=9; if (i%t==0)
*/
int count = 0;
int sum=0;
for(int i=1;i <= 100;i++){
if (i%9==0){
System.out.println("i="+i);
count++;
sum+=i;
}
}
System.out.println("count=" + count);
System.out.println("sum="+sum);
}
}
package chapter04.D4循环控制;
public class ForExercise02 {
public static void main(String[] args) {
/*
化繁为简
1 先输出 0-5
2 后面+的是 5-i
先死后活
1 5替换成变量n int n=5
*/
for (int i=0;i<=5;i++){
System.out.println(i+"+"+(5-i)+"=5");
}
}
}
Mulfor
package chapter04.D4循环控制;
import java.util.Scanner;
public class MulForExercise01 {
public static void main(String[] args) {
/*第一个班5个人成绩和均分
double sum=0;
Scanner myscanner = new Scanner(System.in);
for (int j=1;j<=5;j++){
System.out.println("请输入1班第"+j+"个学生的成绩");
double score = myscanner.nextDouble();
sum+=score;
System.out.println("成绩为"+score);
}
System.out.println("平均分为"+sum/5);*/
//三个班套外层循环
// 统计所有成绩 total
// 统计过的人 passnum
//由死变活 定义班级数 人数
int total=0;
int passnum=0;
for (int i=1;i<=3;i++){
double sum=0;
Scanner myscanner = new Scanner(System.in);
for (int j=1;j<=5;j++){
System.out.println("请输入"+i+"班第"+j+"个学生的成绩");
double score = myscanner.nextDouble();
if (score>=60){
passnum++;
}
sum+=score;
total+=score;
System.out.println("成绩为"+score);
}
System.out.println(i+"班"+"平均分为"+sum/5);
}
System.out.println("总分为"+total);
System.out.println("过的人"+passnum);
}
}
package chapter04.D4循环控制;
public class MulForExercise02 {
public static void main(String[] args) {
/*化繁为简
1 先打印矩形
2 打印半个金字塔 n层有n个星
3 打印整个金字塔 2n-1个星 5-n个空
4 打印空心金字塔 加条件 当前行第一个和最后一个有* 最后一行全部输出
5 先死后活 定义层数
*/
for (int i=1;i<=5;i++){//i为层数
for (int j=1;j<=(5-i);j++){
System.out.print(" ");
}
for(int g=1;g<=(2*i-1);g++){
if (g==1||g==2*i-1||i==5){//最后一层全输出
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.println();//换行
}
}
}
While
package chapter04.D4循环控制;
//打印40-200之间偶数
public class WhileExercise {
public static void main(String[] args) {
int j=40;
while ( j <= 200){
if (j%2==0){
System.out.println("j="+j);
}
j++;
}
}
}
Do while
package chapter04.D4循环控制;
public class DoWhileExercise {
public static void main(String[] args) {
/*统计1-200能被5整除但不能被3整除的个数
1 使用do while 输出1-200
2 过滤
3 计数
4 求和
先死后活
1 范围1-200
2 5和3条件替换
*/
int i=1;
int count=0;
int sum=0;
do {
if (i%5==0 &&i%3!=0){
System.out.println("i="+i);
count++;
sum+=i;
}
i++;
}while(i<=200);
System.out.println("count="+ count);
System.out.println("sum="+ sum);
}
}
package chapter04.D4循环控制;
import java.util.Scanner;
public class DoWhileExercise02 {
public static void main(String[] args) {
int x = 500;
Scanner myscanner = new Scanner(System.in);
String answer =" ";
System.out.println("有一个人前来买瓜....");
do {
if (x >=100){
x-=100;
System.out.println("你被打了,血量-100");
System.out.println("当前血量:" + x);
System.out.println("问你,你这瓜保熟吗?");
answer = myscanner.next();
System.out.println("你的回答:"+answer);
}else {
System.out.println("你被打了,血量-100");
System.out.println("你已经挂了....");
break;
}
}while (!answer.equals("保熟"));//answer = true 不停问
}
}
Break
package chapter04.D4循环控制;
public class BreakExercise {
public static void main(String[] args) {
int sum=0;
int n=0;
for (int i=1;i<=100;i++){
sum+=i;
if (sum>20){
System.out.println("和>20时候 当前数i="+i);
n=i;
break;
}
}
System.out.println("当前数="+n);
}
}
package chapter04.D4循环控制;
import java.util.Scanner;
public class BreakExercise02 {
public static void main(String[] args) {
Scanner myscanner = new Scanner(System.in);
String name = " ";
String passwd = " ";
int chance=3;
for (int i=1; i<=3; i++){
System.out.println("请输入账号");
name=myscanner.next();
System.out.println("请输入密码");
passwd=myscanner.next();
if ("征途".equals(name)&&"666".equals(passwd)){
System.out.println("恭喜你,登陆成功!");
break;
}
chance--;
System.out.println("你还有"+chance+"次机会");
}
}
}
Continue
package chapter04.D4循环控制;
//画内存分析图
public class ContinueDetail {
public static void main(String[] args) {
label1:
for (int j=0;j<2;j++){
label2:
for (int i=0;i<10;i++){
if(i==2){
//continue;
continue label1;
}
System.out.println("i="+i);
}
}
}
}
Homework
package chapter04.D4循环控制;
public class Homework01 {
public static void main(String[] args) {
double money=100000;
int count=0;
while (true){
if (money>50000){
money*=0.95;
count++;
}else if (money>=1000 ){
money-=1000;
count++;
}else {//钱不够
break;
}
}
System.out.println("可以过"+count+"个路口");
}
}
package chapter04.D4循环控制;
public class Homework02 {
public static void main(String[] args) {
//水仙花数 先得到个十百位,再用if判断立方和是否相等
int n=153;
int n1=n/100;//百位
int n2=n%100/10;//十位
int n3=n%10;//个位
if (n1*n1*n1+n2*n2*n2+n3*n3*n3==n){
System.out.println(n+"是水仙花数");
}else{
System.out.println(n+"不是水仙花数");
}
}
}
package chapter04.D4循环控制;
public class Homework03 {
public static void main(String[] args) {
int count =0;
for (int i=1;i<=100;i++){
if (i%5!=0){
count++;
System.out.print(i+"\t");
if (count%5==0){
System.out.println();
}
}
}
}
}
package chapter04.D4循环控制;
public class Homework04 {
public static void main(String[] args) {
for (char c1='a';c1<='z';c1++){
System.out.print(c1+" ");
}
for (char c1='Z';c1>='A';c1--){
System.out.print(c1+" ");
}
}
}
package chapter04.D4循环控制;
public class Homework05 {
public static void main(String[] args) {
double sum=0;
for (int i=1;i<=100;i++){
if (i%2!=0){
sum=sum+(1.0/i);//隐藏陷阱,分子1要变1.0
}else {
sum-=1.0/i;
}
}
System.out.println("sum="+sum);
}
}
package chapter04.D4循环控制;
public class Homework06 {
public static void main(String[] args) {
//1+(1+2)+(1+2+3)...+(1+2...+100)
//100项 双循环 i是当前项的最后一个数
int sum=0;
for (int i=1;i<=100;i++){
for (int j=1;j<=i;j++){
sum+=j;
}
}
System.out.println("sum="+sum);
}
}
return
package chapter04.D4循环控制;
public class Return01 {
public static void main(String[] args) {
for (int i = 1; i <=5 ; i++) {
if (i==3){
System.out.println("征途"+i);
break;
//continue;调到上一次循环{前
// return;用在方法时退出方法,主方法退出程序
}
System.out.println("Hello World!");
}
System.out.println("go on...");
}
}
标签:Java,十天,sum,System,int,循环,println,public,out 来源: https://www.cnblogs.com/wybqjcdd/p/15969686.html