1.从键盘连续输入任意个整数,(当输入为0 时,结束输入),分别求其中正数、负数的平均值,并输出结果。
2.任意输入三个整数A,B,C,按从小到大的顺序排列输出。
3.求方程ax2+bx+c=0的解,并输出求解结果,其中a可以为0。
4.输入包括两个运算量和一个运算符(+、-、*、/)的算术表达式,计算并输出运算结果。例如: 输入:10+20,输出:10+20=30
5.任意输入3条边的边长,计算三角形的面积(需要判定输入的三边是否可以构成三角形)
6. 输入任意一个整数,求其逆序数并输出,例如,输入1234,输出4321。
7. 求1+2+3+4+…+n,当和值刚大于10000时,n的值是多少?
8. 使用级数求PI的近似值
9. 根据x的值,按下式计算y的值,并保留小数点后2位有效数字(实现四舍五入)。
10. 输出并统计100-1000之间所有的素数。
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class Test {
public static void main(String[] args)
{
//work1();
//work2();
//work3();
//work4();
//work5();
//work6();
//work7();
//work8();
//work9();
work10();
}
//1.从键盘连续输入任意个整数,(当输入为0 时,结束输入),分别求其中正数、负数的平均值,并输出结果。
public static void work1()
{
Scanner sc = new Scanner(System.in);
int T,sum1=0,sum2=0,count1=0,count2=0;
T=sc.nextInt();
while(true)
{
if(T>0)
{
sum1+=T;
count1++;
}
else if(T<0)
{
sum2+=T;
count2++;
}
else
{
break;
}
T=sc.nextInt();
}
double avg1=(double)sum1/(double)count1;
double avg2=(double)sum2/(double)count2;
System.out.println("正数的和: "+avg1);
System.out.println("负数的和: "+avg2);
}
//2.任意输入三个整数A,B,C,按从小到大的顺序排列输出。
public static void work2()
{
Scanner sc = new Scanner(System.in);
int[] str = new int[3];
for(int i = 0;i<3;i++)
{
String temp;
temp=sc.next();
str[i]=(int)temp.charAt(0);
// System.out.println(str[i]);
}
Arrays.sort(str);
for(int i = 0;i<3;i++)
{
char temp = (char) str[i];
// System.out.print(str[i]);
System.out.print(temp+" ");
}
}
//3.求方程ax2+bx+c=0的解,并输出求解结果,其中a可以为0。
public static void work3()
{
Scanner sc = new Scanner(System.in);
double a,b,c,d,e;
a = sc.nextDouble();
b = sc.nextDouble();
c = sc.nextDouble();
d = b*b-4*a*c;
if(a==0)
System.out.println("x="+(-c/b));
else if(d<0)
System.out.println("无解");
else if(d==0)
System.out.println("x="+(-b/(2*a)));
else {
e = Math.sqrt(d);
System.out.println("x1="+(-b+e)/(2*a));
System.out.println("x2="+(-b-e)/(2*a));
}
}
//4.输入包括两个运算量和一个运算符(+、-、*、/)的算术表达式,计算并输出运算结果。例如: 输入:10+20,输出:10+20=30
public static void work4()
{
Scanner sc = new Scanner(System.in);
double a,b,c = 0.0;String s;
a = sc.nextDouble();
s = sc.next();
b = sc.nextDouble();
if("+".equals(s))
{
c=a+b;
}
if("-".equals(s))
{
c=a-b;
}
if("*".equals(s))
{
c=a*b;
}
if("/".equals(s))
{
c=a/b;
}
System.out.println(a +" "+(char)s.codePointAt(0) +" "+ b+" "+ "=" + c) ;
}
//5.任意输入3条边的边长,计算三角形的面积(需要判定输入的三边是否可以构成三角形)
public static void work5()
{
Scanner sc = new Scanner(System.in);
double a,b,c,d;
a = sc.nextDouble();
b=sc.nextDouble();
c= sc.nextDouble();
d=(a+b+c)/2;
boolean flag;
flag = ((a+b)>c&&(a+c)>b&&(b+c)>a);
if(flag)
{
System.out.println(Math.sqrt(d*(d-a)*(d-b)*(d-c)));
}
else
{
System.out.println("不能构成三角形");
}
}
//6. 输入任意一个整数,求其逆序数并输出,例如,输入1234,输出4321。
public static void work6()
{
Scanner sc = new Scanner(System.in);
String s;
s=sc.next();
String reverse = new StringBuffer(s).reverse().toString();
System.out.println(reverse);
}
//7. 求1+2+3+4+…+n,当和值刚大于10000时,n的值是多少?
public static void work7()
{
long sum=0;
for(int i = 1;i<=10000;i++)
{
sum+=i;
}
System.out.println(sum);
}
//8. 使用级数求的近似值其计算精度你自定)
public static void work8()
{
double sum = 0;
double pi;
for(long i = 1; i <= 100000; i++){
sum += 1.0 / (i * i);
}
pi = Math.sqrt(sum * 6);
System.out.println(pi);
}
//9. 根据x的值,按下式计算y的值,并保留小数点后2位有效数字(实现四舍五入)。
public static void work9()
{
Scanner sc = new Scanner(System.in);
double x,ans,t;
x = sc.nextDouble();
if(x>=1&&x<2)
ans = 3*x-5;
else if(x>=2&&x<4) {
t = Math.cos(Math.toDegrees(x));
ans = 2*t+1;
}
else if(x>=4&&x<6)
ans = Math.sqrt(x*x+1);
else
ans = x*x-4*x+5;
System.out.println(String.format("%.2f",ans));
}
//10. 输出并统计100-1000之间所有的素数。
public static void work10()
{
int j;
for(int i =100;i<=1000;i++)
{
for(j= 2;j<i/2;j++)
{
if(i%j==0)
{
break;
}
}
if(j>=i/2)
{
System.out.println(i);
}
}
}
}
//