前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >一些简单的java代码

一些简单的java代码

作者头像
用户7886150
修改2021-02-03 10:37:36
6160
修改2021-02-03 10:37:36
举报
文章被收录于专栏:bit哲学院

参考链接: Java简单程序

1.

 public class Beersong {

 public static void main (String[] args){

int beernum =99;

String word = "bottle";

while (beernum>0){

if (beernum == 1){

word = "bootle";

}

System.out.print(beernum+""+word+"of beer on the wall");

System.out.println(beernum+""+"of beer");

System.out.println("Take one down.");

System.out.println("passit around.");

beernum = beernum -1;

   if (beernum>0); {

 System.out.println(beernum+""+"of beer on wall");

   }

   {

 System.out.println("No more bottles of beer on the wall");

   }

 }

 }         

2.class Books{ String title; String author; } class BookTestDrive { public static void main(String args[]){ Books[] myBooks = new Books[3]; int x = 0; //创建books对象 myBooks[0]=new Books(); myBooks[1]=new Books(); myBooks[2]=new Books();  myBooks[0].title="The Grapes of Java"; myBooks[1].title="The Java Gatsby"; myBooks[2].title="The Java Cookbook";  myBooks[0].author="bob"; myBooks[1].author="sue"; myBooks[2].author="ian";  while(x<3){ System.out.println(myBooks[x].title); System.out.println("by"); System.out.println(myBooks[x].author); x=x+1; } }   } 

3.class Cat{ int size;  String name; void bark(){ if(size>60){ System.out.println("Woof!Woof!"); }else if(size>14){ System.out.println("Ruff!Ruff!"); }else{ System.out.println("Yip!Yip!"); }  } } class DogTestDrive { public static void main(String[] args){ Cat one = new Cat(); one.size = 70; Cat two = new Cat(); two.size = 8; Cat three =  new Cat(); three.size = 35;  one.bark(); two.bark(); three.bark(); } } 

4.//编写类 class Movie{ //实例对象 String title; String genre; int rating; //方法 void playIt(){     System.out.println("Playing the movie");   } } public class MovieTestDrive { //编写测试用的类    public static void main(String[] args){   //建立movie对象   Movie one = new Movie();   //圆点运算符引用   one.title = "Gone with the Stock";   one.genre = "Tragic";   one.rating = -2;      //建立movie对象   Movie two = new Movie();   two.title = "Lost in Cubicle Space";   two.genre = "Comedy";   two.rating = 5;  //调用方法   two.playIt();       }   } 

5.   public class SimpleDotCom { int[] locationCells; int numOfHits = 0;   public void setLocationCells(int[] locs){ locationCells = locs; }   public String checkYourself(String StringGuess) { int guess = Integer.parseInt(StringGuess); String result = "miss"; for (int cell:locationCells){ if (guess == cell){ result = "hit"; numOfHits++; break; } } if(numOfHits == locationCells.length){ result = "kill"; } System.out.println(result); return result; }   } 

  public class SimpleDotComTestDrive {   public static void main(String[] args){  //初始化一个SimpleDotCom对象  SimpleDotCom dot = new SimpleDotCom();  //创建带有dot com位置的数组  int[] locations = {2,3,4};  //调用dot com 的setter  dot.setLocationCells(locations);  //假的猜测  String userGuess = "2";  //调用被测方法并传入假的数据  String result = dot.checkYourself(userGuess);     } }

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档