//第一题:
创新互联作为成都网站建设公司,专注网站建设、网站设计,有关企业网站建设方案、改版、费用等问题,行业涉及柔性防护网等多个领域,已为上千家企业服务,得到了客户的尊重与认可。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.ArrayList;
import java.util.List;
public class ManageName {
ListString namelist = new ArrayListString();
public void PrintMenu() {
System.out.println("\n\r1.Add new name " + "2.Display all name"
+ " 3.Quit"
+ "\nPlease select menu(1,2,3)");
}
public void displayname() {
for (int w = 0; w this.namelist.size(); w++) {
System.out.println("Name" + w+":" + this.namelist.get(w));
}
}
public List addName(String name) {
namelist.add(name);
return namelist;
}
public static void main(String[] args) throws IOException {
ManageName a = new ManageName();
a.addName("jing5083394");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while (true) {
a.PrintMenu();
String content = in.readLine();
if (content.equalsIgnoreCase("3")) {
break;
} else if (content.equalsIgnoreCase("1")) {
String name;
BufferedReader in2 = new BufferedReader(new InputStreamReader(
System.in));
System.out.println("Please input the Name to add new user");
String content2 = in.readLine();
name = content2;
a.addName(name);
System.out.println("Add name successfully\r\n");
}
else if (content.equalsIgnoreCase("2")) {
a.displayname();
}
else {
System.out.println("Invalid selections!\n");
}
}
}
}
//第二题: 把文件放到namefile.txt
/*
* 从namefile.txt文件中读入的字符串全部转换成大写字母,
* 再按原来的顺序输出到yoursData.txt文件中。
*/
import java.lang.Character;
import java.io.*;
//将一个文件复制到另一个文件中(覆盖)
public class Filestream {
public static void main(String args[]) {
try {
File inFile = new File("c:\\namefile.txt");
File outFile = new File("c:\\yoursData.txt");
FileInputStream fis = new FileInputStream(inFile); // 读输入文件
FileOutputStream fos = new FileOutputStream(outFile);
int c;
while ((c = fis.read()) != -1) {
c = Character.toUpperCase(c);
fos.write(c);
}// 写入文件中
System.out.println("Output file finish");
fis.close();
fos.close();
} catch (FileNotFoundException e) {
System.out.println("FileStreamsTest: " + e);
} catch (IOException e) {
System.err.println("FileStreamsTest: " + e);
}
}
}
刚写的,新鲜出炉
import java.util.ArrayList;
import java.util.Scanner;
public class LoopAndFunction {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int number;
ArrayListInteger theList = new ArrayListInteger();
System.out.print("Enter a number for list, end with -1: ");
number = input.nextInt();
while(number != -1){
theList.add(number);
System.out.print("Enter a number for list, end with -1: ");
number = input.nextInt();
}
System.out.println("the list is: " + theList.toString());
sortArray(theList);
System.out.println("after sorting, the list is: " + theList.toString());
for(int i = 0; i theList.size(); i++){
int eachNumber = theList.get(i);
if(isOddNumber(eachNumber)){
System.out.println(eachNumber + " is odd number");
}
else{
System.out.println(eachNumber + " is even number");
}
}
}
public static boolean isOddNumber(int number){
boolean isOdd = false;
if(number % 2 == 1){
isOdd = true;
}
return isOdd;
}
public static void sortArray(ArrayListInteger list){
for(int i = 0; i list.size() - 1; i++){
int temp = list.get(i);
for(int j = i + 1; j list.size(); j++){
if(list.get(i) list.get(j)){
list.set(i, list.get(j));
list.set(j, temp);
}
}
}
}
}
讲解嘛,main方法就是调用两个方法的,通过Scanner, while循环让用户输入一些整数,以-1结束,然后先是排列数组,再使用for来挨个判断这些数字是否为偶数。isOddNumber很简单,就是判断一个整数是否为奇数,用%求余来实现。sortArray也很简单,就是排序一个整数型的list的。
贪吃蛇游戏 望采纳
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);
}
}
}
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);
}
}
}
首先,感谢提问,其次也请尊重别人的时间和劳动成果。如果解决了你的问题,请不吝给分。谢谢。
在你的makepic2()中加入jp1.validate()就可以了。如下所示:
private void makepic2(){
jp1.removeAll();
jp1.repaint();
double t =Math.floor(Math.random()*9 + 1);
int k= (int) t;
coranswer= String.valueOf(k) ;
for(int i = 0;ik;i++)
{ int j = i + 1;
animal[i]=new JLabel(new ImageIcon(getClass().getResource("animal"+j+".png")));
jp1.add(animal[i]);
}
jp1.validate();
}
不能load的原因就是JPanel是不断的再变化的,而JPanel从Container继承的方法validate可以解决这个问题
validate
public void validate()
验证此容器及其所有子组件。
使用 validate 方法会使容器再次布置其子组件。已经显示容器后,在修改此容器的子组件的时候(在容器中添加或移除组件,或者更改与布局相关的信息),应该调用上述方法。
如果此 Container 无效,则此方法将调用 validateTree 方法,并将此 Container 标记为有效。否则不执行任何动作。
覆盖:
类 Component 中的 validate
随便给你写了一个
package com.wys.util;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Test {
public static ListInteger smallNumbers,largeNumbers;
public static int sum1 = 0,sum2 = 0;
public static void main(String[] args) {
smallNumbers = new ArrayListInteger();
largeNumbers = new ArrayListInteger();
run();
}
public static void run() {
int i = 0;
for (int j = 0; j 50; j++) {
Random rand = new Random();
i = rand.nextInt(1000);
if (i500) {
small(i);
}else{
large(i);
}
}
System.out.println("随机输出的50个数字中:");
System.out.println("大于500的数(包括500)共有"+largeNumbers.size()+"个");
System.out.print("他们是"+largeNumbers);
System.out.println();
System.out.println("他们的和是"+sum1);
System.out.println("小于500的数共有"+smallNumbers.size()+"个");
System.out.print("他们是"+smallNumbers);
System.out.println();
System.out.println("他们的和是"+sum2);
}
private static void large(int number) {
largeNumbers.add(number);
sum1 += number;
}
private static void small(int number) {
smallNumbers.add(number);
sum2 += number;
}
}