刷题的狂欢-----JAVA每日三练-----第十八天
Posted 敲代码的xiaolang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了刷题的狂欢-----JAVA每日三练-----第十八天相关的知识,希望对你有一定的参考价值。
努力刷题,每日三题。-------题目来源 “牛客网”
第一题
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] arr = new int[7];
for (int i = 0; i < arr.length; i++) {
arr[i] = scanner.nextInt();
}
scanner.close();
for(int j = 0; j < 7; j++){
for(int l = 0; l < 6-j; l++){
if(arr[l] > arr[l+1]){
int temp;
temp = arr[l];
arr[l] = arr[l+1];
arr[l+1] = temp;
}
}
}
for (int k = 0;k < arr.length; k++) {
System.out.print(arr[k]+" ");
}
}
}
第二题
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextDouble()) {
double s = scanner.nextDouble();
System.out.println(String.format("%.3f",new Circle(s).getArea()));
System.out.println(String.format("%.3f", new Square(s).getArea()));
}
}
}
class Shape {
private double s; // 周长
public Shape(double s) {
this.s = s;
}
public double getS() {
return s;
}
}
interface Area {
double getArea(); // 面积
}
// 圆形
class Circle extends Shape implements Area {
public Circle(double s) {
super(s);
}
public double getArea() {
double r = getS()/(Math.PI*2);
double area = Math.PI*r*r;
return area;
}
}
// 方形
class Square extends Shape implements Area {
public Square(double s) {
super(s);
}
public double getArea() {
double r = getS()/4;
double area = r*r;
return area;
}
}
第三题
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int start = scanner.nextInt();
int end = scanner.nextInt();
method(start,end);
}
public static void method(int start,int end){
int count=0;
if(end < start){
int temp = start;
start = end;
end = temp;
}
if(end == start){ //此处使用if,而不是else if
count = 0;
}
else{
int i,start1;
int number=0;
if(start <3){
start1 = 3;
}
else{
start1 = start;
}
for(i = start1; i <= end; i++){
int j;
for(j = 1; j <= i; j++){
if(i%j==0){
number = number + 1;
}
}
if(number <= 2){
count++;
}
number = 0;
}
}
System.out.println(start+"到"+end+"之间有"+count+"个素数");
}
}
😉😉😉有问题评论区留言,欢迎大家浏览下面的博文,并参与博主设置的投票当中。😊😊😊
https://blog.csdn.net/weixin_52605156/article/details/120689189?spm=1001.2014.3001.5501
以上是关于刷题的狂欢-----JAVA每日三练-----第十八天的主要内容,如果未能解决你的问题,请参考以下文章