1.要求从键盘输入3个数,然后输出其中最大的数!
import java.util.Scanner;
public class t01 {
public static void main(String args[]){
double a[]=new double[3];
double max=0;
for(int i=0;i<=2;i++){
Scanner sr=new Scanner(System.in);
a[i]=sr.nextDouble();
}
for(int i=0;i<=2;i++){
if(a[i]>max)
max=a[i];
}
System.out.print("max="+max);
}
}
2.找出3-99之间的所有素数!
public class t02 {
public static void main(String args[]){
for(int i=3;i<=99;i++){
for(int j=2;j<=i-1;j++){
if(i%j==0){
break;
}
if(j+1==i){
System.out.println(i);
}
}
}
}
}
3.计算一元二次方程的两个根,首先判断△=b^2-4ac
是否大于等于0,如果无根,则输出提示,如果有根,
则输出两个根的值。ax^2+bx+c=0**
import java.util.Scanner;
public class t03 {
public static void main(String args[]){
double a[]=new double[3];
for(int i=0;i<=2;i++){
Scanner sr=new Scanner(System.in);
a[i]=sr.nextDouble();
}
double deita=(a[1]*a[1])-(4*a[0]*a[2]);
double x1=0;double x2=0;
if (deita>0){
x1=((-a[1])+Math.sqrt(deita))/(2*a[0]);
x2=(a[1]+Math.sqrt(deita))/(2*a[0]);
System.out.println("x1="+x1+";x2="+x2);
}
else if(deita==0){
x1=a[1]/((-2)*a[0]);
System.out.println("x="+x1);
}
else
System.out.println("此方程无解");
}
}
4. e≈2.718282
e=1+1/1!+1/2!+1/3!+1/4!+....
直到最后一项1/n的绝对值小于1e-7为止。
public class t04 {
public static void main(String args[]){
int n=1;double e=0;double jc=1;
do{
jc=jc*n;
e=e+(1/jc);
n++;
}while((1/jc)>0.0000001);
System.out.println(e+1);
System.out.println(n);
}
}
5.编写一个简单的加密处理程序,从键盘输入
一段英文文字,将其中每个字母用其后的第
4个字母代替,最后的字母轮回道前面的字
符,其他符号不变。如:a用代替;z用d代替
import java.util.Scanner;
public class t05 {
public static void main(String args[]){
Scanner sr=new Scanner(System.in);
String a=sr.nextLine();
for(int i=0;a.length() >i;i++){
char c=a.charAt(i);
byte b=(byte)c;
if((b>=65 && b<=90) || (b>=97 &&b<=122))
{if((b>=87 && b<=90) || (b>=120 && b<=122))
System.out.print((char)(b+4-26));
else
System.out.print((char)(b+4));}
else
System.out.print((char)b);
}
}
6.从键盘输入5个任意字符串存到数组中,将数组元素按由小到大排列 。
import java.util.Scanner;
public class t06 {
public static void main(String args[]){
String a[]=new String[5];
for(int i=0;i<=4;i++){
Scanner sr=new Scanner(System.in);
a[i]=sr.nextLine();
}
java.util.Arrays.sort(a);
for(int i=0;i<=4;i++){
System.out.println(a[i]);
}
}
}
7.从键盘输入一段英文文字,然后,将该句反向
输出。如:who are you变成uoy era ohw
import java.util.Scanner;
public class t07 {
public static void main(String args[]){
Scanner sr=new Scanner(System.in);
String s=sr.nextLine();
StringBuffer dest=new StringBuffer(s.length());
for(int k=s.length()-1;k>=0;k--){
dest.append(s.charAt(k));
}
System.out.println(dest.toString());
//第二种方法
String res="";
for(int i=s.length()-1;i>=0;i--){
res=res+s.charAt(i);
}
System.out.println(res);
}
}
8.String s = "Nice to meet you, Jack!"
统计该英文中一共有多少个单词。
public class t08 {
public static void main(String args[]){
String str="Nice to meet you, Jack!";
int n=str.split(" ").length;
System.out.println(n);
}
}
**
9.定义一个 Person类,包括:私有属性姓名、
性别、年龄,(可提供对以上私有属性的set
与get操作方法。)再定义一个Students类,
继承Person类,增加私有属性学号、班级。
显示“张三”,“男” ,19,“17510101”,“175101”**
class Person {
private String username;
private int userage;
private String sex;
public void setName(String name) {
this.username = name;
}
public String getName() {
return this.username;
}
public void setAge(int age) {
this.userage = age;
}
public int getAge() {
return this.userage;
}
Person() {
}
Person(String name, int age,String sex) {
if (age > 0 && age < 150) {
username = name;
userage = age;
sex=sex;
} else {
this.username = name;
this.userage = -1;
this.setSex(sex);
}
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
};
class Student1 extends Person {
private String xh;
private String bj;
Student1(String name, int age, String sex,String xh,String bj) {
this.setName(name);
this.setAge(age);
this.setSex(sex);
this.bj = bj;
this.xh=xh;
}
public String toString() {
return "学生姓名:" + this.getName() + ",年龄:" + this.getAge() + ",性别:"
+ getSex()+",学号:"+this.xh+",班级:"+this.bj;
}
}
public class t09 {
public static void main(String args[]){
Person s1=new Person("张三",19,"男");
Student1 s2=new Student1("张三",19,"男","19510135","195101");
System.out.println(s2.toString());
}
}
10.设计一个类,其功能是完成1-n的求和。
类中包含一个属性(int n);一个方
法:(求1-n的和)。编程:利用该类
实现1-50、1-100的和。
class He{
int n;
int s=0;
He(int n){
this.n=n;
for(int i=1;i<=n;i++){
s=s+i;
}
}
public String toString() {
return "和为:" + s ;
}
}
public class t10 {
public static void main(String args[]){
He s1=new He(50);
He s2=new He(100);
System.out.println(s1.toString());
System.out.println(s2.toString());
}
}
11.定义接口Shape,其中包括一个方法size(),设计“矩形”、“圆”
等类实现Shape接口,其size()方法分别表示计算矩形面积、圆
面积。分别创建代表“矩形”、“圆” 的两个对象存入一个Shape
类型的数组中,通过调用size()方法将数组中各类图形的面积大
小输出。
备注:利用该类实现(边长=5,”矩形”,面积)(半径=2,”圆”,面积)
abstract class Shape { //定义抽象类
abstract public double area(); //抽象方法
}
class Rectangle extends Shape { //定义矩形
private double width, height;
public Rectangle(double j, double k) {
width = j; height = k;
}
public double area() {
return width * height;
}
public String toString() {
return "矩形,长:" + width+",高:"+height + ",面积:" + area();
}
}
class Circle extends Shape { //定义圆
private double r;
public Circle(double r) {
this.r = r;
}
public double area() {
return 3.14 * r * r;
}
public String toString() {
return "圆,半径:" + r + ",面积:" + area();
}
}
public class t11{
public static void main(String args[]) {
Shape s[] = new Shape[2];
s[0] = new Rectangle(15,20);
s[1] = new Circle(47);
for (int k= 0; k < s.length; k ++)
System.out.println( s[k].toString());
}
}
12.定义一个抽象类-水果,其中包括总价total()抽象方法
,编写程序分别创建苹果、橘子两个类,创建两个水果对
象存放在一个水果类型的数组中,输出数组中所有水果
的类型、重量以及总价。
备注:利用该类实现(”苹果”,2,总价)、(”橘子”,5,总价)
abstract class Fruit{
abstract public double total();
}
class Apple extends Fruit{
double weight;double zj;
public Apple(double weight,double zj){
this.weight=weight;
this.zj=zj;
}
public double total() {
// TODO 自动生成的方法存根
return zj*weight;
}
public String toString() {
return "苹果,重量:" + weight + ",总价:" + total();
}
}
class Orange extends Fruit{
double weight;double zj;
public Orange(double weight,double zj){
this.weight=weight;
this.zj=zj;
}
public double total() {
// TODO 自动生成的方法存根
return zj*weight;
}
public String toString() {
return "橘子,重量:" + weight + ",总价:" + total();
}
}
public class t12 {
public static void main(String args[]){
Fruit s[]=new Fruit[2];
s[0]=new Apple(2,5);
s[1]=new Orange(3,4);
System.out.println( s[0].toString() );
System.out.println( s[1].toString() );
}
}
13.编写一个代表三角形的类。其中,三条边为三角型的
属性,并封装有求三角形的面积和周长的方法。并设
计构造方法和toString() 方法。
分别针对三条边为3,4,5和7,8,9的两个三角形
进行测试。
class Triangle {
double x,y,z;
public Triangle(double x, double y, double z) {
super();
this.x = x;
this.y = y;
this.z = z;
}
public double area(){
double p=(x+y+z)/2;
return Math.sqrt(p*(p-x)*(p-y)*(p-z));
}
public double girth(){
return x+y+z;
}
public String toString() {
return "面积:"+area()+"周长:"+girth();
}
}
public class t13 {
public static void main(String args[]){
Triangle s1=new Triangle(3,4,5);
Triangle s2=new Triangle(7,8,9);
System.out.println(s1.toString());
System.out.println(s2.toString());
}
}