java代码示例(4)

 /**
* 需求分析:计算100以内的和,用while
* @author chenyanlong
* 日期:2017/10/14
*/
package com.hp.test04; public class HS_Loop1 { public static void main(String[] args) {
// TODO Auto-generated method stub
int i=1,sum=0; while(i<=100){
sum=sum+i;
i++;
}
System.out.println("100以内的和:"+sum);
} }
 /**
* 需求分析:计算100以内的和,用while
* @author chenyanlong
* 日期:2017/10/14
*/
package com.hp.test04; public class HS_Loop1 { public static void main(String[] args) {
// TODO Auto-generated method stub
int i=1,sum=0; while(i<=100){
sum=sum+i;
i++;
}
System.out.println("100以内的和:"+sum);
} }
 /**
* 需求分析:输入5门成绩求平均分
* @author chenyanlong
* 日期:2017/10/14
*/
package com.hp.test04; import java.util.Scanner; public class HS_Loop3 { public static void main(String[] args) {
// TODO Auto-generated method stub
String name;
double sum=0,score;
double avg=0;
System.out.println("请输入姓名:"); Scanner input= new Scanner(System.in);
name=input.next();
//System.out.println(name);
for(int i=1;i<=5;i++){
System.out.print("请输入5门功课的第"+i+"门功课的成绩:"); Scanner input2=new Scanner(System.in);
score=input2.nextDouble();
sum=sum+score;
}
avg=sum/5;
System.out.println(name+"的平均分是:"+avg);
}
}
 /**
* 需求分析:输入5门成绩求平均分,可以输入多个人
* @author chenyanlong
* 日期:2017/10/14
*/
package com.hp.test04; import java.util.Scanner; public class HS_Loop4 { public static void main(String[] args) {
// TODO Auto-generated method stub
String letter=null;
do{
String name;
double sum=0,score;
double avg=0;
System.out.println("请输入姓名:"); Scanner input= new Scanner(System.in);
name=input.next();
//System.out.println(name);
for(int i=1;i<=5;i++){
System.out.print("请输入5门功课的第"+i+"门功课的成绩:"); Scanner input2=new Scanner(System.in);
score=input2.nextDouble();
sum=sum+score;
}
avg=sum/5;
System.out.println(name+"的平均分是:"+avg); System.out.print("继续输出吗?(Y/N):");
Scanner input3=new Scanner(System.in);
letter=input3.next();
}while(letter.equals("Y")||letter.equals("y"));
System.out.println("录入完毕");
}
}
 /**
* 需求分析:break的用法
* @author chenyanlong
* 日期:2017/10/14
*/
package com.hp.test04; import java.util.Scanner; public class HS_Loop5 { public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=1;i<10;i++){
if(i==5){
System.out.println("跳出一次");
continue;
}
System.out.println(" "+i);
}
}
}
 /**
* 需求分析:break的用法
* @author chenyanlong
* 日期:2017/10/14
*/
package com.hp.test04; import java.util.Scanner; public class HS_Loop6 { public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=1;i<10;i++){
if(i==5){
System.out.println("循环提前结束");
break;
}
System.out.println(" "+i);
}
}
}
上一篇:探求C#.Net中ArrayList与Array的区别


下一篇:C++的反思[转]