【无标题】题题题

7-1 各类字符数

import java.util.Scanner;

public class Main{

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

String s = scan.nextLine();

int big = 0;

int small = 0;

int nothing = 0;

for(int i = 0; i < s.length(); i++){

if(s.charAt(i) >= 'A' && s.charAt(i) <= 'Z'){

big++;

}else if(s.charAt(i) >= 'a' && s.charAt(i) <= 'z'){

small++;

}

}

System.out.println(big);

System.out.println(small);

System.out.println(s.length() - big - small);

}

}

-2 编程题:判断闰年

import java.util.*;

public class Main{

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

int year = scan.nextInt();

if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0){

System.out.println("yes");

}else{

System.out.println("no");

}

}

}

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

import java.util.Scanner;

public class Main{

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

int n = scan.nextInt();

int oddNumber = 0;

int evenNumber = 0;

for(int i = 1; i <= n;i++){

if(i % 3 ==0){

if(i % 2 ==0){

evenNumber++;

}else if(i % 2 != 0){

oddNumber++;

}

}

}

System.out.printf("%d,%d",oddNumber,evenNumber);

}

}

7-5 分解质因数

import java.util.Scanner;

public class Main{

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

int m = scan.nextInt();

int n = scan.nextInt();

for(int i = m;i <= n;i++){

qiu(i);

        }

}

    

    

    

public static void qiu(int temp){

System.out.print(temp + "=");

for(int i = 2;i <= Math.sqrt(temp);i++){

if(temp % i == 0){

System.out.print(i + "*");

temp /= i;

i--;

}

}

System.out.println(temp);

}

}

7-6 统计最大数出现次数

import java.util.Scanner;

public class Main{

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int a;

        int max=-1;

        int count=1;

        do {

            a=sc.nextInt();

            if (max<a) {

                max = a;

                count=1;

            }

            else if (max==a)count++;

        }while (a!=0);

            System.out.println("The largest number is "+max);

            System.out.println("The occurrence count of the largest number is "+count);

    }

}

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

import java.util.Scanner;

public class Main{

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

String str = scan.nextLine();

//String.toCharArray 方法,作用:将字符串转换为字符数组。

char[] chars = str.toCharArray();

int letter=0,num=0,blank=0,other=0;

for(int i=0;i<chars.length;i++) {

if((chars[i]>='A' && chars[i]<='Z') || (chars[i]>='a' && chars[i]<='z')){

letter++;

}else if(chars[i]>='0' && chars[i]<='9') {

num++;

}else if(chars[i]==' ' ) {

blank++;

}else {

other++;

}

}

System.out.println("字母个数:"+letter);

System.out.println("数字个数:"+num);

System.out.println("空格个数:"+blank);

System.out.println("其他字符个数:"+other);

}

}

7-8 sdust-Java-字符串集合求并集

import java.util.*;

public class Main{

    public static void main(String[] args){   //题目大意:创建两个集合,每个集合都加入五个不同的字符串

        Scanner in = new Scanner(System.in);

        String str = in.nextLine();

        StringBuffer ss = new StringBuffer();  //这里用到StringBuffer,由于方法区的内存问题,一般情况不要直接用String

        TreeSet<String> ts = new TreeSet<>();  //否则会出现内存溢出问题,

        int cnt = 0;

        Set<String> s1 = new HashSet<>();  //第一个集合

        Set<String> s2 = new HashSet<>();  //第二个集合

        for(int i = 0; i < str.length(); i ++){   //也可以不用StringBuffer,用slipt来优化也行,这样不用反复转换

            char c = str.charAt(i);               

            if(c != ' '){

                ss.append(c);             //StringBuffer拼接字符串

            }else{

                if(cnt < 5){

                    if(s1.contains(ss.toString()) == false){

                        s1.add(ss.toString());

                        if(!ts.contains(ss.toString())){   //java的集合里面好像不支持StringBuffer的比较,

                                                            // 所以要转化成String类型来比较

                            ts.add(ss.toString());          //代码大意是判断集合中是否存在当前的字符串

                        }

                        cnt ++;

                    }

                    ss.delete(0,ss.length());

                }else{

                    if(s2.contains(ss.toString()) == false){

                        s2.add(ss.toString());

                        if(!ts.contains(ss.toString())){

                            ts.add(ss.toString());

                        }

                        cnt ++;

                    }

                    ss.delete(0,ss.length());

                }

            }

        }

        for(String tt : ts){

            System.out.println(tt);

        }

    }

}

7-9 sdust-Java-学生成绩读取与排序 (10 分)

import java.util.*;

public class Main {

    public static void main(String[] args) {

        stu[] nums = new stu[100];            //用一个数组来记录学生类,方便调用Arrays.sort

        Scanner in = new Scanner(System.in);

        HashMap<Integer,stu> hs = new HashMap<Integer, stu>();

        int n = 0;

        while(true){

            String[] arr = (in.nextLine()).split(",");       //分割字符串,以 逗号 为分割点

            if(arr[0].equals("exit"))

                break;

            Integer number = Integer.valueOf(arr[1]);

            if(hs.containsKey(number) == false){          //用哈希表查看是否存在学号,也就是说这个学号的人是否被记录

                stu tmp = new stu();

                tmp.name = arr[0];

                tmp.number = number;

                tmp.insert(arr[2],Integer.valueOf(arr[3])); //Integet.valueOf可以把只含数字的字符串转化为数字

                hs.put(number,tmp);

                nums[n ++] = tmp;

            }else{

                stu tmp = hs.get(number);

                tmp.insert(arr[2],Integer.valueOf(arr[3]));

            }

        }

        Arrays.sort(nums,0,n,new stucomparable()); //排序

        for(int i = 0; i < n; i ++){

            System.out.println("No" + (i + 1) + ":"+ nums[i]);

        }

    }

}

class stu{

    String name;

    int number;

    int tot;

    HashSet<String> subject;        //用一个hashset来记录当前的学科是否被添加,如果数据不卡的话不用,直接加单科即可

    public stu(){

        subject = new HashSet<>();

    }

    public void insert(String sub,int score){     //检查并更新学生的成绩

        if(subject.contains(sub) == false){

            tot += score;

        }

    }

    public String toString(){               //重写toString

        return number + "," + name;

    }

}

class stucomparable implements Comparator<stu> {    //自定义的类如果要排序需要重写比较器

    public int compare(stu s1,stu s2){

        if(s2.tot == s1.tot){

            return s1.number - s2.number;

        }

        return s2.tot - s1.tot;

    }

}

-10 jmu-Java-03面向对象基础-04-形状-继承

import java.util.Scanner;

public class Main{

public static double sumAllArea(Shape[] shape) {

double sum = 0;

for(int i = 0;i < shape.length;i++) {

sum += shape[i].getArea();

}

return sum;

}

public static double sumAllPerimeter(Shape[] shape) {

double sum = 0;

for(int i = 0;i < shape.length;i++) {

sum += shape[i].getPerimeter();

}

return sum;

}

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

int n = scan.nextInt();

Shape[] shape = new Shape[n];

for(int i = 0;i < shape.length;i++) {

String temp = scan.next();

if(temp.equals("rect")) {

int width = scan.nextInt();

int length = scan.nextInt();

shape[i] = new Rectangle(width,length);

}else if(temp.equals("cir")) {

int radius = scan.nextInt();

shape[i] = new Circle(radius);

}

}

System.out.println(sumAllPerimeter(shape));

System.out.println(sumAllArea(shape));

System.out.print("[");

for(int i = 0;i < shape.length;i++) {

System.out.print(shape[i].toString());

if(i != shape.length - 1)

System.out.print(", ");

}

System.out.println("]");

for(int i = 0;i < shape.length;i++) {

System.out.println(shape[i].getClass() + "," + shape[i].getClass().getSuperclass());

}

}

}

abstract class Shape{

public static final double PI = 3.14;

public abstract double getArea();

public abstract double getPerimeter();

}

class Rectangle extends Shape{

int width;

int length;

public Rectangle(int width, int length) {

super();

this.width = width;

this.length = length;

}

@Override

public double getArea() {

return width * length;

}

@Override

public double getPerimeter() {

return 2 * (width + length);

}

@Override

public String toString() {

return "Rectangle [width=" + width + ", length=" + length + "]";

}

}

class Circle extends Shape{

int radius;

public Circle(int radius) {

super();

this.radius = radius;

}

@Override

public double getArea() {

// TODO Auto-generated method stub

return PI * radius * radius;

}

@Override

public double getPerimeter() {

// TODO Auto-generated method stub

return 2 * PI * radius;

}

@Override

public String toString() {

return "Circle [radius=" + radius + "]";

}

}

7-12 程序改错题2 (

//父类中添加run()方法

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Animal animal = new Dog();

        animal.shout();

        animal.run();

        

    }

}

class Animal {

    void shout() {

        System.out.println("animal shout!");

    }

    void run(){

    

    }

}

class Dog extends Animal {

    void shout() {

        super.shout();

        System.out.println("wangwang……");

    }

    void run() {

        System.out.println("Dog is running");

    }

}

7-14 设计一个Shape及其子类Oval

import java.util.Scanner;

abstract class Shape{

public static final double PI = 3.1415926;

abstract double area();

abstract double perimeter();

}

class Oval extends Shape{

private double a;//长轴半径

private double b;//短轴半径

//构造器

public Oval(double a, double b) {

super();

this.a = a;

this.b = b;

}

public Oval() {

super();

this.a = 0;

this.b = 0;

}

double area(){

return PI * a * b;

}

double perimeter(){

return 2 * PI * Math.sqrt((a * a + b * b) / 2);

}

//public String toString( ),将把当前椭圆对象的转换成字符串形式,

//例如长轴半径为10.0,短轴半径为5,

//返回字符串"Oval(a:10.0,b:5.0)"。

public String toString(){

return "Oval(a:" + a + ",b:" + b + ")";

}

public String getArea(){

return "The area of " + this.toString() + " is " + this.area();

}

public String getPerimeter(){

return "The perimeterof " + this.toString() + " is " + this.perimeter();

}

}

public class Main{

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

double a = scan.nextDouble();

double b = scan.nextDouble();

Oval oval = new Oval(a,b);

System.out.println(oval.getArea());

System.out.println(oval.getPerimeter());

}

}

7-15 USB接口的定义

interface USB{

void work();

void stop();

}

class Mouse implements USB{

public void work() {

System.out.println("我点点点");

}

public void stop() {

System.out.println("我不能点了");

}

}

class Upan implements USB{

public void work() {

System.out.println("我存存存");

}

public void stop() {

System.out.println("我走了");

}

}

public class Main{

public static void main(String[] args) {

USB usb1 = new Mouse();

usb1.work();

usb1.stop();

USB[] usb = new USB[2];

usb[0] = new Upan();

usb[1] = new Mouse();

for(USB u : usb) {

u.work();

u.stop();

}

}

}

jmu-Java-03面向对象基础-01-构造函数与toString

import java.util.*;

public class Main {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

int n = scan.nextInt();

Person[] p = new Person[n];

for(int i = 0;i < n;i++) {

p[i] = new Person(scan.next(),scan.nextInt(),scan.nextBoolean());

}

for(int i = n - 1;i >=0 ;i--) {

System.out.println(p[i].toString());

}

Person per = new Person();

System.out.println(per.toString());

}

}

class Person{

private String name;

private int age;

private boolean gender;

private int id;

public Person() {

super();

System.out.println("This is constructor");

System.out.println(name + "," + age + "," + gender + "," + id);

}

@Override

public String toString() {

return "Person [name=" + name + ", age=" + age + ", gender=" + gender + ", id=" + id + "]";

}

public Person(String name, int age, boolean gender) {

super();

this.name = name;

this.age = age;

this.gender = gender;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public boolean isGender() {

return gender;

}

public void setGender(boolean gender) {

this.gender = gender;

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

}

7-17 定义商品类,封装成员变量,输出对象 (

import java.util.Scanner;

public class Main{

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

Goods goods = new Goods("WJ002","记事本",5.5);

System.out.println(goods.toString());

}

}

class Goods{

private String id;

private String name;

private double price;

//构造器

public Goods(String id, String name, double price) {

super();

this.id = id;

this.name = name;

this.price = price;

}

//toString

@Override

public String toString() {

return id + "," + name + "," + price;

}

//访问器

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public double getPrice() {

return price;

}

public void setPrice(double price) {

this.price = price;

}

}

7-18 定义类与创建对象

import java.util.Scanner;

public class Main{

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

Person p1 = new Person("lili",19);

Person p2 = new Person("lucy",20);

System.out.println(p1.toString());

System.out.println(p2.toString());

}

}

class Person{

String name;

int age;

public Person(String name, int age) {

super();

this.name = name;

this.age = age;

}

@Override

public String toString() {

return "this person is " + name  + ",her age is " + age;

}

}

7-20 好玩的图形

import java.util.Scanner;

interface Shape{

float getArea();//求面积

float getPerimeter();//求周长

}

class Circle implements Shape{

public float radius;

public Circle(float radius) {

super();

this.radius = radius;

}

@Override

public float getArea() {

return (float)(Math.PI * radius * radius);

}

@Override

public float getPerimeter() {

return (float)(2 * Math.PI * radius);

}

}

class Rectangle implements Shape{

private float length;

private float heigth;

public Rectangle(float length, float heigth) {

super();

this.length = length;

this.heigth = heigth;

}

@Override

public float getArea() {

return length * heigth;

}

@Override

public float getPerimeter() {

return 2 * (length + heigth);

}

}

public class Main{

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        int n = scan.nextInt();

        Shape[] shape = new Shape[n];

        for(int i = 0; i < n;i++){

         int temp = scan.nextInt();

         if(temp == 1){

         shape[i] = new Circle(scan.nextFloat());

         System.out.printf("%.2f %.2f\n",shape[i].getArea(),shape[i].getPerimeter());

        

         }else if(temp == 2){

         shape[i] = new Rectangle(scan.nextFloat(),scan.nextFloat());

         System.out.printf("%.2f %.2f\n",shape[i].getArea(),shape[i].getPerimeter());

        

         }

        }

        

    }

}

7-4 字母图形

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        char[] array = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',

                'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };

        Scanner sc=new Scanner(System.in);

        int h=sc.nextInt();

        int l=sc.nextInt();

        int k;

        for(int i=0;i<h;i++) {

            for(int j=0;j<l;j++) {

                k=Math.abs(i-j); //取绝对值

                System.out.print(array[k]);

            }

           System.out.printf("\n");

        }

    }

}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值