首页 > 编程语言 >软件工程 之 (XMUT)PTA Java 编程题

软件工程 之 (XMUT)PTA Java 编程题

时间:2023-09-18 18:26:46浏览次数:44  
标签:Java String int System PTA out public XMUT Scanner

Java编程题

先随笔记录,以后有时间再整理吧。

求最大值
统计符合条件元素的个数
import java.util.Scanner;
public class Main {

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int n = scanner.nextInt();
    int a = 0,b = 0;
    for (int i = 1; i <= n; i++) {
        if (i % 3 == 0 && i % 2 == 0){
            b++;  
        }
        if (i % 3 == 0 && i % 2 != 0){
            a++;
        }
    }
    System.out.println(a+","+b);
}   

}
判断闰年

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int y = scanner.nextInt();
if (((y % 4 == 0 )&& (y % 100 != 0)) || y % 400 == 0) {
System.out.println("yes");
} else {
System.out.println("no");
}
}
}
java基本语法-整数四则运算
import java.util.Scanner;

public class Main{
public static void main(String args[]){
Scanner in= new Scanner(System.in);
int a=in.nextInt();
int b= in.nextInt();
System.out.println(a+b);
System.out.println(a-b);
System.out.println(ab);
float c=(float)a/b;
System.out.println(c);
}
}
学投资
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
int M=scanner.nextInt();
float P=scanner.nextFloat();
int N=scanner.nextInt();
double a,b,c;
a=M
(Math.pow(1+P,N));
b=M(1+PN);
c=a>b?a-b:b-a;
System.out.print(Math.round(a)+" ");
System.out.print(Math.round(b)+" ");
System.out.print(Math.round(c));
}
}
点是否在圆内?
import java.util.Scanner;
public class Main {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n1 = sc.nextInt();
    int n2 = sc.nextInt();
    System.out.println(Math.sqrt(n1*n1+n2*n2)<=10?1:0);
}

}
给出一个月的总天数
import java.util.Scanner;
public class Main {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n1 = sc.nextInt();
    int n2 = sc.nextInt();
    int n3;
    switch(n1){
        case 2:
            if(n2%4==0&&n2%100!=0||n2%400==0)
                n3=29;
            else
                n3=28;
            break;
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            n3=31;
            break;
        default:
            n3=30;
    }
    System.out.println(n3);
}

}
程序改错题1
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int x, j = 1;
x = in.nextInt();
switch (x) {
case 1:
j++;break;
case 2:
j++;break;
case 3:
j = x*j++;break;
case 4:
j= x + j++;break;
case 5:
j = x - j++;break;
default:
j = x;break;
}
System.out.println(j);
}
}
成绩分级管理
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
int n;
Scanner input=new Scanner(System.in);
n=input.nextInt();
if(n<60)
{
System.out.println("E");
}
if(n>=60&&n<70)
{
System.out.println("D");
}
if(n>=70&&n<80)
{
System.out.println("C");
}
if(n>=80&&n<90)
{
System.out.println("B");
}
if(n>=90)
{
System.out.println("A");
}
}
}
Java中二进制位运算
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
String [] arr = str.split("\s+");
int num1 = Integer.parseInt(arr[0]);
int num2 = Integer.parseInt(arr[2]);
switch (arr[1]){
case "&":
System.out.println(num1 + " & " + num2 + " = " + (num1&num2));
break;
case "|":
System.out.println(num1 + " | " + num2 + " = " + (num1|num2));
break;
case "^":
System.out.println(num1 + " ^ " + num2 + " = " + (num1^num2));
break;
default:
System.out.println("ERROR");
break;
}

}

}
电话键盘字母数字转换
import java.util.Scanner;
public class Main {
public static void main(String[] args){
//输入一个char
Scanner cin=new Scanner(System.in);
String s=cin.nextLine();
char a=s.charAt(0);//第一个字母
if((a>='a'&&a<='z')||(a>='A'&&a<='Z'))
{
if((a>='a'&&a<='c')||(a>='A'&&a<='C'))
System.out.println(2);
if((a>='d'&&a<='f')||(a>='D'&&a<='F'))
System.out.println(3);
if((a>='g'&&a<='i')||(a>='G'&&a<='I'))
System.out.println(4);
if((a>='j'&&a<='l')||(a>='J'&&a<='L'))
System.out.println(5);
if((a>='m'&&a<='o')||(a>='M'&&a<='O'))
System.out.println(6);
if((a>='p'&&a<='s')||(a>='P'&&a<='S'))
System.out.println(7);
if((a>='t'&&a<='v')||(a>='T'&&a<='V'))
System.out.println(8);
if((a>='w'&&a<='z')||(a>='W'&&a<='Z'))
System.out.println(9);
}
else
System.out.println(a+" is an invalid input");
}
}
西安距离
import java.util.Scanner;
public class Main {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int x = sc.nextInt();
    int y = sc.nextInt();
    int z = sc.nextInt();
    int b = sc.nextInt();
    x = Math.abs(z-x)+Math.abs(b-y);
    System.out.println(x);
}

}
编程题:统计符合条件元素的个数
import java.util.Scanner;
public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int i,j=0,o=0;
for(i=1;i<=n;i++) {
if(i%30) {
if(i%2
0) {
o++;
}else {
j++;
}
}
}
System.out.println(j+","+o);
}
}
JAVA-求整数序列中出现次数最多的数
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
HashMap<Integer,Integer> m = new HashMap<>();
int n = cin.nextInt();
for(int i = 0;i < n;i++){
int x = cin.nextInt();
if(m.containsKey(x)){
m.put(x,m.get(x)+1);
}
else{
m.put(x,1);
}
}
int k = 0,sum = 0;
for(Map.Entry<Integer,Integer> entry: m.entrySet()){
int x = entry.getKey();
int y = entry.getValue();
if(y > sum){
k = x;
sum = y;
}
}
System.out.println(k+" "+sum);
}
}
统计正数和负数的个数然后计算这些数的平均值
import java.util.Scanner;

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

    int positiveNum = 0;  // 正数
    int    negativeNum = 0;  // 负数
    int sum = 0;   // 总和
    
    while (true) {
        int x = input.nextInt();
        if (x == 0)     // 当读到0时,输入结束
            break;
        
        if (x > 0) 
            positiveNum++;    
        else 
            negativeNum++;
        
        sum += x;
    }
    
    if (positiveNum + negativeNum != 0) {    
        System.out.println(positiveNum);
        System.out.println(negativeNum);
        System.out.println(sum);
        
        double average = 0.0;
        average = 1.0 * sum / (positiveNum + negativeNum);
        System.out.println(average);
    }
}

}
分队列
import java.util.Scanner;
public class Main{
public static void main(String [] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt(); //n=10
for(int i=1;i<=n;i+=2){ //报1的同学出列
System.out.print(i);
if(i!=n&&i<n-1){ // 最后报的数不是n且
System.out.print(" ");
}
}
System.out.println();
}

}
累加器
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;
int j=0;

for(i=0;i<n;i++){
    
    j+=in.nextInt();
    
}
System.out.println(j);

}
}
我是升旗手
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 j=in.nextInt();
if(max<j)
max=j;
}
System.out.println(max);
}
}
等腰直角三角形
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n;
int i;
String s = "";
n = in.nextInt();
System.out.println(s);
for(i = 1; i < n; i++){
s += "
";
System.out.println(s);
}
}
}
666
import java.util.;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n=in.nextInt();
int j=0;
int sn=0;
for (int i=0;i<n;i++){
j+=Math.pow(10,i)
6;
sn+=j;
}
System.out.println(sn);
}
}
倒顺数字串
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int count=scanner.nextInt();
if (count==1){
System.out.println(1);
return;
}
int i,j;
for (i=1;i<=count;i++){
System.out.print(i+" ");
}
for (j=count-1;j>1;j--){
System.out.print(j+" ");
}
System.out.println(j);
}
}
上三角数字三角形

include<stdio.h>

int main(){
int n,i;
scanf("%d",&n);
int j=1;
for(;n>0;n--){
for(i=j;i<n+j;i++){
if(i<10)
printf(" %d",i);
else if(i>=10&&i<100)
printf(" %d",i);
else if(i>=100&&i<1000)
printf(" %d",i);
else
printf("%d",i);
}
printf("\n");
j=i;
}
return 0;
}
求质数
import java.io.*;
public class Main {

public static void main(String[] args) throws IOException {
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String n=in.readLine();
int rn=Integer.parseInt(n);
sushu(rn);

   }
   static void  sushu(int n)
{
    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.printf("%d ",i);
        }
    }
}

}
jmu-Java-03面向对象基础-01-构造函数与toString
import java.util.Scanner;

class Person{
private String name = null;
private int age = 0;
private boolean gender = false;
private int id = 0;

public Person() {
    System.out.println("This is constructor");
    System.out.println(name+","+age+","+gender+","+id);
    System.out.println("Person [name="+name+", age="+age+", gender="+gender+", id="+id+"]");
}

public Person(String n, int a, boolean g) {
    this.name = n;
    this.age = a;
    this.gender = g;
}

public String toString() {
    System.out.println("Person [name="+this.name+", age="+this.age+", gender="+this.gender+", id="+0+"]");
    return name;
}

}

public class Main{

public static void main(String[] args) {
    // TODO Auto-generated method stub
    @SuppressWarnings("resource")
    Scanner reader = new Scanner(System.in);
    int number = reader.nextInt();
    Person[] per = new Person[number];
    for(int i=0; i<per.length; i++) {
        String name = reader.next();
        int age = reader.nextInt();
        boolean genter = reader.nextBoolean();
        per[i] = new Person(name,age,genter);
    }
    for(int x=per.length-1; x>=0;x--){    
        per[x].toString();
    }
    
    per.toString();
    @SuppressWarnings("unused")
    Person s = new Person();
}

}
学生类-构造函数
import java.util.Scanner;

class Student{
private String name;
private String sex;
private int age;

public String toString() {
    String str="Student [name='"+name+"', sex='"+sex+"', age="+age+"]";
    return str;
}

public Student(String name, String sex, int age) {
    this.name = name;
    this.sex = sex;
    this.age = age;
}

public Student() {
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getSex() {
    return sex;
}

public void setSex(String sex) {
    this.sex = sex;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

}
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String name=sc.next();
int age=Integer.valueOf(sc.next());
String sx=sc.next();
Student student = new Student(name, sx, age);
System.out.println(student);
}
}
类与对象
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
try (Scanner in = new Scanner(System.in)) {
int a,b,c,d,e;
a = in.nextInt();
b = in.nextInt();
c = in.nextInt();
d = in.nextInt();
e = in.nextInt();
RR rr = new RR();
double dd = rr.fun(a,b,c,d,e);
System.out.printf("%.2f",dd);
}
}
}
class RR{

public double fun(int a,int b,int c,int d,int e) {
return (a+b+c+d+e)/5;
}

}
计算年龄
import java.util.Scanner;
class Birthday{
private int year;
private int month;
private int day;
Birthday(int year,int month,int day){
this.year=year;
this.month=month;
this.day=day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getAge(){
int age;
age=2017-year;
if(month>=12&&day>25) {
age++;
}
return age;
}
}
public class Main{
public static void main(String[]args) {
Scanner sc=new Scanner(System.in);
Birthday bir=new Birthday(sc.nextInt(),sc.nextInt(),sc.nextInt());
System.out.println("age="+bir.getAge());
}
}
程序填空题2
public class Main {
public static void main(String[] args) {
Test test = new Test(true);
}
}

class Test {
public Test(){
System.out.println("Constructor one invoked!");
}
public Test(int x){
this();
System.out.println("Constructor two invoked!");
}
public Test(boolean b){
this(1);
System.out.println("Constructor three invoked!");
}

}
定义类与创建对象
class Person{
String name;
int age;
Person(String n,int a) {
name=n;
age=a;
}
}

public class Main{

public static void main(String[] args) {
    // TODO Auto-generated method stub
 Person p1=new Person("lili",19);
 Person p2=new Person("lucy",20);
 System.out.println("this person is "+p1.name+",her age is "+p1.age);
 System.out.println("this person is "+p2.name+",her age is "+p2.age);
}

}
设计一个BankAccount类
import java.util.Scanner;

class BankAccount{
int balance;
BankAccount() {
this.balance=0;
}
BankAccount(int a){
this.balance=a;
}
public int getBalance() {
return balance;
}
public int withdraw(int amount) {
return balance-amount;

}
public int deposit(int amount) {
    return amount;
            
}

}

public class Main {

public static void main(String[] args) {
    try (// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in)) {
    int a,b,c;
    a=sc.nextInt();
    b=sc.nextInt();
    c=sc.nextInt();
    
    BankAccount m=new BankAccount(a);
    System.out.println(m.getBalance() );
    System.out.println(m.withdraw(b));
    System.out.println(m.withdraw(b)+m.deposit(c));
}
 
}

}
程序改错题2
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!");
}

public void run() {
    // TODO Auto-generated method stub
    
}

}

class Dog extends Animal {
void shout() {
super.shout();
System.out.println("wangwang……");
}

public void run() {
    System.out.println("Dog is running");
}

}
程序填空题3
public class Main {
public static void main(String[] args) {
Son son = new Son();
son.method();
}

}
class Parent {
Parent() {
System.out.println("Parent's Constructor without parameter");
}

Parent(boolean b) {
    System.out.println("Parent's Constructor with a boolean parameter");
}

public void method() {
    System.out.println("Parent's method()");
}

}

class Son extends Parent {
//补全本类定义
private static boolean b;//为什么要写static
Son()
{//为什么不写public
super(b);
System.out.println("Son's Constructor without parameter");
}
public void method() {
System.out.println("Son's method()");
System.out.println("Parent's method()");

}

}
设计圆和圆柱体
import java.util.Scanner;
public class Main{
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
for(int i = 0; i < n; i++) {
String str = input.next();
if(str.equals("Circle")) {
Circle c = new Circle(input.nextDouble());
System.out.println("The area of " + c.toString() + " is " + String.format("%.2f",c.getArea()));
System.out.println("The perimeterof " + c.toString() + " is "+ String.format("%.2f",c.getPerimeter()));
} else if(str.equals("Cylinder")) {
Cylinder r = new Cylinder(input.nextDouble(), new Circle(input.nextDouble()));
System.out.println("The area of " + r.toString() + " is " + String.format("%.2f",r.getArea()));
System.out.println("The volume of " + r.toString() + " is " + String.format("%.2f",r.getVolume()));
}
}
}
}
class Circle{
private double radius;

public Circle(double radius){
    this.radius = radius;
}
public Circle(){
    radius = 0;
}
public void setRadius(double r){
    radius = r;
}
public double getRadius(){
    return this.radius;
}
public double getArea(){
    return Math.PI * radius * radius;
}
public double getPerimeter(){
    return 2 * Math.PI * radius;
}
@Override
public String toString(){
    return "Circle(r:" + radius + ")";
}

}
class Cylinder{
private double height;
private Circle circle;
public Cylinder(double height, Circle circle){
this.height = height;
this.circle = circle;
}
public Cylinder(){
height = 0;
circle = new Circle(0);
}
public void setHeight(double height){
this.height = height;
}
public double getHeight(){
return height;
}
public void setCircle(Circle circle){
this.circle = circle;
}
public Circle getCircle(){
return circle;
}
public double getArea(){
return 2 * Math.PI * circle.getRadius() * circle.getRadius() + 2 * Math.PI * circle.getRadius() * height;
}
public double getVolume(){
return Math.PI * circle.getRadius() * circle.getRadius() * height;
}
@Override
public String toString(){
return "Cylinder(h:" + height + "," + circle.toString() + ")";
}
}
员工类
import java.util.Scanner;

class Employee {
public int salary;

public Employee() {
    this.salary = 0;
}

public Employee(int salary) {
    this.salary = salary;
}

public int getSalary() {
    return salary;
}

public void setSalary(int salary) {
    this.salary = salary;
}

}

class Manager extends Employee {
public int salary;

public Manager(int salary) {
    this.salary = salary;
}

@Override
public int getSalary() {
    return salary;
}

@Override
public void setSalary(int salary) {
    this.salary = salary;
}

}

class Salesman extends Employee {
public int salary;
public int commission;

public Salesman(int salary, int commission) {
    this.salary = salary;
    this.commission = commission;
}

@Override
public int getSalary() {
    return salary + commission;
}

public void setSalary(int salary, int commission) {
    this.salary = salary;
    this.commission = commission;
}

}

class Worker extends Employee {

public int day;
public int daySalary;

public Worker(int day, int daySalary) {
    this.day = day;
    this.daySalary = daySalary;
}

public int getSalary() {
    return daySalary * day;
}

public void setSalary(int day, int daySalary) {
    this.day = day;
    this.daySalary = daySalary;

}

}

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int ms = scanner.nextInt();
int ss = scanner.nextInt(), sc = scanner.nextInt();
int wd = scanner.nextInt(), wds = scanner.nextInt();

    Manager m = new Manager(ms);
    Salesman s = new Salesman(ss, sc);
    Worker w = new Worker(wd, wds);

    System.out.println(m.getSalary());
    System.out.println(s.getSalary());
    System.out.println(w.getSalary());
}

}
Shape类-2
import java.util.Scanner;
public class Main{
public static void main(String[] args)
{
Shape s1,s2,s3;
Scanner scan = new Scanner(System.in);
double length = scan.nextDouble();
s1 = new Square(length);
System.out.printf("%.2f ",s1.getPerimeter());
System.out.printf("%.2f\n",s1.getArea());

     length = scan.nextDouble();
     double wide = scan.nextDouble();
     s2 = new Rectangle(length,wide);
     System.out.printf("%.2f ",s2.getPerimeter());
     System.out.printf("%.2f\n",s2.getArea());

     double radius = scan.nextDouble();
     s3 = new Circle(radius);
     System.out.printf("%.2f ",s3.getPerimeter());
     System.out.printf("%.2f\n",s3.getArea());

     scan.close(); 
}

}
class Shape
{
public double getPerimeter() {return 0;}
public double getArea() {return 0;}
}
class Square extends Shape
{
double length;
public Square(double length)
{
this.length = length;
}
@Override
public double getPerimeter()
{
return 4 * length;
}
@Override
public double getArea()
{
return length * length;
}
}
class Rectangle extends Square
{
double width;
public Rectangle(double length,double width)
{
super(length);
this.width = width;
}
@Override
public double getArea() {
return length*width;
}

@Override
public double getPerimeter() {
    return 2*(length+width);
}

}
class Circle extends Shape
{
double r;
public Circle(double r)
{
this.r = r;
}
@Override
public double getArea()
{
return Math.PI * r * r;
}
@Override
public double getPerimeter()
{
return 2 * Math.PI * r;
}
}
学生、大学生、研究生类-2
import java.util.Scanner;

abstract class Student{
String name;
int id;
String sex;
// String major;
int grade;
String major;
Student(int id, String name, String sex, int grade, String major){
this.id = id;
this.name = name;
this.sex = sex;
this.grade = grade;
this.major = major;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getSex() {
    return sex;
}

public void setSex(String sex) {
    this.sex = sex;
}

abstract void getGrade();

}

class Undergraduate extends Student{
// 学号、姓名、性别、专业、成绩

Undergraduate(int id, String name, String sex , String major, int grade){
    super(id, name, sex, grade, major);
    this.major = major;
}

@Override
void getGrade() {
    if(grade >= 80){
        System.out.println('A');
    }
    else if(grade >= 70){
        System.out.println('B');
    }
    else if(grade >= 60){
        System.out.println('C');
    }
    else if(grade >= 50){
        System.out.println('D');
    }
    else{
        System.out.println('E');
    }
}

}

class Graduate extends Student{
String teacher;
Graduate(int id, String name, String sex, String major, String teacher, int grade){
super(id, name, sex, grade, major);
this.teacher = teacher;
}

@Override
void getGrade() {
    if(grade >= 90){
        System.out.println('A');
    }
    else if(grade >= 80){
        System.out.println('B');
    }
    else if(grade >= 70){
        System.out.println('C');
    }
    else if(grade >= 60){
        System.out.println('D');
    }
    else{
        System.out.println('E');
    }
}

}

public class Main {

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

    int id = sc.nextInt();
    String name = sc.next();
    String sex = sc.next();
    String major = sc.next();
    int grade = sc.nextInt();
    Student a = new Undergraduate(id, name, sex, major, grade);
    a.getGrade();

    id = sc.nextInt();
    name = sc.next();
    sex = sc.next();
    major = sc.next();
    String teacher = sc.next();
    grade = sc.nextInt();
    Student b = new Graduate(id, name, sex, major, teacher, grade);
    b.getGrade();

}

}
jmu-Java-01入门-取数字浮点数
import java.util.Scanner;
//FloatingPoint
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
String s = sc.nextLine();
char[] ch = s.toCharArray();
int sum = 0;
for (int i = 0; i < ch.length; i++) {
if (ch[i] == '-'|| ch[i] == '.') {

            } else {
                //将字符char类型转换成int类型
                int temp = Integer.parseInt(String.valueOf(ch[i]));
                sum += temp;
            }
        }
        System.out.println(sum);
    }
}

}
无聊的小明来数1
import java.util.Scanner;

public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int T = scanner.nextInt();//输入
String[] number = new String[T];
for(int i=0;i<T;i++){//输入
number[i] = bin(scanner.nextInt());
}
for(String i:number){//输出
System.out.println(count(i));
}
}
public static String bin(int num1){//递归实现,将十进制转为二进制
if(num1/20){//商为0时
return num1%2 + "";//返回转换为String类型的余数
}else{
return bin(num1/2) + num1%2 + " ";//下一层递归的结果放左侧
}
}
public static int count(String str){//计算字符串中1的个数
int num = 0;
for(int i=0;i<str.length();i++){//遍历字符串
if(str.charAt(i)
'1'){
num ++;
}
}
return num;
}
}
jmu-Java-03面向对象基础-05-覆盖
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n1= sc.nextInt();
boolean f=true;
sc.nextLine();
ArrayList ap=new ArrayList();
for (int i=0;i<n1;i++){
ap.add(new PersonOverride());
}
int n2= sc.nextInt();
sc.nextLine();
for (int i=0;i<n2;i++){
f=true;
PersonOverride a=new PersonOverride(sc.next(),sc.nextInt(), sc.nextBoolean());
sc.nextLine();
for (PersonOverride aa:ap) {
if (aa.equals(a)){
f=false;
break;
}
}
if (f)ap.add(a);
}

    for(PersonOverride a:ap){
        System.out.println(a.toString().replace("[","").replace("]",""));
    }
    System.out.println(ap.size()-n1);
    System.out.println(Arrays.toString(PersonOverride.class.getConstructors()));
}

}

class PersonOverride{
private String name;
private int age;
private boolean gender;

public PersonOverride() {
    this("default",1,true);
}

public PersonOverride(String name, int age, boolean gender) {
    this.name = name;
    this.age = age;
    this.gender = gender;
}


@Override
public String toString() {
    return name + "-" + age + "-" + gender;
}

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    PersonOverride that = (PersonOverride) o;
    return age == that.age &&
            gender == that.gender &&
            name.equals(that.name);
}

@Override
public int hashCode() {
    return Objects.hash(name, age, gender);
}

}
教师类

import java.util.Scanner;
public class Main {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int no,age;
    String name,seminary;
    int i;
    no = sc.nextInt();
    name = sc.next();
    age = sc.nextInt();
    seminary = sc.next();
    Teacher t1 = new Teacher(no,name,age,seminary);
    
    
    no = sc.nextInt();
    name = sc.next();
    age = sc.nextInt();
    seminary = sc.next();
    Teacher t2 = new Teacher(no,name,age,seminary);
    
    System.out.println(t1.toString());
    System.out.println(t2.toString());
    System.out.println(t1.equals(t2));
}

}

class Teacher{
int no,age;
String name,seminary;
public Teacher(int no,String name,int age,String seminary) {
this.no = no;
this.name = name;
this.age = age;
this.seminary = seminary;
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
public int getNo() {
return no;
}
public String getSeminary() {
return seminary;
}
public boolean equals(Teacher t) {
if(this.no == t.no)
return true;
else
return false;
}
public String toString() {
return "no: "+no+", name:"+name+", age: "+age+", seminary: "+seminary;
}
}
声明图书类,记录图书总册数,利用静态变量赋值。
class Book {
private String bookName;
private int bookNo;
private double price;
private static int num = 1;

public Book(String bookName, double price) {
    this.bookName = bookName;
    this.price = price;
    this.bookNo = num++;
}

public String getBookName() {
    return bookName;
}

public void setBookName(String bookName) {
    this.bookName = bookName;
}

public int getBookNo() {
    return bookNo;
}

public double getPrice() {
    return price;
}

public void setPrice(double price) {
    this.price = price;
}

public static int getNum() {  
    return num - 1;
}

}

public class Main {

public static void main(String[] args) {
    
    Book[] book = {new Book("Java程序设计", 34.5), new Book("数据结构", 44.8), new Book("C++程序设计", 35.0)};
    for (int i = 0; i < book.length; i++) {
        System.out.println("书名:" + book[i].getBookName() + ", 书号:" + book[i].getBookNo() + ", 书价:" + book[i].getPrice());
    }
    System.out.println("图书总册数为:" + Book.getNum());
}

}
定义类2
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
double dd = RR.fun();
System.out.printf("%.2f",dd);
}
}
class RR
{
static double fun()
{
int i,j,index;
double t;
double f[]=new double[5];
Scanner in=new Scanner(System.in);
for(i=0;i<5;i++)
f[i]=in.nextDouble();
for(i=0;i<4;i++)
{
index=i;
for(j=i+1;j<5;j++)
if(f[index]>f[j])
index=j;
t=f[i];f[i]=f[index];f[index]=t;
}

    return f[2];
}

}
Person类-静态变量
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Person[] p=new Person[3];
int sum=0;
int avg;
for(int i = 0; i < 3; i ++ ) {
p[i] = new Person(sc.next(), sc.next(), sc.nextInt());
}
for(int i = 0; i < 3; i ++ )
sum=sum+p[i].getAge();
avg = sum/3;
System.out.println(avg);
sc.close();
}
}
class Person{
private String name;
private String sex;
private int age;
private static int avgAge = 0;
Person(String name,String sex,int age){
this.name=name;
this.sex=sex;
this.age=age;
}
int getAge(){
return age;
}
}
jmu-Java-03面向对象基础-04-形状-继承
import java.util.Scanner;

abstract class Shape {
double PI = 3.14;

public abstract double getPerimeter();

public abstract double getArea();

}

class Rectangle extends Shape {
int wide, len;

Rectangle(int a, int b) {
    wide = a;
    len = b;
}

@Override
public double getPerimeter() {
    // TODO Auto-generated method stub
    return 2 * (wide + len);
}

@Override
public double getArea() {
    // TODO Auto-generated method stub
    return wide * len;
}

public String toString(){
    return "[width=" + wide + ", length=" + len + "]";
}

}

class Circle extends Shape {
int radius;

Circle(int _radius) {
    radius = _radius;
}

@Override
public double getPerimeter() {
    // TODO Auto-generated method stub
    return radius * 2 * PI;
}

@Override
public double getArea() {
    // TODO Auto-generated method stub
    return radius * radius * PI;
}

public String toString(){
    return "[radius=" + radius + "]";
}

}

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.nextLine();
Shape A[] = new Shape[n];
int k = 0, j = 0;
double sumAllArea = 0, sumAllPerimeter = 0;
for (int i = 0; i < n; i++) {
String S = in.next();
if (S.equals("rect")) {
int wide = in.nextInt(), len = in.nextInt();
in.nextLine();
A[i] = new Rectangle(wide, len);
} else if (S.equals("cir")) {
int radius = in.nextInt();
in.nextLine();
A[i] = new Circle(radius);
}
sumAllArea += A[i].getArea();
sumAllPerimeter += A[i].getPerimeter();
}

    System.out.println(sumAllPerimeter);
    System.out.println(sumAllArea);
    System.out.print("[");
    for (int i = 0; i < n; i++) {
        if(i != 0)
            System.out.print(", ");
        if (A[i] instanceof Rectangle) {
                System.out.print("Rectangle ");
            System.out.print(A[i].toString());
        }
        else {
                System.out.print("Circle ");
            System.out.print(A[i].toString());    
        }
    }

    System.out.println("]");

    for(int i = 0;i < n;i++) {
        if(A[i] instanceof Rectangle) {
            System.out.println("class Rectangle,class Shape");
        }else {
            System.out.println("class Circle,class Shape");

        }
    }
    in.close();
}

}
集体评分
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] grade = new int[5];
for(int i=0; i<grade.length; i++){
grade[i] = in.nextInt();
}

            RR rr = new RT(grade);
            double dd = rr.mark();
            System.out.printf("%.2f",dd);
}

}

interface RR{
abstract double mark();
}

class RT implements RR{
int[] grade;
public RT(int[] grade){
this.grade = grade;
}
public double mark() {
int sum=0;
int max=grade[0];
int min=grade[0];
for(int i=0;i<grade.length;i++) {
if(max<grade[i]) {
max=grade[i];
}
else if(min>grade[i]) {
min=grade[i];
}
sum+=grade[i];
}
return (sum-max-min)/3;
}
}
定义ClassName接口,设计类Company,实现获取类名称的功能
interface ClassName {

public String getClassName();

}

class Company implements ClassName {
public String getClassName(){
return "Company";
}
}

public class Main {
public static void main(String []args){
ClassName name=new Company();
System.out.println(name.getClassName());
}
}
集体评分2
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] grade = new int[5];
for(int i=0; i<grade.length; i++){
grade[i] = in.nextInt();
}

            RR rr = new RT(grade);
            double dd = rr.mark();
            System.out.printf("%.2f",dd);
}

}

interface RR{
abstract double mark();
}

class RT implements RR{
int[] grade;
public RT(int[] grade){
this.grade = grade;
}
public double mark() {
int sum=0;
int max=grade[0];
int min=grade[0];
for(int i=0;i<grade.length;i++) {
if(max<grade[i]) {
max=grade[i];
}
else if(min>grade[i]) {
min=grade[i];
}
sum+=grade[i];
}
return (sum-max-min)/3;
}
}
接口--四则计算器

import java.util.Scanner;

public class Main{

public static void main(String[] args) {
    Scanner in=new Scanner(System.in);
    int n=in.nextInt();
    int m=in.nextInt();
    Add l=new Add();
    Sub j=new Sub();
    System.out.println(l.computer(n, m));
    System.out.println(j.computer(n, m));
    
    in.close();

}

}
interface IComputer{
abstract public int computer(int n ,int m);
}
class Add implements IComputer
{
int m,n;
public int computer(int n ,int m) {
return n+m;
}
}
class Sub implements IComputer
{
int m,n;
public int computer(int n ,int m) {
return n-m;
}
}
USB接口的定义
public class Main {

public static void main(String[] args) {
    Mouse usb1=new Mouse();
    usb1.work();
    usb1.stop();
    USB usbs[]=new USB[2];
    usbs[0]=new UPan();
    usbs[0].work();
    usbs[0].stop();
    usbs[1]=new Mouse();
    usbs[1].work();
    usbs[1].stop();
    

}

}
interface USB{
void work();
void stop();
}
class Mouse implements USB{
public void work(){//要写public
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("我走了");
}
}
jmu-Java-06异常-01-常见异常
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s=null;
int a[]=new int[5];
while(!(s=scan.next()).equals("other")) {
if(s.equals("arr")) {
int t=scan.nextInt();
try {
int tt=a[t];
}catch(Exception e) {
System.out.println(e);
}
}
else if(s.equals("null")) {
System.out.println("java.lang.NullPointerException");
}
else if(s.equals("cast")) {
System.out.println("java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer");
}
else if(s.equals("num")) {
String t=scan.next();
try {
int temp=Integer.parseInt(t);
}catch(Exception e) {
System.out.println(e);
}
}
}

    scan.close();
}

}
jmu-Java-06异常-02-使用异常机制处理异常输入
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n=scan.nextInt();
int a[]=new int[n];
int count=0;
while(count<n) {
try {
String temp=scan.next();
int i=Integer.parseInt(temp);
a[count++]=i;
}catch(Exception e) {
System.out.println(e);
}
}
System.out.println(Arrays.toString(a));
scan.close();
}
}
天不假年

import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int age;
age = in.nextInt();
Person p = new Person(age);
age = in.nextInt();
try{
p.setAge(age);
System.out.println("A");
}catch(AgeException e){
System.out.println("B");
}
}
}
class Person{
int age;
public Person(int age){
this.age = age;
}
public void setAge(int age) throws AgeException {
if(this.age <=age){
this.age = age;
}else{
throw new AgeException();
}
}
}
class AgeException extends Exception{
}
成绩录入时的及格与不及格人数统计
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int count=sc.nextInt();
int pass=0;
for (int i=0;i<count;i++){
int grade=sc.nextInt();
if (grade<0||grade>100){
System.out.println(grade+"invalid!");
i--;
}else if (grade>=60){
pass++;
}
}
System.out.println(pass);
System.out.println(count-pass);

}

}
设计一个Tiangle异常类
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double s1 = input.nextDouble();
double s2 = input.nextDouble();
double s3 = input.nextDouble();
try {
Triangle t = new Triangle(s1,s2,s3);
System.out.println(t);
}catch (IllegalTriangleException ex) {
System.out.println(ex.getMessage());
}
}
}
class IllegalTriangleException extends Exception{
private double side1;
private double side2;
private double side3;

    public IllegalTriangleException(double side1,double side2 ,double side3) {
        super("Invalid: "+side1+","+side2+","+side3);//message是父类成员变量,使用super向上传参!
        this.side1=side1;
        this.side2=side2;
        this.side3=side3;
    }

}
class Triangle{

    private double side1;
    private double side2;
    private double side3;
    
    public Triangle(double side1,double side2,double side3) 
      throws IllegalTriangleException{
            if((side1+side2)>side3&(side1+side3)>side2&(side2+side3)>side1) 
                {this.side1=side1;this.side2=side2;this.side3=side3;}
            else
                throw new  IllegalTriangleException(side1,side2,side3);
    }
    @Override
    public String toString(){
        return "Triangle ["+"side1="+this.side1+", side2="+this.side2+", side3="+this.side3+"]";
    }

}
计算正五边形的面积和周长
import java.lang.Math;
import java.math.BigDecimal;
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();
Bian b1=new Bian(a);

DecimalFormat df1 = new DecimalFormat("#.####"); 
System.out.println(df1.format(b1.getArea()));
System.out.println(df1.format(b1.getPerimeter()));
} 

}
abstract class shape{
public abstract double getArea();
public abstract double getPerimeter();
}
class Bian extends shape{
private double a;
public Bian(double a){
this.a=a;
}
public double getArea(){
double s=aa/4(Math.sqrt(25+10Math.sqrt(5.0)));
return s;
}
public double getPerimeter(){
return a
5;
}
}
创建一个倒数计数线程
import java.util.Scanner;
public class Main {

public static void main(String[] args) {
    Test t=new Test();
    Thread th=new Thread(t);
    th.start();
}

}
class Test implements Runnable {
public void run() {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=n;i>=0;i--){
System.out.println(i);
try
{
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}

}
程序改错题4
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Thread t = new Thread(new RunHandler());
t.start();
}
}

class RunHandler implements Runnable {
public void run() {
Scanner in = new Scanner(System.in);
int x = in.nextInt();
for(int i=0;i<x;i++) {
System.out.println("run");
}
}
}
试试多线程
import java.util.Scanner;

public class Main {
public static void main(String[] args){
MyRunnble mr1=new MyRunnble(0);
MyRunnble mr2=new MyRunnble(25);
MyRunnble mr3=new MyRunnble(50);
MyRunnble mr4=new MyRunnble(75);
Thread t1=new Thread(mr1);
Thread t2=new Thread(mr2);
Thread t3=new Thread(mr3);
Thread t4=new Thread(mr4);
t1.start();
t2.start();
t3.start();
t4.start();

    //这里利用一个标记来保证每个线程执行完毕,不知道有没有更好的思路,欢迎探讨
    while (!mr1.isF()||!mr2.isF()||!mr3.isF()||!mr4.isF());
    System.out.println(mr1.getSum()+ mr2.getSum()+ mr3.getSum()+ mr4.getSum());
}

}

class MyRunnble implements Runnable{
private int num;
private int sum=0;
private boolean f=false;

public MyRunnble(int num) {
    this.num = num;
}

@Override
public void run() {
    for (int i=0;i<25;i++){
        num++;
        sum+=num;
    }
    f=true;
}

public int getSum() {
    return sum;
}

public boolean isF() {
    return f;
}

}
单词替换
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main (String [] args) {

    Scanner scanner = new Scanner(System.in);
    String oldStr = scanner.nextLine();
    String searchWord = scanner.nextLine();
    String replaceWord = scanner.nextLine();
    scanner.close();


    String [] removeCommaStr = oldStr.split(",");
    StringBuilder resultStr = new StringBuilder();

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

       
        String[] splitStr = removeCommaStr[i].split("\\s+");


        List<String> strList = Arrays.asList(splitStr);
    
        Collections.replaceAll(strList, searchWord, replaceWord);

      
        StringBuilder removeCommaResultStr = new StringBuilder();
        for (String str : strList) {
            removeCommaResultStr.append(str).append(" ");
        }

     
        removeCommaResultStr.setLength(removeCommaResultStr.length() - 1);

       
        resultStr.append(removeCommaResultStr).append(",");
    }
   
    resultStr.setLength(resultStr.length() - 1);
    System.out.println(resultStr.toString());
}

}
图书价格汇总
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Scanner;

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

    int total = 0;
    String str = sc.nextLine();

    String[] sList = str.split(";");
    ArrayList<String> strList = new ArrayList<>();

    for (String s : sList) {
        strList.add(s);
    }

    for (String s : strList) {
        System.out.println(s);
        total += Integer.parseInt(s.substring(s.indexOf(":") + 1).trim());
    }

    System.out.println("总价格为" + total);

    sc.close();
}

}
通过键盘输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
判断登录信息是否正确--字符串比较
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");
}
}
伪随机数
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);
}
}
字符串字母大小写转换逆序输出
import java.util.Scanner;
public class Main
{
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
StringBuffer sb=new StringBuffer();
for(int i=0;i<s.length();i++) {
char ch=s.charAt(i);
if(Character.isUpperCase(ch)) {
sb.append(Character.toLowerCase(ch));
}
else if(Character.isLowerCase(ch)) {
sb.append(Character.toUpperCase(ch));
}
else
sb.append(ch);
}
System.out.println(sb.reverse());
}

}
计算正五边形的面积和周长
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;

public double getArea() {

    return 5*Math.pow(a,2)/(4*Math.tan(36*Math.PI/180));
}


public double getPerimeter() {

    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();
}
}
创建一个倒数计数线程
import java.util.Scanner;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.sql.Time;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

class CountThread implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int n = in.nextInt();
while (n>=0) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(n);
n--;
}
}

}

public class Main {
public static void main(String[] args) {
CountThread countThread = new CountThread();
new Thread(countThread).start();
}
}
程序改错题4
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Thread t = new Thread(new RunHandler());
t.start();
}
}

class RunHandler implements Runnable {
public void run() {
Scanner in = new Scanner(System.in);
int x = in.nextInt();
for(int i=0;i<x;i++) {
System.out.println("run");
}
}
}
试试多线程
import java.util.Scanner;

class CountThread implements Runnable{
private int n;
private int summ=0;
private boolean f=false;
public CountThread() {

}
public CountThread(int n) {
    this.n = n;
}
@Override
public void run() {
    // TODO Auto-generated method stub
    int t=25;
    while (t>0) {
        summ+=n;
        this.n++;
        t--;
    }
    this.f=true;
}
public int getN() {
    return n;
}

public void setN(int n) {
    this.n = n;
}
public boolean isF() {
    return f;
}
public void setF(boolean f) {
    this.f = f;
}
public int getSumm() {
    return summ;
}
public void setSumm(int summ) {
    this.summ = summ;
}

}

public class Main {
public static void main(String[] args) {
CountThread []countThread = new CountThread[4];
Thread [] threads = new Thread[4];
for (int i=1;i<100;i+=25) {
int index = (int) Math.floor(i/25);
countThread[index]=new CountThread(i);
}
for(int i=0;i<4;i++) {
threads[i] = new Thread(countThread[i]);
threads[i].start();
}
for(int i=0;i<4;i++) {
try {
threads[i].join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
int summ=0;
for(int i=0;i<4;i++) {
summ+=countThread[i].getSumm();
}
System.out.println(summ);
}
}
评委打分

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    //创建选手数组
    int player[] = new int[10];
    //为每位选手赋以初始分数
    for (int i = 0; i < 10; i++) {
        player[i] = input.nextInt();
    }
    //输入题目总数
    int num = input.nextInt();
    //答题选手加分
    for (int i = 0; i < num; i++) {
        //注意数组序号从零开始,此处需要处理一下选手编号
        int index = input.nextInt()-1;
        if (index<11 && index >=0) {
            player[index] =  player[index] + 10;
        }
    }

    //遍历输出
    for (int i = 0; i < player.length; i++) {
        //输出最后一个分数时不带空格
        if (i == player.length-1){
            System.out.println(player[i]);
        }else {
            System.out.print(player[i] + " ");
        }
    }
}

}
结伴同行去秋游

import java.util.Scanner;

public class Main{
public static void main(String a[]){
Scanner scan = new Scanner(System.in);
while(scan.hasNext()){
int n = scan.nextInt();
int stuId[] = new int[n];
for(int i=0; i<n; i++){
stuId[i] = scan.nextInt();
}
for(int i=0; i<n/2; i++){
System.out.println(stuId[i] + " " + stuId[n-i-1]);
}
}
}
}
数组元素的删除

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int [] array=new int[n];
for (int i = 0; i < array.length; i++) {
array[i]=sc.nextInt();
}
int k=sc.nextInt();
for (int i = 0; i < k; i++) {
int flag=sc.nextInt();
for (int j = 0; j < n; j++) {
if(j>=flag-1 && j<=n-2){
array[j]=array[j+1];

            }
        }
        
    }
    for (int i = 0; i < n - k ; i++) {
        if(i==n-k-1) System.out.printf("%d",array[i]);
        else System.out.printf("%d ",array[i]);
    }
}

}

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

import java.util.Scanner;
class stuff{
private String number;
private String name;
private double price;
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
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;
}
stuff(String number, String name,double price ) {//带参的构造方法
this.name = name;
this.number = number;
this.price = price;
}
}
public class Main {

 public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String number = sc.next();//控制输入
    String name = sc.next();
    double price = sc.nextDouble();
    stuff a = new stuff(number, name, price);
    System.out.println(a.getNumber()+","+a.getName()+","+a.getPrice());//调用方法
}

}
矩阵乘数

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
//声明
int [][]matrix = new int [100][100];//矩阵
int hang,lie;
int number;
Scanner scanner = new Scanner(System.in);
//输入
hang = scanner.nextInt();
lie = scanner.nextInt();
for(int i=1;i<=hang;i++){//行
for(int j=1;j<=lie;j++){//列
matrix[i][j] = scanner.nextInt();
}
}
number = scanner.nextInt();
//计算
for(int i=1;i<=hang;i++){//行
for(int j=1;j<=lie;j++){//列
matrix[i][j] *= number;
}
}
//输出
for(int i=1;i<=hang;i++){
for(int j=1;j<=lie;j++){
System.out.print(" " + matrix[i][j]);
}
System.out.println();//换行
}
}
}

END

标签:Java,String,int,System,PTA,out,public,XMUT,Scanner
From: https://www.cnblogs.com/IvanKK/p/17712731.html

相关文章

  • 软件工程 之 (XMUT)软件项目管理—课后习题及答案
    {mtitletitle="软件工程之(XMUT)软件项目管理—课后习题及答案"/}{lamp/}第一章一、填空题1.敏捷模型包括(4)个核心价值,对应(12)个敏捷原则。2.项目管理包括(启动过程组)、(计划过程组)、(执行过程组)、(控制过程组)、(收尾过程组)5个过......
  • 软件工程 之 (XMUT)PTA Java 判断题
    PTAJava判断题随笔,后整理在一个编辑器中编写好的Java应用程序,是扩展名为.class的文件。Fjava语言中不用区分字母的大写小写。FJava语言中,变量名可以用汉字表示。T字节码文件是与平台无关的二进制码,执行时由解释器解释成本地机器码。T一个Java源文件中可以有多个类,但只能有......
  • 软件工程 之 (XMUT)计算机操作系统—计算应用题
    {mtitletitle="软件工程之(XMUT)计算机操作系统-计算应用题"/}{lamp/}一.多道程序设计课堂练习 第1题设内存中有三道程序A、B、C,它们按A、B、C的优先次序执行。它们的计算和I/O操作的时间见下表。假设三道程序使用相同设备进行I/O操作,即程序以串行方式使用设备,试画出单道......
  • 软件工程 之 (XMUT)计算机操作系统—课上习题及答案
    {mtitletitle="软件工程之(XMUT)计算机操作系统—课上习题及答案"/}{lamp/}《操作系统》课堂习题第一章:《操作系统概述》1一个完整的计算机系统应该包括(B)。A、主机、键盘和显示器B、硬件系统和软件系统C、主机和它的外部设备D、系统软件和应用软件.【解析】硬件系......
  • 记录--JavaScript 令人惊讶的一点:对于空数组every()方法返回true
    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助JavaScript语言的内核足够大,导致我们很容易误解它的某些部分是如何工作的。我最近重构了一些使用every()方法的代码,并且发现我并不真正理解every()的逻辑。在我看来,我认为回调函数必须被调用并返回true的时候ev......
  • JAVA基础知识学习
    基础语法基础语法对象:对象(Object)是类的实例类:描述对象的属性和行为方法:封装了特定功能的代码块,可以在程序中被多次调用。方法是类的一部分,用于定义类的行为和功能实例变量:也称为成员变量或字段,是类中声明的变量,它们用于存储对象的状态或属性publicclassHelloMoke......
  • java通过流往外写文字
    1、创建文件写文字,覆盖1Filefile=newFile("文件路径");2FileOutputStreamout=null;3try{4if(!file.exists()){5file.createNewFile();6}7out=newFileOutputStream(file);8......
  • 软件工程 之 (XMUT)Java期末复习题及答案2
    {calloutcolor="#f0ad4e"}软件工程实用案例教程https://www.ivanky.cn/XMUTRG/304.htmlJava期末复习题及答案https://www.ivanky.cn/XMUTRG/361.html计算机网络复习题库https://www.ivanky.cn/XMUTRG/405.html(XMUT)Java期末复习题及答案选择题{collapse}{collapse-i......
  • 软件工程 之 (XMUT)计算机网络复习题库
    {calloutcolor="#f0ad4e"}软件工程实用案例教程https://www.ivanky.cn/XMUTRG/304.htmlJava期末复习题及答案https://www.ivanky.cn/XMUTRG/361.html计算机网络复习题库https://www.ivanky.cn/XMUTRG/405.html计算机网络复习题库考试酷--自测习题{collapse}{/......
  • java安装 系统变量设置
    变量名:JAVA_HOME变量值:C:\ProgramFiles\Java\jdk1.8.0_111变量名:Path变量值:%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;变量名:CLASSPATH变量值:.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;       注意:这前面有一个点‘.’......