本文目录一览:
- 1、Java程序,100行左右,需要运行成果图。 程序随便。 2个有追加
- 2、java程序100行
- 3、求个简单点的Java程序 100行左右。 需要解释。
- 4、随便给个100行左右的java小程序,就给分,作业,急
- 5、java程序:编写生成100个1-6之间的随机数,统计1—6每个出现的概率,菜鸟看不懂程序
Java程序,100行左右,需要运行成果图。 程序随便。 2个有追加
200多行,程序功能是输入两个数字的英文单词(20以内),输出结果,这个结果也是一个英文(就是将结果转换成英文输出)
import java.util.Scanner;
public class test{
private static String temp, tempNumA, tempNumB;
private static int geWei, shiWei, baiWei, judgeA, judgeB;
private static int number(String inputNum) {
if (inputNum.equalsIgnoreCase("zero")) {
return 0;
} else if (inputNum.equalsIgnoreCase("one")) {
return 1;
} else if (inputNum.equalsIgnoreCase("two")) {
return 2;
} else if (inputNum.equalsIgnoreCase("three")) {
return 3;
} else if (inputNum.equalsIgnoreCase("four")) {
return 4;
} else if (inputNum.equalsIgnoreCase("five")) {
return 5;
} else if (inputNum.equalsIgnoreCase("six")) {
return 6;
} else if (inputNum.equalsIgnoreCase("seven")) {
return 7;
} else if (inputNum.equalsIgnoreCase("eigth")) {
return 8;
} else if (inputNum.equalsIgnoreCase("nine")) {
return 9;
} else if (inputNum.equalsIgnoreCase("ten")) {
return 10;
} else if (inputNum.equalsIgnoreCase("eleven")) {
return 11;
} else if (inputNum.equalsIgnoreCase("twelve")) {
return 12;
} else if (inputNum.equalsIgnoreCase("thirteen")) {
return 13;
} else if (inputNum.equalsIgnoreCase("fourteen")) {
return 14;
} else if (inputNum.equalsIgnoreCase("fifteen")) {
return 15;
} else if (inputNum.equalsIgnoreCase("sixteen")) {
return 16;
} else if (inputNum.equalsIgnoreCase("seventeen")) {
return 17;
} else if (inputNum.equalsIgnoreCase("eighteen")) {
return 18;
} else if (inputNum.equalsIgnoreCase("nineteen")) {
return 19;
} else if (inputNum.equalsIgnoreCase("twenty")) {
return 20;
} else {
return -1;
}
}
private static String lessTen(int num) {
switch (num) {
case 0:
temp = "zero";
break;
case 1:
temp = "one";
break;
case 2:
temp = "two";
break;
case 3:
temp = "three";
break;
case 4:
temp = "four";
break;
case 5:
temp = "five";
break;
case 6:
temp = "six";
break;
case 7:
temp = "seven";
break;
case 8:
temp = "eight";
break;
case 9:
temp = "nine";
break;
default:
temp = "erro";
break;
}
return temp;
}
private static String moreThanTen(int num) {
if (num 20) {
switch (num) {
case 10:
temp = "ten";
break;
case 11:
temp = "eleven";
break;
case 12:
temp = "twelve";
break;
case 13:
temp = "thirteen";
break;
case 14:
temp = "fourteen";
break;
case 15:
temp = "fifteen";
break;
case 16:
temp = "sixteen";
break;
case 17:
temp = "seventeen";
break;
case 18:
temp = "eighteen";
break;
case 19:
temp = "nineteen";
break;
}
} else if (num = 20) {
switch (num) {
case 20:
temp = "twenty";
break;
case 30:
temp = "thirty";
break;
case 40:
temp = "forty";
break;
case 50:
temp = "fifty";
break;
case 60:
temp = "sixty";
break;
case 70:
temp = "seventy";
break;
case 80:
temp = "eighty";
break;
case 90:
temp = "ninety";
break;
}
}
return temp;
}
private static void outputReult(int num) {
if (num = 0 num = 9) {
System.out.println(lessTen(num));
} else if (num = 10 num = 19) {
System.out.println(moreThanTen(num));
} else if (num = 20 num = 99) {
shiWei = num / 10;
geWei = num % 10;
if (geWei == 0) {
System.out.println(moreThanTen(shiWei * 10));
} else {
System.out.println(moreThanTen(shiWei * 10) + "-"
+ lessTen(geWei));
}
} else if (num = 100 num = 400) {
baiWei = num / 100;
shiWei = num % 100 / 10;
geWei = num % 100 % 10;
if (shiWei == 0 geWei == 0) {
System.out.println(lessTen(baiWei) + " " + "hundred");
} else if (shiWei == 0 geWei != 0) {
System.out.println(lessTen(baiWei) + " " + "hundred" + " "
+ "and" + " " + lessTen(geWei));
} else if (shiWei != 0 geWei == 0) {
System.out.println(lessTen(baiWei) + " " + "hundred" + " "
+ "and" + " " + moreThanTen(shiWei * 10 + geWei));
} else {
if (shiWei == 1 geWei != 0) {
System.out.println(lessTen(baiWei) + " " + "hundred" + " "
+ "and" + " " + moreThanTen(shiWei * 10 + geWei));
} else {
System.out.println(lessTen(baiWei) + " " + "hundred" + " "
+ "and" + " " + moreThanTen(shiWei * 10) + "-"
+ lessTen(geWei));
}
}
}
}
public static void inputNumber() {
System.out.print("请输入第一个数字(数字必须小于等于20)的单词:");
tempNumA = new Scanner(System.in).next();
judgeA = number(tempNumA);
System.out.print("请输入第二个数字(数字必须小于等于20)的单词:");
tempNumB = new Scanner(System.in).next();
judgeB = number(tempNumB);
if (judgeA == -1 || judgeB == -1) {
System.out.println("你输入的数字单词不正确,请重新输入!");
System.out.println(" ");
inputNumber();
}
}
public static void main(String[] args) {
inputNumber();
System.out.print(tempNumA + " multiply " + tempNumB + " equals ");
outputReult(number(tempNumA) * number(tempNumB));
}
}
java程序100行
package soyea.base.util;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import soyea.base.bean.physics.ChargeSite;
/**
* 反射类,获取对象属性值,设置对象属性值等操作
*/
public class ClassReflect {
/**
* 用于获取指定对象不确定属性名的值 反射机制
* @param object 实体对象
* @param str 属性名
* @return 属性值
* @throws Exception
*/
public static Object get(Object object,String str) throws Exception {
Field field = hasEqualsIgnoreCase(object.getClass(),str);
if(field != null){
Method m = object.getClass().getMethod("get"+getMethodName(str));
Object invoke = m.invoke(object);
if(invoke == null){
return null;
}else{
return invoke.toString();
}
}else{
return null;
}
}
/**
* 设置对象属性
* @param object
* @param str
* @return
* @throws Exception
*/
public static void set(Object object,String property,Object value) throws Exception {
Field field = hasEqualsIgnoreCase(object.getClass(),property) ;
if(field != null){
Method m = object.getClass().getDeclaredMethod("set"+getMethodName(property),field.getType());
if(value!=null){
m.invoke(object,value);
}
}
}
/**
* @param clz 类文件
* @param str 类中的属性名
* @return 属性在liest中的下标
* @throws Exception
*/
public static Field hasEqualsIgnoreCase(Class? clz,String str) throws Exception{
ListField fields = Arrays.asList(clz.getDeclaredFields());
for(int i=0;ifields.size();i++){
Field x = fields.get(i);
if(str.equalsIgnoreCase(x.getName().toString())){
return x;
}
}
return null;
}
/**
* 大写第一个字母
* @param str 字段名
* @return
* @throws Exception
*/
public static String getMethodName(String str) throws Exception{
byte[] b = str.getBytes();
b[0]=(byte)((char)b[0]-'a'+'A');
return new String(b);
}
public static void resetObj(Object obj, String property)
throws Exception {
String gbk = Encoder.getByCode(obj, property);
if(gbk != null gbk.trim().length()0){
try {
ClassReflect.set(obj, property, gbk);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void resetAllObjPropertiesByCode(Object obj)
throws Exception {
Field[] fields = obj.getClass().getDeclaredFields();
for(int i=0;ifields.length;i++){
Field f = fields[i];
String name = f.getName();
if("id".equals(name)){
continue;
}
resetObj(obj, name);
}
}
}
求个简单点的Java程序 100行左右。 需要解释。
贪吃蛇游戏 望采纳
import java.awt.Button;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Snake extends JFrame implements KeyListener{
int Count=0;
Button[][] grid = new Button[20][20];
ArrayListPoint snake_list=new ArrayListPoint();
Point bean=new Point(-1,-1);//保存随机豆子【坐标】
int Direction = 1; //方向标志 1:上 2:下 3:左 4:右
//构造方法
public Snake()
{
//窗体初始化
this.setBounds(400,300,390,395);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout f=new GridLayout(20,20);
this.getContentPane().setBackground(Color.gray);
this.setLayout(f);
//初始化20*20个按钮
for(int i=0;i20;i++)
for(int j=0;j20;j++)
{
grid[i][j]=new Button();
this.add(grid[i][j]);
grid[i][j].setVisible(false);
grid[i][j].addKeyListener(this);
grid[i][j].setBackground(Color.blue);
}
//蛇体初始化
grid[10][10].setVisible(true);
grid[11][10].setVisible(true);
grid[12][10].setVisible(true);
grid[13][10].setVisible(true);
grid[14][10].setVisible(true);
//在动态数组中保存蛇体按钮坐标【行列】信息
snake_list.add(new Point(10,10));
snake_list.add(new Point(11,10));
snake_list.add(new Point(12,10));
snake_list.add(new Point(13,10));
snake_list.add(new Point(14,10));
this.rand_bean();
this.setTitle("总分:0");
this.setVisible(true);
}
//该方法随机一个豆子,且不在蛇体上,并使豆子可见
public void rand_bean(){
Random rd=new Random();
do{
bean.x=rd.nextInt(20);//行
bean.y=rd.nextInt(20);//列
}while(snake_list.contains(bean));
grid[bean.x][bean.y].setVisible(true);
grid[bean.x][bean.y].setBackground(Color.red);
}
//判断拟增蛇头是否与自身有碰撞
public boolean is_cross(Point p){
boolean Flag=false;
for(int i=0;isnake_list.size();i++){
if(p.equals(snake_list.get(i) )){
Flag=true;break;
}
}
return Flag;
}
//判断蛇即将前进位置是否有豆子,有返回true,无返回false
public boolean isHaveBean(){
boolean Flag=false;
int x=snake_list.get(0).x;
int y=snake_list.get(0).y;
Point p=null;
if(Direction==1)p=new Point(x-1,y);
if(Direction==2)p=new Point(x+1,y);
if(Direction==3)p=new Point(x,y-1);
if(Direction==4)p=new Point(x,y+1);
if(bean.equals(p))Flag=true;
return Flag;
}
//前进一格
public void snake_move(){
if(isHaveBean()==true){//////////////有豆子吃
Point p=new Point(bean.x,bean.y);//【很重要,保证吃掉的是豆子的复制对象】
snake_list.add(0,p); //吃豆子
grid[p.x][p.y].setBackground(Color.blue);
this.Count++;
this.setTitle("总分:"+Count);
this.rand_bean(); //再产生一个豆子
}else{///////////////////无豆子吃
//取原蛇头坐标
int x=snake_list.get(0).x;
int y=snake_list.get(0).y;
//根据蛇头坐标推算出拟新增蛇头坐标
Point p=null;
if(Direction==1)p=new Point(x-1,y);//计算出向上的新坐标
if(Direction==2)p=new Point(x+1,y);//计算出向下的新坐标
if(Direction==3)p=new Point(x,y-1);//计算出向左的新坐标
if(Direction==4)p=new Point(x,y+1);//计算出向右的新坐标
//若拟新增蛇头碰壁,或缠绕则游戏结束
if(p.x0||p.x19|| p.y0||p.y19||is_cross(p)==true){
JOptionPane.showMessageDialog(null, "游戏结束!");
System.exit(0);
}
//向蛇体增加新的蛇头坐标,并使新蛇头可见
snake_list.add(0,p);
grid[p.x][p.y].setVisible(true);
//删除原蛇尾坐标,使蛇尾不可见
int x1=snake_list.get(snake_list.size()-1).x;
int y1=snake_list.get(snake_list.size()-1).y;
grid[x1][y1].setVisible(false);
snake_list.remove(snake_list.size()-1);
}
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_UP Direction!=2) Direction=1;
if(e.getKeyCode()==KeyEvent.VK_DOWN Direction!=1) Direction=2;
if(e.getKeyCode()==KeyEvent.VK_LEFT Direction!=4) Direction=3;
if(e.getKeyCode()==KeyEvent.VK_RIGHT Direction!=3) Direction=4;
}
@Override
public void keyReleased(KeyEvent e) { }
@Override
public void keyTyped(KeyEvent e) { }
public static void main(String[] args) throws InterruptedException {
Snake win=new Snake();
while(true){
win.snake_move();
Thread.sleep(300);
}
}
}
随便给个100行左右的java小程序,就给分,作业,急
package com.erayt.ctms.Zimport;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.collections.functors.ForClosure;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.erayt.ctms.system.standard.xnet.CtmsXNetContext;
import com.erayt.xnet4j.XNetCommand;
import com.erayt.xnet4j.XNetData;
public class ReadForwardExcelServiceImpl implements ReadForwardExcelService {
private SearchPosDao searchForwardDao;
public SearchPosDao getSearchForwardDao() {
return searchForwardDao;
}
public void setSearchForwardDao(SearchPosDao searchForwardDao) {
this.searchForwardDao = searchForwardDao;
}
protected Logger logger = LoggerFactory
.getLogger(this.getClass().getName());
private Workbook wb;
// private JSONObject jsonObject = new JSONObject();
private ListObject list_key = new ArrayListObject();
private ListObject list_body = new ArrayListObject();
private ListHashMapString,Object list_map = new ArrayListHashMapString,Object();
private CtmsXNetContext ctmsXNetContext;
private String filename3;
public String getFilename3() {
return filename3;
}
public void setFilename3(String filename3) {
this.filename3 = filename3;
}
public String getFilename4() {
return filename4;
}
public void setFilename4(String filename4) {
this.filename4 = filename4;
}
private String filename4;
public void setCtmsXNetContext(CtmsXNetContext ctmsXNetContext) {
this.ctmsXNetContext = ctmsXNetContext;
}
public void init() {
readXml(filename3);
}
public void readXml(String fileName) {
try {
InputStream input = new FileInputStream(fileName); // 建立输入流
// 根据文件格式2003来初始化
wb = new HSSFWorkbook(input);
Sheet sheet = wb.getSheetAt(0); // 获得第一个表单
IteratorRow rows = sheet.rowIterator(); // 获得第一个表单的迭代器
for(int i=0;isheet.getPhysicalNumberOfRows();i++) {
Row row = rows.next(); // 获得行数据
//System.out.println(sheet.getPhysicalNumberOfRows()+"+++++");
IteratorCell cells = row.cellIterator();// 获得第i行的迭代器
HashMapString, String HashBegain=new HashMapString, String();
for(int j=0;jrow.getPhysicalNumberOfCells();j++){
//System.out.println(row.getPhysicalNumberOfCells()+"+++++"+j);
Cell cell = cells.next();
if(i==0||i==7){
list_key.add(cell.getStringCellValue());
}else {
if(cell.getCellType()== cell.CELL_TYPE_NUMERIC){
HashBegain.put((String) list_key.get(j), String.valueOf(cell.getNumericCellValue()));
}
else{HashBegain.put((String) list_key.get(j), cell.getStringCellValue());}
}
}
if(i!=0i!=7){
list_body.add(HashBegain);
}
if(i==6){
list_key = new ArrayListObject();
}
}
installJson();
readExcelEqual();
} catch (IOException ex) {
ex.printStackTrace();
}
}
// 组装即期交易json报文,并发送
public void installJson() {
String headStr = "{Head:" + "{\"chanelNo\":1508170004977,"
+ "\"dataCount\":\"1\"," + "\"eventType\":\"1\","
+ "\"msgNo\":\"1508170004977-20150917171754\","
+ "\"msgType\":\"1\"," + "\"sendingTime\":\"20150917171754\","
+ "\"source\":\"TR1\"," + "\"subSource\":\"TR1\","
+ "\"version\":\"1.0\"}," + "Body:"
+ JSONArray.toJSONString(list_body).replaceAll("\"", "'") + "}";
System.out.println(headStr);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sendXNetTradeData(headStr, "RK1IN.Trade", "UTF-8");
}
// 发送到xnetd
public void sendXNetTradeData(String sendJson, String title, String charset) {
XNetData xnetData = new XNetData();
xnetData.setData(sendJson, charset);
xnetData.setTitle(title);
xnetData.setCommand(XNetCommand.TITLE);
if (ctmsXNetContext.getCtmsXnetSocket().isLoginOK()) {
ctmsXNetContext.getCtmsXnetSocket().sendData(xnetData);
} else {
logger.warn("CtmsXnet no login. sendXNetData[" + title
+ "] return.");
}
}
// 打印json属性
public static void printClassInfo(Object object) {
Field[] fields = object.getClass().getDeclaredFields();
for (int i = 0; i fields.length; i++) {
try {
boolean accessFlag = fields[i].isAccessible();
fields[i].setAccessible(true);
String varName = fields[i].getName();
Object varValue = fields[i].get(object);
System.out.println(varName + "=" + varValue);
fields[i].setAccessible(accessFlag);
} catch (Exception e) {
e.printStackTrace();
}
}
}
//获取excel结果数据,并进行比较
public void readExcelEqual() {
//读取excel头寸数据
HashMapString, String HashEnd=new HashMapString, String();
Workbook wb1;
ListObject list_keyBody = new ArrayListObject();
ListHashMap list_keyBody2 = new ArrayListHashMap();
//ListHashMapString,Object list_keyBody2 = new ArrayListHashMapString,Object();
boolean END=false;
try {
InputStream input = new FileInputStream(filename4); // 建立输入流
wb1 = new HSSFWorkbook(input);
Sheet sheet = wb1.getSheetAt(0); // 获得第一个表单
IteratorRow rows = sheet.rowIterator(); // 获得第一个表单的迭代器
int i = 0;
while (rows.hasNext()) {
HashEnd=new HashMapString, String();
Row row = rows.next(); // 获得行数据
for (int j = 0; j row.getLastCellNum(); j++) {
//System.out.println(row.getLastCellNum()+"XXXXX");
Cell cell = row.getCell(j);
if(i==0){
list_keyBody.add(cell.getStringCellValue());
}else{
if(cell.getCellType()==cell.CELL_TYPE_NUMERIC){
HashEnd.put((String) list_keyBody.get(j), String.valueOf(cell.getNumericCellValue()));
}
else
{
HashEnd.put((String) list_keyBody.get(j), cell.getStringCellValue());
}
}
}
if(i!=0){
list_keyBody2.add(HashEnd);}
i++;
}
} catch (IOException ex) {
ex.printStackTrace();
}
//从数据库获取头寸数据
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
list_map=searchForwardDao.findForwardPos();
//输出计算正确与否
for(int k=0;klist_map.size();k++){
if(END=list_map.get(k).get("AMOUNT_1").toString().equals((String) list_keyBody2.get(k).get("AMOUNT_1")))
{
logger.debug("远期第"+k+"组 货币1金额计算正确"+" Amount1="+list_map.get(k).get("AMOUNT_1").toString());
}else {
logger.debug("远期第"+k+"组 货币1金额计算不正确"+"left="+list_map.get(k).get("AMOUNT_1").toString()+" right="+list_keyBody2.get(k).get("AMOUNT_1"));
}
if(END=list_map.get(k).get("AMOUNT_2").toString().equals((String) list_keyBody2.get(k).get("AMOUNT_2")))
{
logger.debug("远期第"+k+"组 货币2金额计算正确"+" Amount2="+list_map.get(k).get("AMOUNT_2").toString());
}else {
logger.debug("远期第"+k+"组 货币2金额计算不正确"+"left="+list_map.get(k).get("AMOUNT_2").toString()+" right="+list_keyBody2.get(k).get("AMOUNT_2"));
}
if(END=list_map.get(k).get("START_PL").toString().equals((String) list_keyBody2.get(k).get("START_PL")))
{
logger.debug("远期第"+k+"组 累计损益计算正确"+" START_PL="+list_map.get(k).get("START_PL").toString());
}else {
logger.debug("远期第"+k+"组 累计损益计算不正确"+"left="+list_map.get(k).get("START_PL").toString()+" right="+list_keyBody2.get(k).get("START_PL"));
}
if(END=list_map.get(k).get("DAILY_PL").toString().equals((String) list_keyBody2.get(k).get("DAILY_PL")))
{
logger.debug("远期第"+k+"组 日止损计算正确"+" DAILY_PL="+list_map.get(k).get("DAILY_PL").toString());
}else {
logger.debug("远期第"+k+"组 日止损计算不正确"+"left="+list_map.get(k).get("DAILY_PL").toString()+" right="+list_keyBody2.get(k).get("DAILY_PL"));
}
}
}
}
java程序:编写生成100个1-6之间的随机数,统计1—6每个出现的概率,菜鸟看不懂程序
1.用内置方法Math.random()产生一个随机数介于0~1。
2.强制转化为int类型,根据int类型的特点,这个数是0~5之间的整数。
3.得到的数加1,这样这个数就是介于1~6的整数,并且是随机的。
4.将介于1~6之间的这个整数赋值给randomNum
5.(重点)probability是个数组,这个程序的算法就是:利用probability这个数组记录随机数出现的次数,probability[0]记录随机数1出现的次数,就是当出现的随机数为1时,数组的probability[randomNum-1]项加1(randomNum-1是为了表现算法,实际的程序中是不需要先加1再减1的)。同理:其它的随意数也做这样的处理。这样,probability这个数组中[0]项保存的是1的出现次数,[1]项保存的是2出现的次数…………
6.输出随机数的值(后面加个空格“\t”)
7.循环到步骤第1.个步骤.