-
import java.util.Scanner;
-
class demo3_1{
-
public static void main(String[] argus){
-
Scanner scanner=
new Scanner(System.in);
-
System.out.print(
"请输入a b c:");
-
double a=scanner.nextDouble();
-
double b=scanner.nextDouble();
-
double c=scanner.nextDouble();
-
double delt=b*b-
4*a*c;
-
if(delt>
0){
-
double r1=(-b + Math.sqrt(delt))/(
2*a);
-
double r2=(-b - Math.sqrt(delt))/(
2*a);
-
System.out.printf(
"r1=%.2f,r2=%.2f",r1,r2);
-
}
else
if(delt==
0){
-
double r=(-b + Math.sqrt(delt))/(
2*a);
-
System.out.printf(
"r=%.2f",r);
-
}
else{
-
System.out.printf(
"没有实数解!");
-
}
-
}
-
-
}
-
import java.util.Scanner;
-
class demo3_2{
-
public static void main(String[] args){
-
Scanner scanner=
new Scanner(System.in);
-
System.out.println(
"Enter a,b,c,d,e,f:");
-
double a=scanner.nextDouble();
-
double b=scanner.nextDouble();
-
double c=scanner.nextDouble();
-
double d=scanner.nextDouble();
-
double e=scanner.nextDouble();
-
double f=scanner.nextDouble();
-
double A=(a*d-b*c);
-
double B=(e*d-b*f);
-
double C=(a*f-e*c);
-
double x;
-
double y;
-
if(A!=
0){
-
x=B/A;
-
y=C/A;
-
System.out.println(
"x is" + x +
" and y is" + y);
-
-
}
else{
-
System.out.println(
"方程式无解");
-
}
-
-
}
-
}
分析:
数据:今天的周几 未来的天数 未来的周几
(0+12)%7=5
(今天的周几+未来的天数)%7=未来的周几
步骤:
1.输入今天是周几
2.输入未来的几天
3.打印未来的几天是周几
-
-
import java.util.Scanner;
-
class demo3_3{
-
public static void main(String[] argus){
-
//1.
-
Scanner scanner=
new Scanner(System.in);
-
System.out.print(
"今天是周几:");
-
int today=scanner.nextInt();
-
//2.
-
System.out.print(
"未来的几天:");
-
int future=scanner.nextInt();
-
//3.
-
int futureDay=(today+future)%
7;
-
String todayStr=
"";
-
String futureDayStr=
"";
-
if(today==
0){
-
todayStr=
"周日";
-
}
else
if(today==
1){
-
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==
0){
-
futureDayStr=
"周日";
-
}
else
if(futureDay==
1){
-
futureDayStr=
"周一";
-
}
else
if(futureDay==
2){
-
futureDayStr=
"周二";
-
}
else
if(futureDay==
3){
-
futureDayStr=
"周三";
-
}
else
if(futureDay==
4){
-
futureDayStr=
"周四";
-
}
else
if(futureDay==
5){
-
futureDayStr=
"周五";
-
}
else
if(futureDay==
6){
-
futureDayStr=
"周六";
-
}
-
System.out.printf(
"今天是" + todayStr +
",未来的日子是" + futureDayStr );
-
-
}
-
}
分析:
12345
12345%10=5 12345/10=1234
1234%10=4 1234/10=123
123%10=3 123/10=12
12%10=2 12/10=1
1%10=1 1/10=0
54321
5*10000+4*1000+3*100+2*10+1
(5*1000+4*100+3*10+2)*10+1
((5*100+4*10+3)*10+2)*10+1
(((5*10+4)*10+3)*10+2)*10+1
((((0*10+5)*10+4)*10+3)*10+2)*10+1
sum=0
sum=sum*10+5; //5
sum=sum*10+4; //54
sum=sum*10+3; //543
sum=sum*10+2; //5432
sum=sum*10+1; //54321
-
-
import java.util.Scanner;
-
class demo3_4{
-
public static void main(String[] args){
-
//1.输入一个数字
-
Scanner scanner=
new Scanner(System.in);
-
System.out.print(
"请输入一个数字:");
-
int num=scanner.nextInt();
-
int temp=num;
//给num备份
-
//2.拼接出该数字的反序
-
int sum=
0;
-
sum=sum*
10+num%
10;
-
num/=
10;
-
sum=sum*
10+num%
10;
-
num/=
10;
-
sum=sum*
10+num%
10;
-
num/=
10;
-
/* sum=sum*10+num%10;
-
num/=10;
-
sum=sum*10+num%10;
-
num/=10;
-
sum=sum*10+num%10;
-
num/=10;*/
-
if(sum==temp){
-
System.out.println(
"是回文");
-
}
else{
-
System.out.println(
"不是回文");
-
}
-
}
-
}
分析:
数据:电脑随机产生的一个数字com 用户输入的一个数字usr
0 1 2
剪刀 石头 布
步骤:
1.提示用户输入一个数字
2.计算机随机产生一个数字
3.将两个数字进行对比,分输赢
平局 com==usr
用户赢 usr=0 com=2 | usr=1 com=0 | usr=2 com=1
用户输 剩下的都是用户输
随机数
Math.random() [0,1.0)*3 -> [0,3.0)->(int)[0,3.0)
Random nextInt(n)
本题使用: int num=(int)(Math.random()*3);
System.out.println(num);
import java.util.*; class demo3_6{ public static void main(String[] args){ //1. Scanner scanner= new Scanner(System.in); System.out.print( "请输入 剪刀0 石头1 布2:"); int usr=scanner.nextInt(); //2. Random random= new Random(); int com=random.nextInt( 3); String usrStr= ""; String comStr= ""; switch(usr){ case 0: //if usr==0 usrStr= "剪刀"; break; case 1: // if usr==1 usrStr= "石头"; break; case 2: // if usr==2 usrStr= "布"; break; } switch(com){ case 0: comStr= "剪刀"; break; case 1: comStr= "石头"; break; case 2: comStr= "布"; break; } //3. if(usr==com){ System.out.printf( "用户是%s,电脑是%s,平局",usrStr,comStr); } else if(usr== 0&&com== 2 || usr== 1&&com== 0 || usr== 2&&com== 1){ System.out.printf( "用户是%s,电脑是%s,用户赢",usrStr,comStr); } else{ System.out.printf( "用户是%s,电脑是%s,用户输",usrStr,comStr); } } }
数据:圆的圆心 圆的半径 点的坐标 距离
步骤:
1.提示用户输入点的坐标
2.计算点到圆心之间的距离
3.判断距离和半径之间的关系
距离>半径 点在圆外
距离==半径 点在圆上
距离<半径 点在圆内
import java.util.Scanner; class demo3_8{ public static void main(String[] args){ //1. Scanner scanner= new Scanner(System.in); System.out.print( "请输入坐标点:"); double x=scanner.nextDouble(); double y=scanner.nextDouble(); //2. double xo= 0; double yo= 0; double radius= 10; double distance=Math.sqrt(Math.pow(x-xo, 2)+Math.pow(y-yo, 2)); //3. if(distance>radius){ System.out.println( "点在圆外"); } else if(distance==radius){ System.out.println( "点在圆上"); } else{ System.out.println( "点在圆内"); } } }
转载:https://blog.csdn.net/qq_43276642/article/details/104413877