java例题

 public class t1 {
public static void main(String[] args) {
int total=0;
for(int i=1;i<100;i=i+2)
{
total=total+i;
}
System.out.println("1到100以内奇数和为:"+total);
}

*******************************************************************************


  package com.test1;
import java.io.*;
public class Test2 {
public static void main(String[] args)
{
float y = 0;

try{
InputStreamReader isr=new
InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
System.out.println("传入x值");
//从控制台读取一行数据
String a1 = br.readLine();
// 把 String 转为 int
float x=Float.parseFloat(a1);
if(x>0)
{
y=x+3;
}else if(x==0)
{
y=0;
}else if(x<0)
{
y=x*x-1;
}
System.out.println(y);
}catch(Exception e){
e.printStackTrace();
}
}


}
*****************************************************
import java.util.Scanner;
public class Test2_1 {

public static double function(double x) {
if (x > 0) {
return x + 3;
} else if (x == 0) {
return 0.0;
} else {
return  x * x - 1;
}

}

public static void main(String args[]) {
Scanner s=new Scanner(System.in);
double x=s.nextInt();
double y = function(x);
System.out.println(y);
}


}
******************************************************************************************
import java.io.*;
public class Test3{


public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
FileInputStream fis=new FileInputStream("D:/t1.txt");
FileOutputStream fos=new FileOutputStream("D:/t2.txt");
BufferedInputStream bis=new BufferedInputStream(fis);
BufferedOutputStream bos=new BufferedOutputStream(fos);
int ch=0;
while((ch=bis.read())!=-1)
bos.write(ch);
bos.flush();
bis.close();
bos.close();
}


}
***************************************************************************************
  import java.io.*;
  public class Test4{
public static void main(String[] args) {
try {
String encoding="GBK";
File file=new File("D:/t1.txt");
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
System.out.println(lineTxt);
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
  
***************************************************************************************
import java.util.Scanner;
public class Test5 {

public static  int getElement(int index)
{
int a[]= {12,45,34,46,23};
return a[index];

}
public static void main(String[] args)throws Exception {
System.out.println("请输入数组下标");
Scanner s=new Scanner(System.in);
int i=s.nextInt(); //获取键盘的输入
System.out.println("数组下标对应得值为"+getElement(i));
}
}
**********************************************************************************
public class Test6 {


public static void main(String[] args)
throws Exception
{
int a[]={48,45,34,46,23};
System.out.println("最小元素下标为"+minIndex(a));
}
public static int minIndex(int a[])
{
int min=a[0];
int minIndex=0;
for(int i=1;i<a.length;i++)
{
if(min>a[i])
{
min=a[i];
minIndex=i;
}
}
return minIndex;
}
}
*************************************************************************************
class NoThisSongException extends Exception{  
   public NoThisSongException(){  
       super();  
   }  
   public NoThisSongException(String message){  
       super(message);  
   }  
}  
class Player{  
   public void play(int index)throws NoThisSongException{  
       if(index>10){  
           throw new NoThisSongException("您播放的歌曲不存在");  
       }  
       System.out.println("正在播放歌曲");  
   }  
}  
public class Test7 {  
   public static void main(String[] args) {  
       Player player = new Player();  
       try {  
           player.play(13);  
       } catch (NoThisSongException e) {  
           System.out.println("异常信息为: "+e.getMessage());  
       }  
   }  
}  
**************************************************************************************
import java.util.Scanner;
public class Test8 {
public static void main(String args[]){
System.out.println("请输入一个数小于5位的正整数");
Scanner s=new Scanner(System.in);
long temp=s.nextLong();
if(temp>99999){
System.exit(1);
}
String str=String.valueOf(temp);
char c[]=str.toCharArray();
System.out.println("逆序输出为:");
for(int j=c.length-1;j>=0;j--){
System.out.print(c[j]);
}
}


}


***************************************************************************************
import java.util.Scanner;
public class Test9{
public static void main(String[] args) {
int mouth;
do{
System.out.println("请输入月份:");
mouth=new Scanner(System.in).nextInt();
switch(mouth){
case 0:System.out.println("一月");break;
case 1:System.out.println("二月");break;
case 2:System.out.println("三月");break;
case 3:System.out.println("四月");break;
case 4:System.out.println("五月");break;
case 5:System.out.println("六月");break;
case 6:System.out.println("七月");break;
case 7:System.out.println("八月");break;
case 8:System.out.println("九月");break;
case 9:System.out.println("十月");break;
case 10:System.out.println("十一月");break;
case 11:System.out.println("十二月");break;
default:System.out.println("请重新输入月份");break;
}
}while (mouth>=0&&mouth<=11);
}
}
****************************************************************************************
 class Student{
String name;
double grade;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
public Student()
{

}
public Student(String name,double grade)
{
this.name=name;
this.grade=grade;
}

}


public class Test10 {
public static void main(String[] args)
{
Student s1=new Student();
s1.setName("陈");

s1.setGrade(80);
Student s2=new Student("chen",90);

}
}


***************************************************************************************
class Student1 {
public String name;
public int age;
public Student1(String name,int age){
this.name=name;
this.age=age;
}
public void show(){
System.out.println("name: "+name+" age: "+age);
}
}
class UnderGraduate extends Student1{
public String degree;
public UnderGraduate(String name,int age,String degree){
super(name, age);
this.degree=degree;
}
public void show(){
System.out.println("name: "+name+" age: "+age+" degree: "+degree);
}
}
public class Test11{
public static void main(String[] args) {
Student1 student = new Student1("chen", 16);
student.show();
UnderGraduate underGraduate = new UnderGraduate("hui", 20, "bechalor");
underGraduate.show();
}
}
****************************************************************************************
 interface Shape{
abstract double area(double x);
}
class Square implements Shape{



public double area(double x) {

return x*x;
}

}
class Circle implements Shape{



public double area(double r) {

return Math.PI*r*r;
}

}



public class Test12 {
  public static void main(String[] args)
  {    
   Shape s= new Square();
  Shape circle = new Circle();
System.out.println("正方形面积是"+s.area(2));
System.out.println("圆形面积是"+circle.area(3));





  }
}
***********************************************************************************


class People
{
String name;
int age;
public People()
{
this.name=name;
this.age=age;
}

}
class Student2 extends People
{
String number;

public Student2()
{
super();
}

public Student2(String name,int age,String number)
{
super();
this.number=number;
}

public void choose(){ 
System.out.println("我没有选课"); 

public void choose(String choose){ 
System.out.println("我所选课程为"+choose);   


}

}


public class Test13 {


public static void main(String[]args){ 
Student2 s3 = new Student2(); 
s3.choose();
s3.choose("java");
  }


}


*************************************************************************************
class Rectangle{
double length;
double width;
double height;
public Rectangle(double length,double width,double height)
{
this.length = length;
this.width = width;
this.height=height;
}
public double computeVolume()
{
return this.length*this.height*this.width;
}
}
 class Cuboid extends Rectangle{
double weight;
public Cuboid(double length,double width,double height,double weight)
{
super(length,width,height);
this.weight=weight;
}
public double computeArea()
{
return (this.length*this.width+this.length*this.height+this.width*this.height)*2;
}
 
 
 }


public class Test14 {
public static void main(String[] args)
{
Rectangle r1=new Rectangle(10,10,10);
System.out.println("长方体体积为"+r1.computeVolume());
Cuboid r2=new Cuboid(10,10,10,10);
System.out.println("长方体表面积为"+r2.computeArea());

}


}
*****************************************************************************
public class Test15 {

public static void main(String[] args) {
int n=0;
int i=0;
for(n=1;n<=100;n++){
if(n % 3!=0) //不能被 3整除,结束本次循环
continue;//跳出循环
i++;
System.out.print(n+" ");
if(i % 5==0)
{ //每五个数一行 
System.out.println();
}
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值