2021级-JAVA03 基础语法2--控制语句

7-1 sdut-sel-2 汽车超速罚款(选择结构)


import java.util.Scanner;
public class Main {
    public static void main(String args[]) {
        Scanner cin = new Scanner(System.in);
        int a = cin.nextInt();
        int b = cin.nextInt();
        int c;
        if(a >= b)
            System.out.println("Congratulations, you are within the speed limit!");
        else
        {
            if(b - a <= 20)
                c = 100;
            else if(b - a >= 21 && b - a <= 30)
                c = 270;
            else c = 500;
            System.out.println("You are speeding and your fine is "+c+".");
        }
    }
}

7-2 sdut-sel-4 计算工资数

import java.util.Scanner; 
public class Main {
    public static void main(String[] args) {
		int h,money;
		Scanner input=new Scanner(System.in);
		h=input.nextInt();
		if(h>0&&h<=120)
		{
			money=h*20;
			System.out.println(money);
		}
		else if(h>120)
		{
			money=120*20+(h-120)*40;
			System.out.println(money);
		}else{
            System.out.println(0);
        }
		
	}
}

7-3 sdut-循环-5-百钱买百鸡


import java.util.Scanner;
public class Main 
{
    public static void main(String args[]) 
    {
        //Scanner cin = new Scanner(System.in);
        System.out.println("公鸡 0只,母鸡25只,小鸡75只");
        System.out.println("公鸡 4只,母鸡18只,小鸡78只");
        System.out.println("公鸡 8只,母鸡11只,小鸡81只");
        System.out.println("公鸡12只,母鸡 4只,小鸡84只");
    }
}

7-4 sdut-最大公约数和最小公倍数


import java.util.Scanner;
public class Main
{
    public static void main(String args[])
    {
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext())
        {
            int a = cin.nextInt();
            int b = cin.nextInt();
            System.out.print(gcd(a, b) + " ");
            System.out.println(lcm(a, b));
        }
    }
    public static int gcd(int m, int n)
    {
        return m % n == 0 ? n : gcd(n, m % n);
    }
//ll lcm(ll a, ll b) {return a/gcd(a, b)*b;}
    public static int lcm(int m, int n)
    {
        return m/gcd(m, n)*n;
    }
}
//ll gcd(ll a, ll b) {return b ? gcd(b, a%b) : a; }


7-5 sdut-sel-10 直角坐标系象限判断


import java.util.Scanner;
public class Main
{
    public static void main(String args[])
    {
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext())
        {
            int a = cin.nextInt();
            int b = cin.nextInt();
            if(a > 0 && b > 0) System.out.println(1);
            else if(a > 0 && b < 0) System.out.println(4);
            else if(a < 0 && b > 0) System.out.println(2);
            else System.out.println(3);
        }
    }
    public static int gcd(int m, int n)
    {
        return m % n == 0 ? n : gcd(n, m % n);
    }
//ll lcm(ll a, ll b) {return a/gcd(a, b)*b;}
    public static int lcm(int m, int n)
    {
        return m/gcd(m, n)*n;
    }
}
//ll gcd(ll a, ll b) {return b ? gcd(b, a%b) : a; }


7-6 sdut-sel-while-1 社会主义核心价值观之理解与输出


import java.util.Scanner;
public class Main
{
    public static void main(String args[])
    {
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext())
        {
            int a = cin.nextInt();
            if(a == 1)
                System.out.println("富强、民主、文明、和谐(国家层面)");
            else if(a == 2)
                System.out.println("自由、平等、公正、法治(社会层面)");
            else if(a == 3)
                System.out.println("爱国、敬业、诚信、友善(个人层面)");
            else if(a == 0) break;
            else System.out.println("Input out of range!");
        }
    }
    public static int gcd(int m, int n)
    {
        return m % n == 0 ? n : gcd(n, m % n);
    }
//ll lcm(ll a, ll b) {return a/gcd(a, b)*b;}
    public static int lcm(int m, int n)
    {
        return m/gcd(m, n)*n;
    }
}
//ll gcd(ll a, ll b) {return b ? gcd(b, a%b) : a; }


7-7 sdut-期末考试之排名次

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner reader = new Scanner(System.in);
        int i, n, j, a, b, c, temp;
        n = reader.nextInt();
        int s[] = new int[110];// 这里的数组空间多开,如果开的大小正好等于n,则可能发生数组越界的可能
        for (i = 0; i < n; i++) {
            a = reader.nextInt();
            b = reader.nextInt();
            c = reader.nextInt();
            s[i] = a + b + c;
        }
        for (i = 0; i < n; i++) {
            for (j = i + 1; j < n; j++) {
                if (s[i] < s[j]) {
                    temp = s[i];
                    s[i] = s[j];
                    s[j] = temp;

                }
            }
        }
        for (i = 0; i < n - 1; i++) {
            System.out.println(s[i]);
        }
        System.out.print(s[n - 1]);
    }

}

7-8 sdut-循环-4-统计学生平均成绩与及格人数(II)

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int t = cin.nextInt();
        if(t == 0)
        {
            System.out.println("average=0.0\n" +
                    "count=0");
        }
        else
        {
            int n = t;
            double sum = 0;
            int cnt = 0;
            while(t -- != 0)
            {
                double x = cin.nextDouble();
                sum += x;
                if(x >= 60) cnt ++;
            }
            System.out.printf("average=%.1f\n", sum/n);
            System.out.printf("count=%d\n", cnt);
        }
    }

}

7-9 sdut-九九乘法表

import java.util.*;
 
public class Main {
 
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int n,i,j;
        while(cin.hasNext())
        {
            n = cin.nextInt();
            for(i=1;i<=n;i++)
            {
                for(j=1;j<=i;j++)
                {
                    if(j!=i)
                        System.out.printf("%d*%d=%d ",j,i,i*j);
                    else
                        System.out.printf("%d*%d=%d\n",j,i,i*j);
                }
            }
        }
        cin.close();
    }
}

7-10 sdut-sel-1 R-D先生生日(分支结构)

import java.util.Scanner;
public class Main{
 public static void main(String[] args){
    
     Scanner cin=new Scanner(System.in);
     int m=cin.nextInt();
     int n=cin.nextInt();
     if(m<4||  m==4&&n<6)
         System.out.print("BeforeRD");
     else if(m>4|| m==4&&n>6)
          System.out.print("AfterRD");
         else if(m==4&&n==6)
            System.out.print("Special");
 }  
}

7-11 sdut-顺序结构-1 利用海伦公式求三角形面积,了解世界科学史

import java.math.BigDecimal;
import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext())
        {
//            int[][] a;
//            int b[][];
//            a = new int[8][8];
//            b = new int[8][8];
            double a = cin.nextDouble();
            double b = cin.nextDouble();
            double c = cin.nextDouble();
            if(a <= 0 || b <= 0 || c <= 0)
            {
                System.out.println("Input Error!");
            }
            else
            {
                double Max = Math.max(Math.max(a, b), c);
                double p = (a + b + c) / 2.0;
                if(p <= Max) System.out.println("Input Error!");
                else
                {
                    double s = Math.sqrt(p*(p - a)*(p - b)*(p - c));
                    System.out.printf("%.2f\n", s);
                }
            }
        }
    }
}

7-12 sdut-循环-9 求定积分

import java.util.Scanner;

class Main {
	public static void main(String[] args) {
   Scanner sc =new Scanner(System.in); 
   double a,b,interval,sum;
   int n,i;
   double left;
   while(sc.hasNext())
   {
	   a=sc.nextDouble();
	   b=sc.nextDouble();
	   n=sc.nextInt();
	   interval=(b-a)/n;
	   sum=0;
	   for(i=0,left=a;i<n;i++)
	   {
		   double tmp=(left*left+(left+interval)*(left+interval))*interval/2;
		   /*
		    * 定积分约等于求面积
		    * 左边高:left*left
		    * 右边高:(left+interval)*(left+interval)
		    * 长度:interval
		    * 梯形求面积:记得除以2
		    */
		   sum+=tmp;
		   left+=interval;
	   }
	   System.out.printf("%.3f\n",sum);
   }
   
   sc.close();
   
}
}

7-13 sdut-sel-for-1 RDMP音乐播放器(I)(分支、循环)


import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int num = 0;
		char A = 'A', B = 'B', C = 'C', D = 'D', E = 'E';
		for (int i = 0; i < 5; i++) {
			num = in.nextInt();
			if(num==1) {
				char fg='0';
				fg=A;
				A=B;
				B=C;
				C=D;
				D=E;
				E=fg;
			}
			if(num==2) {
				char fg='0';
				fg=E;
				E=D;
				D=C;
				C=B;
				B=A;
				A=fg;
			}
			if(num==3) {
				char fg='0';
				fg=A;
				A=B;
				B=fg;
			}
		}
		System.out.print(A);
		System.out.print(',');
		System.out.print(B);
		System.out.print(',');
		System.out.print(C);
		System.out.print(',');
		System.out.print(D);
		System.out.print(',');
		System.out.print(E);
	}
}

7-14 sdut-循环-1-求倒数之和

import java.util.Scanner;
public class Main{
    public static void main(String[] args){

        Scanner cin=new Scanner(System.in);
        int n=cin.nextInt();
        double sum=0;
        for(int i=1;i<=n;i++)
        {
            sum+=1.0/i;
        }
        System.out.print(String.format("%.6f",sum));
    }
}

7-15 sdut-冒泡排序中数据交换的次数

import java.util.Scanner;
public class Main{
    public static void main(String[] args){

        Scanner cin=new Scanner(System.in);
        int t = cin.nextInt();
        while(t -- != 0)
        {
            int n = cin.nextInt();
            int [] a = new int[100];
            for(int i = 0; i < n; i ++)
            {
                int x = cin.nextInt();
                a[i] = x;
            }
            int cnt = 0;
            for(int i = 0; i < n - 1; i ++)
            {
                for(int j = 0; j < n - 1 - i; j ++)
                {
                    if(a[j] > a[j + 1])
                    {
                        cnt ++;
                        int tt = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = tt;
                    }
                }
            }
            System.out.println(cnt);
        }
    }
}

7-16 sdut-循环-2-格式化输出N个数

import java.util.Scanner;
public class Main{
    public static void main(String[] args){

        Scanner cin=new Scanner(System.in);
        int n=cin.nextInt();
        for(int i=1;i<=n;i++)
        {
            if(i%10==0)
            {

                System.out.println(i);
            }
            else if(i!=n)
                System.out.print(i+" ");
            else
                System.out.print(i);
        }
    }
}

7-17 sdut-温度转换


import java.util.Scanner;
public class Main {
    public static void main(String args[]) {
        Scanner cin = new Scanner(System.in);
        double a = cin.nextDouble();
        double c = 5.0*(a - 32.0)/9.0;
        System.out.printf("%.2f\n", c);
    }

}

7-18 sdut-顺序结构-2 计算得分


import java.util.Scanner;
public class Main {
    public static void main(String args[]) {
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext())
        {
            int a = cin.nextInt();
            int b = cin.nextInt();
            int c = cin.nextInt();
            System.out.println(a*3 + b*2 + c);
        }
    }

}

7-19 sdut-循环-7-统计正数和负数的个数(II)

import java.util.Scanner;
public class Main{
 public static void main(String[] args){
    
     Scanner cin=new Scanner(System.in);
     int z=0,f=0;
     double sum=0,i=0;
     double n=1.0;
     while(n!=0.0)
     {
         n=cin.nextDouble();
         if(n>0)
             z++;
         if(n<0)
             f++;
         sum+=n;
         i++;
     }
     System.out.println(z);
     System.out.println(f);
  System.out.println((int)sum);
     System.out.print(String.format("%.2f",sum/(i-1)));
   
 }  
}

7-20 sdut-循环-6-统计字符 (II)

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int letters=0,blanks=0,digits=0,others=0;
        int n=sc.nextInt();
        sc.nextLine();

        String line=sc.nextLine();

        for(int i=0;i < n; i ++)
        {
            char ch=line.charAt(i);
            if(ch>='A'&&ch<='Z'||ch>='a'&&ch<='z')
                letters++;
            else if(ch>='0'&&ch<='9')
                digits++;
            else if(ch==' ')
                blanks++;
            else
                others++;
        }

        System.out.println("letters="+letters);
        System.out.println("blanks="+blanks);
        System.out.println("digits="+digits);
        System.out.println("others="+others);
    }
}

7-21 sdut-逆序的N位数

import java.util.Scanner;
public class Main{
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        long l=sc.nextLong();
        long result=0;
        while(l!=0)
        {
            long tmp=l%10; //l的个痊数
            l=l/10; //l去除了个数,留十位向左的所有数字位
            result=result*10+tmp;
        }
        System.out.println(result);
    }
}

7-22 sdut-循环-乘法运算

import java.util.Scanner;
public class Main
{
    public static void main(String args[])
    {
        Scanner cin=new Scanner(System.in);
        int n = cin.nextInt();
        for(int i = 1; i <= n; i ++)
        {
            System.out.printf("%d*%d=%d\n", i, n, i*n);
        }
    }
}

7-23 sdut-循环-3-评委评分(II)

import java.util.Scanner;
public class Main{
    public static void main(String[] args){

        Scanner cin=new Scanner(System.in);
        int n=cin.nextInt();
        double max=-1;
        double min=11;
        double sum=0;
        for(int i=1;i<=n;i++)
        {
            double x=cin.nextDouble();
            if(x>max)
                max=x;
            if(x<min)
                min=x;
            sum+=x;
        }
        System.out.println("最高分"+String.format("%.2f",max));
        System.out.println("最低分"+String.format("%.2f",min));
        System.out.print("平均分"+String.format("%.2f",(sum-max-min)/(double)(n-2)));
    }
}

7-24 sdut-循环-9-画菱形(II)

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int num = 0;
		num = in.nextInt();
		for (int i = 1; i <= (2 * num - 1); i++) {

			for (int j = 1; j <= num - i; j++)
				System.out.print(' ');

			for (int j = 1; j <= (2 * i - 1) & i <= num; j++)
				System.out.print('*');

			for (int j = i - num; j >= 1; j--)
				System.out.print(' ');

			for (int j = 2 * (2 * num - i) - 1; j >= 1 & i > num; j--)
				System.out.print('*');
			System.out.println();
		}
	}
}

7-25 sdut-循环-8-等腰直角三角形(II)

import java.util.Scanner;
public class Main{
    public static void main(String[] args){

        Scanner cin=new Scanner(System.in);
        int n=cin.nextInt();
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=i;j++)
            {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

7-26 sdut-判断素数

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner cin=new Scanner(System.in);
        //while(cin.hasNext())
        {
            int t=cin.nextInt();
            while(t -- != 0)
            {
                int n = cin.nextInt();
                if(n == 1) System.out.println("No");
                else
                {
                    int flag = 1;
                    for(int i = 2; i <= n/i; i ++)
                    {
                        if(n%i == 0)
                        {
                            flag = 0;
                            break;
                        }
                    }
                    if(flag == 1) System.out.println("Yes");
                    else System.out.println("No");
                }
            }
        }
    }
}

7-27 sdut-小I选宾馆

import java.util.Scanner;
public class Main
{
    public static void main(String[] arys)
    {
        int i=0,j=0,n=0,k=0,l=0,o=0,p=0;
        Scanner sc= new Scanner(System.in);
        while(sc.hasNext())
        {
            n=sc.nextInt();
            k=sc.nextInt();
            l=sc.nextInt();
            for(i=2;i<=n;i++)
            {
                o=sc.nextInt();
                p=sc.nextInt();
                if(o>k)
                {
                    k=o;
                    l=p;
                    j=i;
                }
                else if(o==k)
                {
                    if(p>l)
                    {
                        k=o;
                        l=p;
                        j=i;
                    }
                    else
                    {
                        k=k;
                        l=l;
                    }
                }
            }
            System.out.println(j);
            i=2;
        }
    }
}

7-28 一元二次方程(有实根)

import java.math.BigDecimal;
import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner cin = new Scanner(System.in);
        double a = cin.nextDouble();
        double b = cin.nextDouble();
        double c = cin.nextDouble();
        double x1 = ((-b)+Math.sqrt(b*b-4.0*a*c))/(2*a);
        double x2 = ((-b)-Math.sqrt(b*b-4.0*a*c))/(2*a);
        System.out.printf("X1=%.3f\n", x1);
        System.out.printf("X2=%.3f\n", x2);
    }
}

7-29 大小写转换

import java.math.BigDecimal;
import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner cin = new Scanner(System.in);
        String c = cin.nextLine();
        char s = c.charAt(0);
        if(s >= 'a' && s <= 'z')
        {
            System.out.println((char)(s - 32));
        }
        else
        {
            System.out.println((char)(s+32));
        }
    }
}

7-30 极坐标->直角坐标

#include <bits/stdc++.h>

using namespace std;

int main()
{
    char c1, c2, c3;
    double r, o, x, y;
    cin >> c1 >> r >> c2 >> o >> c3;
    x = r * cos(o);
    y = r * sin(o);
    printf("( %.10g, %.10g )", x, y);
    return 0;
}

7-31 输入数据范围的简单判断

import java.math.BigDecimal;
import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner cin = new Scanner(System.in);
        int a = cin.nextInt();
        if(a/100 != 0 && a/1000 == 0)
        {
            int b = a/100;
            int c = a/10%10;
            int d = a%10;
            System.out.print(b);
            System.out.print(b);
            System.out.print(c);
            System.out.print(d);
            System.out.println(d);
        }
    }
}

7-32 计算正五边形的面积和周长

import java.text.DecimalFormat;
import java.util.Scanner;

abstract class shape {// 抽象类

public abstract double getArea();

 public abstract double getPerimeter(); }
class RPentagon extends shape
{
private double a;
    @Override
    public double getArea() {
        // TODO 自动生成的方法存根
        return 5*Math.pow(a,2)/(4*Math.tan(36*Math.PI/180));
    }

    @Override
    public double getPerimeter() {
        // TODO 自动生成的方法存根
        return 5*a;
    }
    public RPentagon(double a1)
    {
        a=a1;
    }
}
public class Main
{
    public static void main(String[]args)
    {
        DecimalFormat df=new DecimalFormat("#.####");
        Scanner in=new Scanner(System.in);
        RPentagon r=new RPentagon(in.nextDouble());
        System.out.println(df.format(r.getArea()));
        System.out.println(df.format(r.getPerimeter()));
        in.close();
    }
}

7-33 整数四则运算

import java.math.BigDecimal;
import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner cin = new Scanner(System.in);
        int a = cin.nextInt();
        int b = cin.nextInt();
        System.out.printf("%d+%d=%d\n", a, b, a + b);
        System.out.printf("%d-%d=%d\n", a, b, a - b);
        System.out.printf("%d*%d=%d\n", a, b, a * b);
        System.out.printf("%d/%d=%d\n", a, b, a / b);
    }
}

7-34 判断三角形类型

import java.util.Scanner;
 
public class Main {
	    public static void main(String[] args){
	    	Scanner in = new Scanner (System.in);
	    	boolean Isosceles=false,RT=false;
	    	double a,b,c;
	    	a = in.nextDouble();
	    	b = in.nextDouble();
	    	c = in.nextDouble();
	    	if(a<1||a>200||b<1||b>200||c<1||c>200) {
	    		System.out.print("Wrong Format");
	    		return;
	    	}
	    	if(a+b<=c||a+c<=b||b+c<=a) {
	    		System.out.print("Not a triangle");
	    		return;
	    	}
	    	if(a==b&&a==c&&b==c) {
	    		System.out.print("Equilateral triangle");
	    		return;
	    	}
	    	if(a==b||a==c||b==c) {
	    		Isosceles = true;
	    	}
	    	if(Math.abs(a*a+b*b-c*c)<0.000001||Math.abs(a*a+c*c-b*b)<0.000001||Math.abs(c*c+b*b-a*a)<0.000001) {
	    		RT = true;
	    	}
	    	
	    	if(Isosceles&&RT) {
	    		System.out.print("Isosceles right-angled triangle");
	    	}else if(Isosceles) {
	    		System.out.print("Isosceles triangle");
	    	}else if(RT) {
	    		System.out.print("Right-angled triangle");
	    	}else {
	    		System.out.print("General triangle");
	    	}
	    	
	    	
        }
}

7-35 点是否在圆内?


import java.util.Scanner;
public class Main {
    public static void main(String args[]) {
        Scanner cin = new Scanner(System.in);
        double a = cin.nextDouble();
        double b = cin.nextDouble();
        if(a*a+b*b <= 100)
            System.out.println(1);
        else System.out.println(0);
    }
}

7-36 判断坐标点所在范围


import java.util.Scanner;
public class Main {
    public static void main(String args[]) {
        Scanner cin = new Scanner(System.in);
        double a = cin.nextDouble();
        double b = cin.nextDouble();
        if(a > 5 || a < -5 || b > 2.5 || b < -2.5)
            System.out.println("Not in the rectangle");
        else System.out.println("In the rectangle");
    }
}

7-37 求最大值


import java.util.Scanner;
public class Main {
    public static void main(String args[]) {
        Scanner cin = new Scanner(System.in);
        int a = cin.nextInt();
        int b = cin.nextInt();
        int c = Math.max(a, b);
        System.out.println(c);
    }
}

7-38 大小写字母转换


import java.util.Scanner;
public class Main
{
    public static void main(String args[])
    {
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext())
        {
            char c = cin.next().charAt(0);
            if(c >= 'A'&&c <= 'Z')
            {
                System.out.println((char)(c + 32));
            }
            else
                System.out.println((char)(c - 32));
        }

    }
}

7-39 十进制转十六进制


import java.util.Scanner;
public class Main
{
    public static void main(String args[])
    {
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        if(n < 0 || n > 15)
        {
            System.out.println("Invalid input");
        }
        else
        {
            if(n >= 0 && n <= 9)
                System.out.println(n);
            else if(n == 10)
                System.out.println("A");
            else if(n == 11)
                System.out.println("B");
            else if(n == 12)
                System.out.println("C");
            else if(n == 13)
                System.out.println("D");
            else if(n == 14)
                System.out.println("E");
            else if(n == 15)
                System.out.println("F");
        }
    }
}

7-40 Java中二进制位运算



import java.util.Scanner; 
public class Main {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		int n=sc.nextInt();
		String c=sc.next();
		int m=sc.nextInt();
        
        switch(c.charAt(0)){
            case '&':
                System.out.printf(n+" "+c+" "+m+" = "+(n&m));
                break;
            case '|':
                System.out.printf(n+" "+c+" "+m+" = "+(n|m));
                break;
            case '^':
                System.out.printf(n+" "+c+" "+m+" = "+(n^m));
                break;
            default:
                System.out.printf("ERROR");
        }
        /*
		if(c.charAt(0)=='&') {
			System.out.printf(n+" "+c+" "+m+" = "+(n&m));
		}else if(c.charAt(0)=='|') {
			System.out.printf(n+" "+c+" "+m+" = "+(n|m));
		}else if(c.charAt(0)=='^') {
			System.out.printf(n+" "+c+" "+m+" = "+(n^m));
		}else {
			System.out.printf("ERROR");
		}
		*/
	}

}

7-41 电话键盘字母数字转换

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s=scanner.nextLine();
        char cstr=s.charAt(0);
        
        //char cstr = scanner.next().charAt(0);
        if (cstr >= 'A' && cstr <= 'C' || cstr >= 'a' && cstr <= 'c') {
            System.out.println('2');
        } else if  (cstr >= 'D' && cstr <= 'F' || cstr >= 'd' && cstr <= 'f' ) {
            System.out.println('3');
        } else if  (cstr >= 'G' && cstr <= 'I' || cstr >= 'g' && cstr <= 'i') {
            System.out.println('4');
        } else if  (cstr >= 'J' && cstr <= 'L' || cstr >= 'j' && cstr <= 'l' ) {
            System.out.println('5');
        } else if  (cstr >= 'M' && cstr <= 'O' || cstr >= 'm' && cstr <= 'o' ) {
            System.out.println('6');
        } else if  (cstr >= 'P' && cstr <= 'S' || cstr >= 'p' && cstr <= 's') {
            System.out.println('7');
        } else if  (cstr >= 'T' && cstr <= 'V' || cstr >= 't' && cstr <= 'v' ) {
            System.out.println('8');
        } else if  (cstr >= 'W' && cstr <= 'Z' || cstr >= 'w' && cstr <= 'z') {
            System.out.println('9');
        } else {
            System.out.println(cstr + " is an invalid input");
        }
    }
}

7-42 五级制成绩


import java.util.Scanner;
public class Main
{
    public static void main(String args[]) {
        Scanner cin = new Scanner(System.in);
        char c = cin.next().charAt(0);
        if(c == 'A')
            System.out.println("[90-100]");
        else if(c == 'B')
            System.out.println("[80-90)");
        else if(c == 'C')
            System.out.println("[70-80)");
        else if(c == 'D')
            System.out.println("[60-70)");
        else if(c == 'E')
            System.out.println("[0-60)");
        else System.out.println("ERROR");
    }
}

7-43 编程题:统计符合条件元素的个数

import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int x=0,y=0;
        for(int i=1;i<=n;i++)
        {
            if(i%3==0)
            {
                if(i%2==0)
                    y++;
                else
                    x++;
            }
        }
        
        System.out.print(x+","+y);
    }
}

7-44 回文数

import java.util.Scanner;

public class Main
{
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        int hw=scan.nextInt();
        int y=0;
        int h=hw;
        while(hw>0)
        {
            y=y*10+hw%10;
            hw=hw/10;
        }
        if(y==h)
        {
            System.out.println("yes");
        }
        else
        {
            System.out.println("no");
        }
    }
    public boolean isPalindrome(int x)
    {
        if(x<0) return false;
        String s = String.valueOf(x); //将数字转为为字符串
        for(int i=0;i<s.length()/2;i++)
        {
            if(s.charAt(i)!=s.charAt(s.length()-i-1))
                return false;
        }
        return true;
    }
}

7-45 一元二次方程


import java.util.Scanner;
public class Main
{
    public static void main(String args[])
    {
        Scanner cin = new Scanner(System.in);
        double a = cin.nextDouble();
        double b = cin.nextDouble();
        double c = cin.nextDouble();
        double p = b * b - 4 * a * c;
        double x1 = -b / (2*a);
        if(x1 == 0)
        {
            x1 = 0;
        }
        if(p > 0)
            System.out.printf("x1=%.6f,x2=%.6f\n", (-b + Math.sqrt(p)) / (2 * a),(-b - Math.sqrt(p)) / (2 * a));
        else if(p == 0)
            System.out.printf("x1=x2=%.6f\n",x1);
        else
        {
            p = -p;
            double y1 = Math.sqrt(p) / (2*a);
            double y2 = -Math.sqrt(p) / (2*a);
            System.out.printf("x1=%.6f+%.6fi\n", x1, y1);
            System.out.printf("x2=%.6f%.6fi\n",x1,y2);
        }
    }
}

7-46 jmu-Java-02基本语法-06-枚举

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Scanner;

enum Grade{//枚举的类型需要定义在类之外
    A("Excellent"),B("Good"),C("Average"),D("Fair"),E("Poor");

    public  String desc;

    Grade(String desc) {
        this.desc = desc;
    }

    public String getDesc(){
        return desc;
    }
}

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int score=sc.nextInt();
            Grade a=getGrade(score);
            System.out.println(a.getDesc());
            printGradeInfo(a);
        }
    }

    public static Grade getGrade(int score){
        Grade ret;
        switch (score/10){
            case 10:
            case 9:
                ret=Grade.A;
                break;
            case 8:
                ret=Grade.B;
                break;
            case 7:
                ret=Grade.C;
                break;
            case 6:
                ret=Grade.D;
                break;
            default:
                ret=Grade.E;
                break;
        }
        return ret;
    }

    public static void printGradeInfo(Grade grade){
        System.out.println("class name="+Grade.class);
        System.out.println("grade value="+grade);
    }
}

7-47 分队列


import java.util.Scanner;
public class Main
{
    public static void main(String args[])
    {
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        for(int i = 1; i <= n; i += 2)
        {
            if(i == 1) System.out.print(i);
            else
            {
                System.out.print(" " + i);
            }
        }
    }
}

7-48 1/(1+2+3+...+n)


import java.util.Scanner;
public class Main
{
    public static void main(String args[])
    {
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        double[] a = new double[100];
        a[0] = 0;
        for(int i = 1; i <= n; i ++)
        {
            a[i] = a[i - 1] + i;
        }
        double ans = 0;
        for(int i = 1; i <= n; i ++)
        {
            ans += 1.0/a[i];
        }
        System.out.printf("%.6f\n", ans);
    }
}

7-49 求出两个正整数之间能被3整除但不能被5整除的所有奇数


import java.util.Scanner;
public class Main
{
    public static void main(String args[])
    {
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        int m = cin.nextInt();
        int cnt = 0;
        for(int i = n; i <= m; i ++)
        {
            if(i%3 == 0 && i%5!=0 && i%2 != 0)
            {
                cnt ++;
                if(cnt%8==0)
                    System.out.println(i);
                else
                    System.out.print(i+",");
            }
        }
    }
}

7-50 求1到正整数n之间的偶数之和


import java.util.Scanner;
public class Main
{
    public static void main(String args[])
    {
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        int sum = 0;
        for(int i = 1; i <= n; i ++)
        {
            if(i%2 == 0) sum += i;
        }
        System.out.println(sum);
    }
}

7-51 求n个数的平均值最大值最小值问题。


import java.util.Scanner;
public class Main
{
    public static void main(String args[])
    {
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        int[] a = new int[100];
        int M = -1, m = 10000;
        double sum = 0;
        for(int i = 1; i <= n; i ++)
        {
            int x = cin.nextInt();
            if(x > M) M = x;
            if(x < m) m = x;
            sum += x;
        }
        System.out.printf("%.2f,%d,%d", sum/n,M,m);
    }
}

7-52 输出100至n之间的所有3位数的水仙花数

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        for(int i = 100; i <= Math.min(999, n); i ++)
        {
            int m = i;
            int a = i % 10, b = i / 10 % 10, c = i / 100;
            if (a * a * a + b * b * b + c * c * c == m)
            {
                System.out.println(i);
            }
        }
    }
}

7-53 特殊回文数

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner read = new Scanner(System.in);
        int sum =read.nextInt();
        //int sum=0;
        for(int i=10000;i<=999999;i++)
        {
            if (i>=10000 && i<=99999)
            {
                //个位
                int gewei5=i % 10;
                // 十位
                int shiwei5=(i % 100) / 10;
                // 百位
                int baiwei5=(i/100)%10;
                // 千位
                int qianwei5=(i / 1000) % 10;
                // 万位
                int wanwei5=i / 10000;
                //输出这样的五位数
                if (gewei5== wanwei5 && ((shiwei5) == (qianwei5)  &&  (gewei5+shiwei5+baiwei5+qianwei5+wanwei5==sum)))
                {
                    System.out.println(i);
                }
            }
            else
            {
                //个位
                int gewei6=i % 10;
                // 十位
                int shiwei6=(i % 100) / 10;
                // 百位
                int baiwei6=(i%1000)/100;
                // 千位
                int qianwei6=(i/1000)%10;
                // 万位
                int wanwei6=(i / 10000) % 10;
                //十万位
                int shiwanwei6=i / 100000;

                if ((gewei6 == shiwanwei6 && ((shiwei6) == (wanwei6)) && (baiwei6)==qianwei6)   &&   (gewei6+shiwei6+baiwei6+qianwei6+wanwei6+shiwanwei6==sum)) {
                    //if(){
                    System.out.println(i);
                    //}

                }
            }
        }
    }
}

7-54 666

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        int cnt = 6;
        long ans = 0;
        for(int i = 1; i <= n; i ++)
        {
            ans += cnt;
            cnt += 6*Math.pow(10, i);
        }
        System.out.println(ans);
    }
}

7-55 上三角数字三角形

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        int num = 1;
        for(int i = 1; i <= n; i ++)
        {
            for(int j = i; j <= n; j ++)
            {
                System.out.printf("%4d", num ++);
            }
            System.out.println("");
        }
    }
}

7-56 又来一个上三角数字三角形

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a=sc.nextInt();
        int n=1;
        for (int i=a;i>0;i--){
            int d=a;
            int m=n;
            for (int j=0;j<i;j++){
                System.out.printf("%4d",m);
                m=m+d--;
                if (j==i-1) System.out.println();
            }
            n++;
        }
    }
}

7-57 空心字母金字塔

import java.util.Scanner;
public class Main
{
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
        String s=sc.next();
        for (int i=65;i<=s.charAt(0);i++)
        {
            for (int j=1;j<105-i;j++)
            {
                System.out.print(" ");
            }
            System.out.print((char)i);
            if (i!=s.charAt(0))
            {
                for (int j=0;j<i-65;j++)
                {
                    System.out.print(" ");
                }
                for (int j=0;j<i-66;j++)
                {
                    System.out.print(" ");
                }
            }
            if (i==s.charAt(0))
            {
                for (int m=1;m<2*(i-65);m++)
                {
                    System.out.print((char)i);
                }
            }
            if (i!=65) System.out.print((char)i);
            System.out.println();
        }
    }
}

  • 6
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 7-1 jmu-java-02基本语法-01是指教学内容中的一个章节或课时,主要讲解Java编程语言的基本语法。其中包括数据类型、变量、运算符、控制语句等内容。学习这些基本语法是Java编程的基础,也是后续学习更高级的Java知识的必要前提。 ### 回答2: 7-1 jmu-java-02基本语法-01是一门关于Java编程语言的基础语法课程。在这门课程中,我们将学习Java语言的重要基础知识,包括Java的语法结构、数据类型、变量、运算符、流程控制、数组、方法和类等知识点。 Java语言是一种面向对象编程语言,它具有简单、安全、可移植、网络化等特点,非常适合用来开发网络应用程序、桌面应用程序和移动应用程序等。 在这门课程中,我们将学习Java的基本语法结构,如Java源程序的基本结构、Java标识符、Java关键字、Java注释等。我们还将深入了解Java的基本数据类型、运算符、流程控制语句、数组和方法等。此外,我们还将学习Java的面向对象编程知识,包括类和对象的概念、封装、继承和多态等。 通过学习本课程,我们将能够掌握Java编程的基础语法,能够编写简单的Java程序,如控制台应用程序、图形用户界面程序等。同时,我们还将为深入学习Java各种高级应用程序打下坚实的基础。这将有助于我们更好地理解Java编程语言并更好地应用它来开发各种类型的应用程序。 总之,7-1 jmu-java-02基本语法-01是一门重要的Java编程基础知识课程,通过学习本课程,我们将深入了解Java编程语言的基本语法和面向对象编程知识,从而更好地应用Java编程语言进行开发各种类型的应用程序。 ### 回答3: 7-1 jmu-java-02基本语法-01是Java课程中的一个章节,主要是教授Java的基本语法知识,包括Java程序的基本结构、数据类型、运算符、控制语句、数组、方法等方面的知识。 Java程序的基本结构分为包声明、导入类、类声明和主方法四部分。包声明用来声明代码所属的包,导入类用来引入需要使用的类,在类声明部分编写Java代码。主方法则是程序的入口,程序从这里开始执行。 Java数据类型包括基本数据类型和引用数据类型。基本数据类型包括整型、浮点型、字符型和布尔型四种,每种类型具有不同的取值范围和精度。引用数据类型包括类、数组和接口三种,这些类型都是由Java程序员编写的。 Java运算符包括算术运算符、比较运算符、逻辑运算符和位运算符。算术运算符用于对数字进行基本运算,比较运算符用于比较两个值的大小,逻辑运算符用于连接多个逻辑判断语句,位运算符用于对二进制数进行位运算。 Java控制语句包括if语句、switch语句、for语句、while语句和do-while语句。这些语句用于控制程序的运行流程,根据不同的条件执行不同的代码块。 Java数组是一种存储多个相同类型数据的数据结构,可以通过下标访问数组中的元素。Java方法是一种封装代码的方式,可以将代码块封装成一个方法,并通过方法名调用这个方法,提高代码的可读性和重用性。 综上所述,7-1 jmu-java-02基本语法-01是Java学习的重要部分之一,通过学习这个章节可以掌握Java的基本语法知识,为深入学习Java编程打下基础

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值