1.求数组中一个值等于另外两个元素的和的数的个数
已知一个整数集合内有n个元素(3<=n<=100),每个元素的范围是[1,10000],且元素之间互不相同。这些元素中可能存在这样的数:其值等于另外两个元素的和。
请你编写Java程序,求出满足上述条件的数的个数,并输出。
输入:n和n个互不相同的整数
输出:满足条件的数的个数
样例输入:
5 1 2 3 4 5
样例输出:
3
package com.ts.test;
import java.util.Scanner;
public class Main1 {
public static void main(String[] args) {
Scanner reader=new Scanner(System.in);
int n,i=0,j=0,cot=0,k=0,n1,n2;
n=reader.nextInt();
//输入数组a[n]的句式:
int a[]=new int[n];//定义一个长度为n的整形数组,注:定义中的n必须为确定的常数。
for(i=0;i<n;i++) {
a[i]=reader.nextInt();
}
//获得数组a中任意两个数的和b[]:
int b[]=new int [100000];
for(i=0;i<n;i++) {
for(j=i+1;j<n;j++) {
b[k++]=a[i]+a[j];
}
}
//删除b中重复的数组:
n1=k;
for(i=0;i<n;i++) {
for(j=i+1;j<n;j++) {
if(b[i]==b[j]) {
for(k=j;k<n1-1;k++) {
b[k]=b[k+1];
}n1--;j--;
}
}
}
//求数组a和数组b中相同的数字个数:
for(i=0;i<n1;i++) {
for(j=0;j<n;j++) {
if(b[i]==a[i])cot++;
}
}
System.out.println(cot);
}
}
2.设计一个表示书本的Book类,要求如下:
1.私有化的数据成员包括书名,出版社名,作者(可能有多个,采用字符串数组),价格,页数。
2. 有三个构造方法:(1)空构造方法;(2)初始化书名,出版社名和作者三个数据成员的构造方法;(3)初始化所有数据成员的构造方法。利用this关键字区别形参和数据成员。
3.写出所有的setter和getter方法来读取和设置私有化的数据成员。利用this关键字引用数据成员
4. 功能方法(模拟输出):(1)输出书名、出版社名和作者的方法;(2)输出书籍所有信息的方法;(3)翻开到某页的方法;(4)在某页做笔记的方法。
补充说明:对Book类测试要求如下:
1. 有3本书需要使用构造方法初始化书名,出版社名和作者,然后利用setter方法设置价格和页数。
2. 输出这3本书的所有信息,并统计这3本书的总价格。
3. 在一本书的第289页做笔记,笔记内容是“垃圾分类,从我做起”。
//没有写完:
import java.util.Scanner;
public class Main1 {
public static void main(String[] args) {
Scanner reader=new Scanner(System.in);
booksManager sm=new Scanner(System.in);//创造一个管理类对象sm
sm.print();
sm.shezhi();
}
}
class booksManager{//创建管理类,管理书shu的相关信息
book[] book =new book[1000];//创建“管理类”的成员变量:数组
Scanner reader =new Scanner(System.in);
int n=reader.nextInt();//有n本书
booksManager(Scanner reader){//对数组book赋值:
for(int i=0;i<n;i++) {
book[i]=new book();
String num =reader.next();
String name=reader.next();
String sex=reader.next();
String press=reader.next();
book[i].setBookname(num);
book[i].setPress(name);
book[i].setAuthor(press);
}
}
//功能1.输出书名、出版社名和作者的方法;
void print() {
Scanner reader=new Scanner(System.in);
int n=reader.nextInt();
System.out.prinntf("%s %s %s",book[n].getBookname(),book[n].getAuthor(),book[n].getPress());
}
//设置类:
void shezhi() {
Scanner reader =new Scanner(System.in);
for(int i=0;i<n;i++) {
book[i].setPrice(reader.nextDouble());
int n1=reader.nextInt();//更改
book[i].Gai(n1);
}
}
}
//书本的book类:
class book{
//私有化数据成员:书名,出版社press,作者author,价格,页数
private String bookname;
private String press;
private String author;
private double price;
private int page;
private String f[]=new String[100];
public void Gai(int n) {
Scanner reader=new Scanner(System.in);
f[n]=reader.next();
}
//etter和getter方法来读取和设置私有化的数据成员:
public void setPage(int page) {
this.page=page;
}
public int getPage() {
return page;
}
public void setPrice(double price) {
this.price=price;
}
public double getPrice() {
return price;
}
public void setBookname(String bookname) {
this.bookname=bookname;
}
public String getBookname() {
return bookname;
}
public void setPress (String press) {
this.press=press;
}
public String getPress() {
return press;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author=author;
}
}
3.设计一个表示二维平面上的点的类 Point,包含表示坐标位置的 protected 类型的成员变量 x 和 y,以及获取和设置 x 和 y 的值的 public 类型的方法: getx()、gety()、setx()、sety()。Point 类的构造方法要有多种形式。
import java.util.*;//Scanner 类在这个包里
class Point{
protected float x,y; //定义 Circle 类的坐标
protected Scanner scan=new Scanner(System.in); //设一个Scanner 对象 scan,指定输入方式是从键盘读入
//无参构造 :
public Point(){
x=0;y=0;
}
//有参构造:
public Point(float x,float y){
this.x=x;
this.y=y;
}
//设置X:
public void setx(){
x=scan.nextFloat();
}
//设置y:
public void sety() {
y=scan.nextFloat();
}
// 获取x:
public float getx(){
return this.x;
}
//获取y:
public float gety(){
return this.y;
}
}
4.BMI=weight/hight^2:
接收用户输入的身高和体重,将判断结果输出
过轻:低于18.5
正常:18.5 ~ 22.9
偏胖:23 ~ 24.9
肥胖:25 ~ 29.9
重度肥胖:高于30
极度肥胖:高于40
package second;
import java.util.Scanner;
public class Main1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner reader=new Scanner(System.in);
//创建成员变量height weight
double height=reader.nextDouble();
double weight=reader.nextDouble();
//调用getBMI()方法
getBMI(height,weight);
}
public static void getBMI(double h,double w) {
//求BIM指数:
double bmi=w/(h*h);
//定义一个变量r保存最终结果
String r=" ";
//求结果,给r赋值
if (bmi < 18.5) {
r = "过轻";
} else if (bmi <= 22.9) {
r = "正常";
} else if (bmi <= 24.9) {
r = "偏胖";
} else if (bmi <= 29.9) {
r = "肥胖";
} else if (bmi <= 40) {
r = "重度肥胖";
} else {
r = "极度肥胖";
}
//打印最终结果
System.out.println("您的BMI指数:" + bmi);
System.out.println("您的体重属于:" + r);
}
}
5.求最大公约数和最大公倍数:
package second;
import java.util.Scanner;
public class Main1 {
public static void main(String[] args) {
Scanner reader=new Scanner(System.in);
int a=reader.nextInt();
int b=reader.nextInt();
int m=zdgys(a,b);
long n=zxgbs(a,b);
System.out.println("最大公约数是"+m+","+"最小公倍数是:"+n);
}
private static int zdgys(int a,int b) {
int min=a<b?a:b;
for(int i=min;i>=1;i--) {
if(a%i==0&&b%i==0)return i;
}
//这句代码根本不会执行,让编译可以通过
return 0;
}
private static long zxgbs(int a,int b) {
int max=a>b?a:b;
for(long i=max; ;i++) {
if(i%a==0&&i%b==0)return i;
}
}
}
6.输入数量不确定的【0,9】范围内的整数,统计每一种数字出现次数:
package second;
import java.util.*;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a[]=new int[10];
int []x1=new int[10];
int x;
for(int i=0;i<10;i++) {
x1[i]=sc.nextInt();
x=x1[i];
if(x!=-1) {
if(x>=0&&x<=9) {
a[x]++;
}
}
}
for(int i1=0;i1<a.length;i1++) {
System.out.println(i1+":"+a[i1]);
}
}
}
7编写一个程序,实现下列功能:如果文件file.txt不存在,就创建一个名为file.txt的文件。根据用户输入的操作选项“1 2 3”,执行相应的追加数据、重写数据、读取文件内容;
(1)写数据:使用字符流将用户输入的内容写入file.txt中;
(2)每一输入写操作(1或者2),后面一定会有持续的内容输入,写入到文件中
(3)读数据:使用字符流将读取的数据打印输出;
(4)每次用户仅仅执行一项操作
import java.io.*;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner reader=new Scanner(System.in);
File file =new File("file.txt");
if(!file.exists()) {
try {
file.createNewFile();
}
catch(IOException e) {
e.printStackTrace();
}
}
int n=reader.nextInt();
if(n==1)
{
FileOutputStream out=new FileOutputStream(file);
out=new FileOutputStream(file,true);
String s=reader.next();
byte a[]=new byte[1000];
a=s.getBytes();
out.write(a,0,a.length);
out.close();
}
if(n==2) {
FileOutputStream out=new FileOutputStream(file);
out=new FileOutputStream(file,false);
String s=reader.next();
byte a[]=new byte[1000];
a=s.getBytes();
out.write(a,0,a.length);
out.close();
}
if(n==3) {
FileReader fis=new FileReader("file.txt");
System.out.print("文件file.txt的内容为:");
int i=1;
while((i=fis.read())!=-1){
System.out.print((char)i);
}
}
}
}