当前位置:网站首页>判断某一天是当年的第几天,计算油耗情况,设s=1*2*3*4*5*……*n,求s不大于400000时最大的n。拆分数字,素数,将三个整数按从小到大排序,回文数,判断三个边长能否组成三角形。判断水仙花数
判断某一天是当年的第几天,计算油耗情况,设s=1*2*3*4*5*……*n,求s不大于400000时最大的n。拆分数字,素数,将三个整数按从小到大排序,回文数,判断三个边长能否组成三角形。判断水仙花数
2022-08-06 05:24:00 【半世晨晓1128】
1.输入年月日,返回这一天是当年的第几天。
package For;
public class A {
//在类中编写一个方法,输入年月日,返回这一天是当年的第几天,输入三个数字表示年月日:2019 9 25,返回数字表示一年中的第几天:268
public int m(int x, int y, int z) {
int s = 0;
for (int i = 1; i < y; i++) {
if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) {
s = s + 31;
} else if (i == 4 || i == 6 || i == 9 || i == 11) {
s = s + 30;
} else {
if ((x % 4 == 0 && x % 400 != 0) || x % 100 == 0) {
s = s + 29;
} else {
s = s + 28;
}
}
}
s = s + z;
return s;
}
}
package For;
public class ATest {
public static void main(String[] args) {
A a = new A();
int m = a.m(2019, 9, 25);
System.out.println(m);
}
}
2.计算油耗情况。
package For;
import java.util.Scanner;
//计算油耗情况。要求输入行使的英里数,以及每次加了多少升汽油。程序应计算并显示每次加油后,每升汽油可供行驶多少公里。程序还应综合所有的输入,计算并输出每升汽油可以供行驶多少公里。
public class B {
public static void main(String[] args) {
double a = 0.0;
double b = 0.0;
for (int i = 1; i > 0; i++) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入加油量");
double x = scan.nextInt();
System.out.println("请输入公里数");
double x1 = scan.nextInt();
double x2;
x2 = x1 / x;
System.out.println("每升油行驶" + x2 + "公里");
a += x;
b += x1;
double c;
c = b / a;
System.out.println("平均每升油行驶" + c + "公里");
}
}
}
3.设s=12345*……*n,求s不大于400000时最大的n。
package For;
public class A {
//设s=1*2*3*4*5*……*n,求s不大于400000时最大的n
public static void main(String[] args) {
int s = 1;
int n = 1;
while (s <= 10) {
n++;
s *= n;
}
n--;
System.out.println(n);
}
}
4.拆分数字,要求输入一个数字,数字中包含5个数位。把数字分解成单独的数位,并打印每一个数位。
package For;
import java.util.Scanner;
public class A {
//拆分数字,要求输入一个数字,数字中包含5个数位。把数字分解成单独的数位,并打印每一个数位。例如,假定输入43263这个数字,那么程序应打印结果:4 3 2 6 3。
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入一个数字:");
int x = scan.nextInt();
String s = "";
while (x > 0) {
//43263
int y = x % 10; //3
s = y + " " + s;
x = x / 10; //4326
}
System.out.println(s);
}
}
5.判断数字是否是素数
package For;
public class A {
//在类中编写一个方法,判断数字是否是素数. 传入参数:10,返回:false; 传入参数:11,返回:true
public String m(int x) {
String s = "true";
for (int i = 2; i <= Math.sqrt(x); i++) {
if (x % i == 0) {
s = "false";
break;
}
}
if (x == 1) {
s = "既不是素数也不是合数";
}
return s;
}
}
package For;
public class ATest {
public static void main(String[] args) {
A a = new A();
String b = a.m(4);
System.out.println(b);
}
}
6.将任意三个整数 a,b,c 按从小到大排序。
package For;
//将任意三个整数 a,b,c 按从小到大排序.要求:输入三个参数,返回一个排好顺序的字符串。
public class A {
int x;
int y;
int z;
public void m(int a, int b, int c) {
if (a > b) {
x = a;
a = b;
b = x;
}
if (a > c) {
z = a;
a = c;
c = z;
}
if (b > c) {
y = b;
b = c;
c = y;
}
System.out.print(a + " ");
System.out.print(b + " ");
System.out.print(c);
}
}
package For;
public class ATest {
public static void main(String[] args) {
A t = new A();
t.m(3, 12, 1);
}
}
7.判断回文数。
package For;
public class A {
//判断回文数。输入数字,如果是“回文”,返回true,否则返回false。回文数无论顺读,还是倒读,结果都是一样的。 例如,下面这些整数其实都是“回文”: 11、1221、12321.
public boolean m(int x) {
int y = x; //备份
String s = "";
//拆分数字
while (x > 0) {
int z = x % 10;
x /= 10;
s = s + z;
}
//字符串转化为数字
int a = Integer.valueOf(s);
if (y == a) {
return true;
} else {
return false;
}
}
}
package For;
public class ATest {
public static void main(String[] args) {
A t = new A();
boolean b = t.m(1221);
System.out.println(b);
}
}
8.判断三个边长能否组成三角形。
package For;
public class A {
//判断三个边长能否组成三角形。输入三个非零的整数值,判断它们是否能代表一个三角形的3个边长,如果能,返回true;如果不能返回false。(提示:判断三个边长能否组成三角形的规则是:任意两个边长之和大于第三个边长)
public boolean m(int x, int y, int z) {
if (x > 0 && y > 0 && z > 0 && x + y > z && x + z > y && y + z > x) {
return true;
} else {
return false;
}
}
}
package For;
public class ATest {
public static void main(String[] args) {
A t = new A();
boolean b = t.m(3,2,2);
System.out.println(b);
}
}
9.判断水仙花数。
package For;
public class A {
//判断水仙花数。用程序找出每位数的立方和等于该数本身的值的所有的 3 位数。
public void m() {
for (int i = 100; i <= 999; i++) {
int a = i;//备份
int b = 0;
//拆分数字
while (a > 0) {
int x = a % 10;
int c = x * x * x;
a /= 10;
b = b + c;
}
if (b == i) {
System.out.println(i);
}
}
}
}
package For;
public class ATest {
public static void main(String[] args) {
A t = new A();
t.m();
}
}
边栏推荐
- TCP protocol to understand and use four times and three times handshake
- 4.2 木马攻防实验
- Office2016登录的账户名和microsoft账户名不照应的解决方法
- 落地实践 | 网络安全网格架构(CSMA)的正确打开姿势
- Distributed, Microservices, Cluster Concepts and Differences
- lvs-dr
- 勒索软件攻击防御的9件事
- shell脚本编写(4.函数调用)
- 桌面上随便创建一个word文档,打开它都是显示兼容模式的解决方法
- 揭示OT安全四大挑战!Fortinet 发布《2022年全球运营技术和网络安全态势报告》
猜你喜欢

高危漏洞预警 | Atlassian Confluence OGNL注入命令执行漏洞复现与分析报告

vmware设置多端口对应多网卡(桥接模式)

The Home Assistant container on the Raspberry Pi uses the command sensor to obtain and display information such as CPU temperature, memory usage, etc.

LAMP+DISCUZ论坛搭建流程

动态规划之最小路径之和

4.3 木马隐藏分析

office运行时错误,部分系统文件可能丢失或已损坏(错误代码:0x80040154)

Serial USART and UART

PCIe 5.1 - Introduction

antdesign 动态引入icon
随机推荐
shell之循环语句
树莓派官方系统取消pi用户,没有显示器如何初始化默认用户并进行SSH连接?
openstack各种服务的管理
Haproxy集群负载均衡详解
Office2016登录的账户名和microsoft账户名不照应的解决方法
Fortinet :《2021 年OT与网络安全现状报告》之「OT安全洞察」
MITRE ATT&CK报告发布 FortiEDR连续两年100%拦截恶意攻击
动态规划之乘积最大子数组
Explain in detail how to install Home Assistant Supervised on Raspberry Pi to make smart devices at home smarter
jpa中orphanRemoval和cascade如何理解
ARM64异常之异常入口和异常返回
LVS load balancing server construction
redis-(error) CLUSTERDOWN Hash slot not served
零基础搭建自己的饥荒Don‘t Starve服务器,摆脱联机卡顿和小伙伴快乐联机
4.2 木马攻防实验
系统安全管理
Docker 快速安装&搭建 Mysql 环境
5.5 漏洞扫描:Web安全漏洞扫描及审计
iCopy升级到- v0.2.1-beta.1及后变化(不推荐升级)
Kindle access to HomeAssistant: Realize lock screen wallpaper to display device information in HA and obtain Kindle power in HA