作业1:“循环输出五门成绩平均分_分数为负停止录入”
import java. util. Scanner;
public class 循环输出五门成绩平均分_分数为负停止录入{
public static void main ( String[ ] args) {
int times= 6 ;
double avg= 0 ;
int chengji= 0 ;
int zfs= 0 ;
Scanner a= new Scanner ( System. in) ;
for ( int i= 1 ; i< times; i++ ) {
System. out. println ( "请输入第" + i+ "个成绩" ) ;
chengji= a. nextInt ( ) ;
if ( chengji< 0 ) {
System. out. println ( "您输入错误,还请重新输入" ) ;
i-- ;
}
zfs= zfs+ chengji;
}
System. out. println ( "总分数是:" + zfs) ;
avg= zfs/ 5.0 ;
System. out. println ( "平均数是:" + avg) ;
System. out. println ( "程序结束" ) ;
}
}
作业代码运行效果如下:
作业2:“贵妃减肥”
public class 贵妃减肥 {
public static void main ( String[ ] args) {
double tz= 200.0 ;
int ts= 0 ;
System. out. println ( "贵妃减肥记" ) ;
while ( tz> 99.9 ) {
ts+= 1 ;
if ( ts== 6 || ts== 17 ) {
tz+= 4 ;
System. out. println ( "与皇上出去玩了,共进晚餐,吃的满汉全席,体重大增,现在" + tz+ "斤" ) ;
continue ;
} else if ( ts== 10 ) {
System. out. println ( "减肥的时候摔跤了,腿摔断了,今天不能减肥了,现在" + tz+ "斤" ) ;
continue ;
}
tz-= 0.8 ;
System. out. println ( "运动第" + ts+ "天,现在" + tz+ "斤" ) ;
}
System. out. println ( "贵妃减肥用了" + ts+ "天" ) ;
System. out. println ( "贵妃减肥用了" + ts/ 7 + "周" ) ;
}
}
作业代码如下效果如下: 第一页 第二页 第三页 第四页:
作业3:“编写一个游戏循环”
import java. util. Scanner;
public class 编写一个游戏循环 {
public static void main ( String[ ] args) {
Scanner b= new Scanner ( System. in) ;
System. out. println ( "*游戏正在加载中......请稍等" ) ;
System. out. println ( "*请选择你要登录的方式:\n1.微信登陆\n2.QQ登陆\n3.邮箱登陆" ) ;
int jc= 0 ;
int jc2= 0 ;
int df= 0 ;
while ( jc== 0 ) {
String dl= b. next ( ) ;
if ( dl. equals ( "1" ) || dl. equals ( "2" ) || dl. equals ( "3" ) ) {
System. out. println ( "*登录成功" ) ;
jc= 1 ;
break ;
} else {
System. out. println ( "*请重新选择登录方式:\n1.微信登陆\n2.QQ登陆\n3.邮箱登陆" ) ;
}
}
while ( true ) {
System. out. println ( "*选择游戏模式:\n1.32对32\n2.24对24" ) ;
String yx= b. next ( ) ;
if ( yx. equals ( "1" ) ) {
System. out. println ( "*已选择游戏模式:32对32" ) ;
jc2= 1 ;
while ( jc2== 1 ) {
df= 80 + ( int ) ( Math. random ( ) * ( 100 - 80 + 1 ) ) ;
System. out. println ( "*<32对32>本局得分:" + df) ;
System. out. println ( "*是否继续当前游戏(y)——(输入其他)重新选择游戏模式" ) ;
String a= b. next ( ) ;
if ( a. equals ( "y" ) ) {
System. out. println ( "*继续游戏<32对32>" ) ;
} else {
System. out. println ( "*重新选择游戏模式" ) ;
break ;
}
}
} else if ( yx. equals ( "2" ) ) {
System. out. println ( "*已选择游戏模式:24对24" ) ;
jc2= 2 ;
while ( jc2== 2 ) {
df= 160 + ( int ) ( Math. random ( ) * ( 200 - 160 + 1 ) ) ;
System. out. println ( "*<24对24>本局得分:" + df) ;
System. out. println ( "*是否继续当前游戏(y)——(输入其他)重新选择游戏模式" ) ;
String a= b. next ( ) ;
if ( a. equals ( "y" ) ) {
System. out. println ( "*继续游戏<24对24>" ) ;
} else {
System. out. println ( "*重新选择游戏模式" ) ;
break ;
}
}
} else {
System. out. println ( "*请重新选择游戏模式:\n1.32对32\n2.24对24" ) ;
}
}
}
}
作业代码运行效果如下: