关于循环的⼀些经典例题(经典ifswitch)
import java.util.Scanner;
class Demo3_03{
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.print("今天是周⼏(周⽇为0,周⼀为1,以此类推):");
int Int();
System.out.print("今天之后的天数:");
int Int();
int futureday=(today+future)%7;
String todaystr="";//定义⼀个空字符串,⼀会存字符
String futurestr="";//定义⼀个空字符串,⼀会存字符
if(today==0){// if语句执⾏
todaystr="周⽇";
}else if(today==1){ // 如果 if没执⾏,继续判断这个条件
todaystr="周⼀";
}else if(today==2){// 如上
todaystr="周⼆";
}else if(today==3){
todaystr="周三";
}else if(today==4){
todaystr="周四";
}else if(today==5){
todaystr="周五";
}else if(today==6){
todaystr="周六";
} if(futureday==1){
futurestr="周⼀";
switch case判断字符串}else if(futureday==2){
futurestr="周⼆";
}else if(futureday==3){
futurestr="周三";
}else if(futureday==4){
futurestr="周四";
}else if(futureday==5){
futurestr="周五";
}else if(futureday==6){
futurestr="周六";
}else if(future==0){
futurestr="周⽇";
}
System.out.println("今天是"+todaystr+"未来"+future+"天后是"+futurestr);
}
}
回⽂数
class Demo3_04{
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.print("请输⼊⼀个三位数:");
int Int();
int temp=num;//⼀会⽐较需要的数字,我们⾸先给存起来
int sum=0;
sum=sum*10+num%10; //这是回⽂数的⼀个规律,需要我们记住,因为经常使⽤出三位数最后⼀位*10+倒数第⼆位,后⾯都是⼀样的规律 num/=10;
sum=sum*10+num%10;//如果我们拿999来说 987=(999%10*(10+8)*(10+7));
num/=10;
sum=sum*10+num%10;
num/=10;
if(temp==sum){
System.out.print(temp+"是⼀个回⽂数");
}else
{
System.out.print(temp+"不是⼀个回⽂数");
}
}
}
import java.util.Random;
class Demo3_06{
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.print("请输⼊剪⼑(0),⽯头(1),布(2):");
int Int();
int robot=(int)(Math.random()*3);//左开右闭
//Random random=new Random();
//int Int(3);
String usrStr="";
String robotStr="";
switch(usr){//使⽤Switch 语句
case 0: //if usr==0
usrStr="剪⼑";
break;
case 1:// if usr==1
usrStr="⽯头";
break;
case 2:// if usr==2
usrStr="布";
break;
}
switch(robot){//在使⽤⼀个Switch 语句
case 0:
robotStr="剪⼑";
break;
case 1:
robotStr="⽯头";
break;
case 2:
robotStr="布";
break;
}
if(robot==usr){
System.out.printf("⽤户是%s,电脑是%s,平局",usrStr,robotStr);
}else if(usr==0&&robot==2 || usr==1&&robot==0 || usr==2&&robot==1){ System.out.printf("⽤户是%s,电脑是%s,⽤户赢",usrStr,robotStr); }
else{
System.out.printf("⽤户是%s,电脑是%s,⽤户输",usrStr,robotStr); }
}
}
class Demo03_09{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in); //1.提⽰⽤户输⼊⼀个点的坐标
System.out.print("请输⼊⼀个坐标:");
double Double();
double Double();
//2.先⼤致判断⼀下坐标的范围
/
/3.再精确的判断坐标的范围
if(x>=0&&x<=200&&y<=-0.5*x+100){
System.out.println("点再三⾓形内");
}else{
System.out.println("点再三⾓形外");
}
}
}
画个图好看⼀点这个题
double Double();
//2.再输⼊⼩矩形的中⼼,宽和⾼
System.out.print("请输⼊第2个矩形的信息:");
double Double();
double Double();
double Double();
double Double();
double inXMin=x1-(w1-w2)/2;
double inXMax=x1+(w1-w2)/2;
double inYMin=y1-(h1-h2)/2;
double inYMax=y1+(h1-h2)/2;
double outXMin=x1-(w1+w2)/2;
double outXMax=x1+(w1+w2)/2;
double outYMin=y1-(h1+h2)/2;
double outYMax=y1+(h1+h2)/2;
if(x2>=inXMin&&x2<=inXMax&&y2>=inYMin&&y2<=inYMax){
System.out.println("⼩矩形在⼤矩形⾥⾯!");
}else if(x2<=outXMin||x2>=outXMax||y2<=outYMin||y2>=outYMax){ System.out.println("⼩矩形在⼤矩形外⾯!");
}else{
System.out.println("⼩矩形和⼤矩形相交!");
}
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论