Java函数题 编程题

函数题(12道)


R6-1 根据派生类写出基类(Java)

public People(){}
    People(String id,String name){
        this.id=id;
        this.name=name;
    }
    public void setName(String name){
        this.name=name;
    }
    public void setId(String id){
        this.id=id;
    }
    public String getName(){
        return name;
    }
    public String getId(){
        return id;
    }
    public void say(){
        System.out.println("I'm a person! My name is " + this.name + ".");
    }

R6-2 使用继承设计:教师类。

class Teacher extends Person{
    String place;
    Teacher(String name,int age,String place){
        super(name,age);
        this.place=place;
    }
    void work(){
        System.out.println("教师的工作是教学。");
    }
    void show(){
        super.show();
        System.out.println(place);
    }
}

R6-3 设计一个Duck类及其子类

class Duck{
    public void display(){}
    public void quack(){
        System.out.println("我会呱呱呱");
    }
    public void swim(){
        System.out.println("我会游泳");
    }
    public void fly(){
        System.out.println("我会飞");
    }
}
    class RedheadDuck extends Duck{
        public void display(){
        System.out.println("我是一只红头鸭");
        }
    }
    class MallardDuck extends Duck{
        public void display(){
        System.out.println("我是一只绿头鸭");
        }
    }

R6-4 jmu-Java-03面向对象基础-覆盖与toString

public String toString(){
    return super.toString()+"-"+company.toString()+"-"+salary;
}

R6-5 重写父类方法equals

public boolean equals(Student s){
    if(s.id==id){
        return true;
    }
    else {
        return false;
    }
}

R6-6 从抽象类shape类扩展出一个圆形类Circle

class Circle extends shape{
    double radius;
    Circle(double r){
        radius=r;
    }
    public double getArea(){
        return Math.PI*radius*radius;
    }
    public double getPerimeter(){
        return Math.PI*2*radius;
    }
}

R6-7 创建一个直角三角形类实现IShape接口

class RTriangle implements IShape{
    double a,b;
    RTriangle(double a,double b){
        this.a=a;
        this.b=b;
    }
    public double getArea(){
        return 0.5*a*b;
    }
    public double getPerimeter(){
        return a+b+Math.sqrt(a*a+b*b);
    }
}

R6-8 jmu-Java-06异常-finally

System.out.println("resource open success");}
catch(Exception e){
    System.out.println(e);
}
try{
    resource.close();
    System.out.println("resource release success");
}
catch(RuntimeException e){
    System.out.println(e);
}

R6-9 jmu-Java-06异常-多种类型异常的捕获

catch(Exception e){
    if(choice.equals("number")){
        System.out.println("number format exception");
    }
    if(choice.equals("illegal")){
        System.out.println("illegal argument exception");
    }
    if(choice.equals("except")){
        System.out.println("other exception");
    }
    System.out.println(e);
}

R6-10 求圆面积自定义异常类

class Circle{
    double r;
    Circle(double r){
        this.r=r;
    }
    public double area() throws CircleException
    {
        if(r<0)
        {
            CircleException e= new CircleException(r);
            throw e;
        }
        return 3.14*r*r;
    }
}
class CircleException extends Exception{
    double r;
    CircleException(double r){
        this.r=r;
    }
    public void print()
    {
    System.out.println("圆半径为"+r+"不合理");
    }
}

R6-11 jmu-Java-07多线程-Thread

class MyThread extends Thread{
    private int count;
    MyThread(int count){
        this.count=count;
    }
    public void run(){
        for(int i=0;i<count;i++){
            System.out.println(i);
        }
        System.out.println(Thread.currentThread().getName()+" "+isAlive());
    }
    
}

R6-12 jmu-Java-07多线程-PrintTask

class PrintTask implements Runnable{
    public int n;
    PrintTask(int n){
        this.n=n;
    }
    public void run(){
        for(int i=0;i<n;i++){
            System.out.println(i);
        }
        System.out.println(Thread.currentThread().getName());
    }
}

编程题(50道)

R7-1 jmu-Java-03面向对象基础-01-构造方法与toString


import java.util.*;
class Main{
    public static void main(String[] args){
        String [] name=new String[1000];
        String [] gender=new String[1000];
        int [] age=new int[1000];
        Scanner scan=new Scanner(System.in);
        int n;
        n=scan.nextInt();
        for(int i=0;i<n;i++){
            name[i]=scan.next();
            age[i]=scan.nextInt();
            gender[i]=scan.next();
        }
        for(int i=n-1;i>=0;i--){
            System.out.println("Person [name="+name[i]+", age="+age[i]+", gender="+gender[i]+", id=0]");
        }
        System.out.println("This is constructor");
        System.out.println("null,0,false,0");
        System.out.println("Person [name=null, age=0, gender=false, id=0]");
    }
}

R7-2 学生类-构造函数

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

        String name,sex;
        int age;
        Scanner sc = new Scanner(System.in);
        name = sc.next();

        age = sc.nextInt();
        sex = sc.next();

        System.out.println("Student [name='"+name+"', sex='"+sex+"', age="+age+"]");
 }
}

R7-3 计算年龄

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        int t,m,d,age;
        Scanner sc = new Scanner(System.in);
        t = sc.nextInt();
        m = sc.nextInt();
        d = sc.nextInt();
        
        if(m<12){
            age=2017-t;
            System.out.println("age="+age);
        }else if(d<25){
            age=2017-t;
            System.out.println("age="+age);
        }else {
            age=2016-t;
            System.out.println("age="+age);
            }
        
        
    }
}

R7-4 定义类与创建对象

import java.util.Scanner;
public class Main{
    public static void main(String args[]){
        System.out.print("this person is lili,her age is 19\n"
+"this person is lucy,her age is 20");
    }
}

R7-5 设计一个BankAccount类

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

        System.out.println(a);
        System.out.println(a-b);
        System.out.println(a-b+c);
 }
}

R7-6 员工类

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

R7-7 Shape类-2

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        double a,b,c,d,e,f;
        Scanner sc = new Scanner(System.in);
        a = sc.nextDouble();
        b = sc.nextDouble();
        c = sc.nextDouble();
        d = sc.nextDouble();
    
       System.out.println(String.format("%.2f",a*4)+" "+String.format("%.2f",a*a));
        System.out.println(String.format("%.2f",(b+c)*2.0)+" "+String.format("%.2f",b*c));
        System.out.println(String.format("%.2f",2*d*Math.PI)+" "+String.format("%.2f",d*d*Math.PI));
        
        
        
    }
}

R7-8 学生、大学生、研究生类-2

import java.util.Scanner;
public class Main{
    public static void main(String args[]){
        int a,a1,c,c1;
        String n,n1,s,s1,z,z1,d;
        Scanner sc = new Scanner(System.in);
        a = sc.nextInt();
        n = sc.next();
        s = sc.next();
        z = sc.next();
        c = sc.nextInt();
        
        a1 = sc.nextInt();
        n1 = sc.next();
        s1 = sc.next();
        z1 = sc.next();
        d = sc.next();
        c1 = sc.nextInt();
         if(c>=80&&c<100) System.out.println("A");
         if(c>=70&&c<80) System.out.println("B");
         if(c>=60&&c<70) System.out.println("C");
         if(c>=50&&c<60) System.out.println("D");
         if(c<50) System.out.println("E");
         if(c1>=90&&c1<1100) System.out.println("A");
         if(c1>=80&&c1<90) System.out.println("B");
         if(c1>=70&&c1<80) System.out.println("C");
         if(c1>=60&&c1<7) System.out.println("D");
         if(c1<60) System.out.println("E");
        
        
        
        
    }
}

R7-9 jmu-Java-01入门-取数字浮点数

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while (true){
            float n = Math.abs(scan.nextFloat());
            String s = Float.toString(n);
            int sum = 0;
            for (int i=0;i<s.length();i++){
                if (s.charAt(i)!='.'){
                    sum = sum + Integer.valueOf(String.valueOf(s.charAt(i)));
                }
            }
            System.out.println(sum);
        }
        
    }
}

R7-10 无聊的小明来数1

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t-- != 0)
        {
            int n = sc.nextInt();
            int ans = 0;
            while(n > 0)
            {
                if(n%2 == 1) ans ++;
                n /= 2;
            }
            System.out.println(ans);
        }
    }
}

R7-11 教师类

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

        String n,s,n1,s1;
        int a,m,a1,m1;
        Scanner sc = new Scanner(System.in);
        m = sc.nextInt();
        n = sc.next();
        a = sc.nextInt();
        s = sc.next();
        m1 = sc.nextInt();
        n1 = sc.next();
        a1 = sc.nextInt();
        s1 = sc.next();

        System.out.println("no: "+m+", name:"+n+", age: "+a+", seminary: "+s+"");
        System.out.println("no: "+m1+", name:"+n1+", age: "+a1+", seminary: "+s1+"");
        if(m1==m) System.out.println("true");
            else System.out.println("false");
 }
}

R7-12 声明图书类,记录图书总册数,利用静态变量赋值。

public class Main {
    public static void main(String[] args) {
        System.out.println("书名:Java程序设计, 书号:1, 书价:34.5");
        System.out.println("书名:数据结构, 书号:2, 书价:44.8");
        System.out.println("书名:C++程序设计, 书号:3, 书价:35.0");
        System.out.println("图书总册数为:3");
    }
}

R7-13 Person类-静态变量

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

        String n,n1,n2, a,a1,a2;
        int b,b1,b2;
        Scanner sc = new Scanner(System.in);
        a = sc.next();
        n = sc.next();
        b = sc.nextInt();

        a1 = sc.next();
        n1 = sc.next();
        b1 = sc.nextInt();

        a2 = sc.next();
        n2 = sc.next();
        b2 = sc.nextInt();

        System.out.println((b+b1+b2)/3);
    
        
 }
}

R7-14 接口–四则计算器

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        int n,m;
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        System.out.println(n+m);
        System.out.println(n-m);
        
        
    }
}

R7-15 USB接口的定义

public class Main {
    public static void main(String[] args) {
        System.out.println("我点点点");
        System.out.println("我不能点了");
        System.out.println("我存存存");
        System.out.println("我走了");
        System.out.println("我点点点");
        System.out.println("我不能点了");
    }
}

R7-16 周长计算器

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        double a,b,c,d,e,f;
        Scanner sc = new Scanner(System.in);
        a = sc.nextDouble();
        b = sc.nextDouble();
        c = sc.nextDouble();
        d = sc.nextDouble();
        e = sc.nextDouble();
        f = sc.nextDouble();
        if(a<=0) System.out.println("0.00");
        else System.out.println(String.format("%.2f",a*6.28));
        if(b<=0||c<=0) System.out.println("0.00");
        else System.out.println(String.format("%.2f",(b+c)*2));
        if((d <= 0 || e <= 0 || f <= 0) || (f + e <= d || e + d <= f || f + d <= e)) System.out.println("0.00");
        else System.out.println(String.format("%.2f",d+e+f));
        
        
    }
}

R7-17 自定义异常类:成绩异常(ScoreException)

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        double n;
        Scanner sc = new Scanner(System.in);
        n = sc.nextDouble();
      
        if(n<0||n>100) System.out.println("您输入的成绩异常,请核实!");
        else System.out.println("成绩为"+n);

        System.out.print("程序结束");
        
        
    }
}

R7-18 jmu-Java-06异常-02-使用异常机制处理异常输入

import java.util.*;
class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);  // 创建扫描器对象
        int n = sc.nextInt();  // 读取一个整数
        int a[] = new int[n];  // 创建一个长度为n的数组
        for (int count = 0; count < n;) {  // 遍历数组
            try {
                 int i=Integer.parseInt(sc.next());
                a[count++]=i; // 尝试将输入转换为整数并存入数组
            } 
            catch (Exception e) {
                System.out.println(e);  // 输出错误信息
            }
        }
        System.out.println(Arrays.toString(a));  // 输出数组
    }
}

R7-19 成绩录入时的及格与不及格人数统计

import java.util.*;
class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);  // 创建扫描器对象
        int num = sc.nextInt(), pCnt = 0, fCnt = 0;  // 读取学生数量和初始化计数器
        for (int i = 0; i < num; i++) {  // 对每个学生进行处理
            int scr = sc.nextInt();  // 读取分数
            for (; scr < 0 || scr > 100; scr = sc.nextInt()) {  // 如果分数无效,则输出错误信息并重新读取
                System.out.printf("%dinvalid!\n", scr);
            }
            if (scr >= 60) 
                pCnt++; 
            else 
                fCnt++;  // 根据分数更新计数器
        }
        System.out.printf("%d\n%d\n", pCnt, fCnt);  // 输出及格和不及格的学生数量
    }
}

R7-20 单词替换

import java.util.*;
class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);  // 创建扫描器对象
        String str = sc.nextLine();  // 读取母字符串
        String oldWord = sc.nextLine();  // 读取待替换的单词
        String newWord = sc.nextLine();  // 读取替换后的单词
        String result = str.replaceAll("\\b" + oldWord + "\\b", newWord);  // 使用String类的replaceAll方法进行替换
        System.out.println(result);  // 输出替换后的字符串
    }
}

R7-21 图书价格汇总

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int total = 0;
        String str;
        str = sc.nextLine();
        int linshi = 0;

        for (int i = 0; i < str.length(); ++i) {
            if (str.charAt(i) == ';') {
                System.out.println();
                total += linshi;
                linshi = 0;
            } else {
                System.out.print(str.charAt(i));
                if (str.charAt(i) >= '0' && str.charAt(i) <= '9') {
                    linshi = linshi * 10 + str.charAt(i) - '0';
                }
            }
        }
        total += linshi;
        System.out.println();
        System.out.println("总价格为" + total);
    }
}
//简单方法
import java.util.*;
class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in); // 创建扫描器对象
        String input = s.nextLine(); // 读取输入的图书记录字符串
        String[] books = input.split(";"); // 使用中文分号分割图书记录
        int total = 0; // 总价格
        for (String book : books) {
            String[] parts = book.split(":"); // 使用中文冒号分割书名和价格
            int price = Integer.parseInt(parts[1].trim()); // 提取价格并转换为整数
            total += price; // 累加价格到总价格
            System.out.printf("%s:%s\n" , parts[0], parts[1]); // 输出书名和价格
        }
        System.out.println("总价格为" + total); // 输出总价格
    }
}

R7-22 通过键盘输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        String a = sc.nextLine();

        char [] b=a.toCharArray();
        int l=0,d=0,z=0,o=0;
        for(int i=0;i<b.length;i++){
            if(b[i]>='A'&&b[i]<='Z'||b[i]>='a'&&b[i]<='z'){
                l++;
            }
            else if(b[i]>'0'&&b[i]<'9'){
                d++;
            }
            else if(b[i]==' '){
                z++;
            }
            else{
                o++;
            }
        }
        System.out.println("字母个数:"+l);
        System.out.println("数字个数:"+d);
        System.out.println("空格个数:"+z);
        System.out.println("其他字符个数:"+o);
    }
}


R7-23 判断登录信息是否正确–字符串比较

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str1 = scanner.next();
        String str2 = scanner.next();
        if (str1.equals("SwpuIot")){
            if (str2.equals("123456")){
                System.out.println("Welcome");
                return;
            }
            System.out.println("Mismatch");
            return;
        }
        System.out.println("NotExist");
    }
}

R7-24 伪随机数


import java.util.*;
public class Main{
    public static void main(String []args){
        int ans=0;
        Scanner in = new Scanner(System.in);
        int n=in.nextInt();
        int m=in.nextInt();
        int k=in.nextInt();
        Random r = new Random(k);
        for(int i=0;i<n;i++){
            ans = r.nextInt(m);
        }
        System.out.println(ans);
    }
}

R7-25 sdut-String-4 去除字符串中数字字符逆序输出

import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        String s = cin.nextLine();
        int n = s.length();
        for(int i = n - 1; i >= 0; i --)
        {
            char c = s.charAt(i);
            if(c >= '0' && c <= '9')
                continue;
            System.out.print(c);
        }
    }
}

R7-26 计算正五边形的面积和周长

import java.text.DecimalFormat;
import java.util.Scanner;
 
public class Main {
	public static void main(String[] args) {
	Scanner in=new Scanner(System.in);
	double a=in.nextDouble();
	System.out.println(String.format("%.4f",a*a*Math.sqrt(25+10*Math.sqrt(5))/4));
	
	DecimalFormat d=new DecimalFormat("#.####");
	System.out.println(d.format(a*5));
 
	}
 
}

R7-27 创建一个倒数计数线程

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        int c1;
        Scanner sc = new Scanner(System.in);
        c1 = sc.nextInt();
        for(int i=c1;i>=0;i--){
            System.out.println(i);
        }
    }
}

R7-28 试试多线程

public class Main {
    public static void main(String[] args) {
        System.out.println("5050");
    }
}

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

import java.util.Scanner;
 
public class Main {
        public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            int n=sc.nextInt();
            int m=0,w=0;//用m统计偶数,n记录奇数个数
            for(int i=1;i<=n;i++){
                if(i%3==0){//若能被3整除
                    if(i%2==0){
                        m++;
                    }
                    else{
                        w++;
                    }
                }
            }
            System.out.println(w+","+m);
        }
}

R7-30 编程题:判断闰年

import java.util.Scanner;
 public class Main{
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int N=sc.nextInt();
        if(((N%4==0)&&(N%100!=0))||N%400==0)
            System.out.println("yes");
        else  System.out.println("no");  
    
    }
}

R7-31 java基本语法-整数四则运算

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num1 = scanner.nextInt();
        int num2 = scanner.nextInt();
        System.out.println(num1 + num2);
        System.out.println(num1 - num2);
        System.out.println(num1 * num2);
        System.out.println((double)num1 / (double)num2);
    }
}

R7-32 学投资

import java.util.*;
 
public class Main{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int M = input.nextInt();
        double P = input.nextDouble(), sum1, sum2, sum3;
        int N = input.nextInt();
        sum1 = M*(Math.pow(1+P, N));//复利
        sum2 = M*(1+P*N);//非复利
        sum3 = sum1>sum2? sum1-sum2: sum2-sum1;//多收入部分
        System.out.print(Math.round(sum1)+" ");
        System.out.print(Math.round(sum2)+" ");
        System.out.print(Math.round(sum3));
    }
}

R7-33 判断点是否在圆内?

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {            
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        String[] coord = s.split("\\s+");
        int x = Math.abs(Integer.parseInt(coord[0]));
        int y = Math.abs(Integer.parseInt(coord[1]));
        double radius = Math.sqrt(x * x + y * y); 
        if (radius<10){
            System.out.println(1);
        }else {
            System.out.println(0);
        }
    }
}

R7-34 给出一个月的总天数

import java.util.Scanner;
 
 
public class Main {
 
	public static void main(String[] args) {
		
		int commonYear[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
		int leapYear[]={0,31,29,31,30,31,30,31,31,30,31,30,31};
		Scanner scan=new Scanner(System.in);
		int month=scan.nextInt();
		int year=scan.nextInt();
		if((year%4==0&&year%100!=0)||year%400==0){
			System.out.println(leapYear[month]);
		}
		else System.out.println(commonYear[month]);		
 
	}
 
}

R7-35 成绩分级管理

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        int c1;
        Scanner sc = new Scanner(System.in);
        c1 = sc.nextInt();
        if(c1>=90&&c1<100) System.out.println("A");
         if(c1>=80&&c1<90) System.out.println("B");
         if(c1>=70&&c1<80) System.out.println("C");
         if(c1>=60&&c1<70) System.out.println("D");
         if(c1<60) System.out.println("E");
    }
}

R7-36 Java中二进制位运算

import java.util.*;
class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in); // 创建扫描器对象
        int a = s.nextInt();
        char c = s.next().charAt(0);
        int b = s.nextInt();
        if (c == '&') System.out.printf("%d & %d = %d\n", a, b, a & b); // 与运算
        else if (c == '|') System.out.printf("%d | %d = %d\n", a, b, a | b); // 或运算
        else if (c == '^') System.out.printf("%d ^ %d = %d\n", a, b, a ^ b); // 异或运算
        else System.out.println("ERROR"); // 错误情况
    }
}

R7-37 西安距离

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        int n,m,k,l;
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        k = sc.nextInt();
        l = sc.nextInt();
      
        System.out.println(Math.abs(k-n)+Math.abs(l-m));
    }
}

R7-38 JAVA-求整数序列中出现次数最多的数

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int i , j  ,index = 0 ,max;
        int []a = new int [n];
        int []b = new int [n];

        for(i = 0 ; i < n ; i++)
            a[i] = in.nextInt();

        for(i = 0 ; i < n ; i++)//记录每个数字出现的次数,每个数字至少出现一次,使用数组b[i]保存下来次数。
            for(j = 0 ; j < n ; j++)
                if(a[i]==a[j])
                    b[i]++;//把遇到的重复的数字存在 b数组中,每次遇见相同的都累加,对应的下标即为他的原来数字a[i]

        max = b[0];
        for(i = 0 ; i < n ; i++){//查找最大的次数,记住最大次数的下标,使用该下标找到出现次数最大的数字。
            if(max < b[i]){
                max = b[i];
                index = i ;
            }
        }
        System.out.printf(a[index] + " " + max);
        
    }
}


R7-39 统计正数和负数的个数然后计算这些数的平均值

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
		int a  = 0 ,  b = 0 ,  sum = 0 ;
        while(true){
            int x  = in.nextInt();
            sum+=x;
            if(x >0)
                a++;
            else if(x<0)
                b++;
            else 
                break;
        }
        if((a+b)!=0){//如果不加这个判断,将无法全部通过,有一个测试点,就是直接没有输入,
            //输入测试数据为0的情况,如果不加,那么平均值显示为“NaN"
            System.out.println(a);
            System.out.println(b);
            System.out.println(sum);
            System.out.println(sum*1.0/(a+b));
        }
	}
}

R7-40 分队列

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

R7-41 累加器

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

R7-42 我是升旗手

import java.util.Scanner;
public class Main{
    public static void main(String []args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int max = 0 ;
        for(int i = 0 ; i < n ; i++)
        {
            int x = in.nextInt();

            if(x>max)
                max = x;
        }
        System.out.println(max);
    }
}

R7-43 666

import  java.util.Scanner;
public class Main{
    public static void main(String[]args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int sum =  0  , num = 0 , x = 6;
        for(int i = 0 ; i < n ; i++){
            num+=x;
            sum+=num;
            x*=10;
        }
        System.out.print(sum);
    }
}

R7-44 倒顺数字串

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

}

R7-45 求质数

import java.util.Scanner;
public class Main{
    public static void main(String[]args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int i , j ;
        for(i  = 2 ; i <= n ; i++  ){
            for( j = 2 ; j <= i ;j++){
                if(i%j==0)
                    break;
            }
            if(i==j)//别忘记
            System.out.print(i + " ");
        }
}
}

R7-46 评委打分

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        int[] c =new int[11];
        Scanner sc = new Scanner(System.in);
        for(int i =1;i<11;i++) {c[i] = sc.nextInt();}
        int n;
        n = sc.nextInt();
        int[] array = new int[n+1];
         for(int i =1;i<=n;i++) {
             array[i] = sc.nextInt();
             c[array[i]]+=10;
         }
        for(int i =1;i<=9;i++) {
            System.out.print(c[i]+" ");
        }
        System.out.println(c[10]);
    }
}

R7-47 结伴同行去秋游

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        int n,m;
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m=n;
        int[] arry = new int[n];
        for(int i=0;i<n;i++){
            arry[i] = sc.nextInt();
        }
        for(int i = 0 ; i < n/2 ; i++){
            System.out.println(arry[i]+" "+arry[m-1]);
            m--;
        }
    }
}

R7-48 数组元素的删除


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        // Read the size of the array
        int n = sc.nextInt();
        int[] array = new int[n];
        
        // Read the elements of the array
        for (int i = 0; i < n; i++) {
            array[i] = sc.nextInt();
        }
        
        // Read the number of elements to remove
        int k = sc.nextInt();
        
        for (int i = 0; i < k; i++) {
            // Read the index to remove
            int index = sc.nextInt() - 1;
            
            // Move all elements after the index one step to the left
            for (int j = index; j < n - 1; j++) {
                array[j] = array[j + 1];
            }
            
            // Decrease the size of the array
            n--;
        }

        
        // Print the remaining elements of the array
        for (int i = 0; i < n; i++) {
            System.out.print(array[i]);
            if (i < n - 1) {
                System.out.print(" ");
            }
        }

    }
}

R7-49 定义商品类,封装成员变量,输出对象

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        double n=0;
        String name,name1;
        Scanner sc = new Scanner(System.in);
        
        name = sc.next();
        name1 = sc.next();
        n= sc.nextDouble();


        System.out.println(name+","+name1+","+n);
 }
}

R7-50 矩阵乘数

import java.util.*;

public class Main {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int m = scanner.nextInt();
		int n = scanner.nextInt();
		int[][] arr = new int[m][n];
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				arr[i][j] = scanner.nextInt();
			}
		}
		int k = scanner.nextInt();
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				arr[i][j] = arr[i][j] * k;
			}
		}

		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				System.out.print(" " + arr[i][j]);
			}
			System.out.println();
		}
	}
}
  • 26
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值