求大神帮忙编一个简单的java程序代码

求大神帮忙编一个简单的java程序代码,第1张

import javautil;

public class Test {

public static void main(String[] args){

Systemoutprintln("您的性别是:\n1男\n2女\n请根据具体情况选择编号:");

Scanner sc=new Scanner(Systemin);

int sex=scnextInt();

switch(sex){

case 1:Systemoutprintln("您选择的性别是男人");

break;

case 2:Systemoutprintln("您选择的性别是女人");

break;

default:Systemoutprintln("数据非法!");

break;

}

}

}

工程自己建,然后你建个Test类把代码复制进去就行了。

class Ball {

public void play() {

Systemoutprintln("玩球儿");

}

}

class Football extends Ball {

public void play() {

Systemoutprintln("使用足球运动");

}

}

class Basketball extends Ball {

public void play() {

Systemoutprintln("使用篮球运动");

}

}

public class TestMain {

public static void main(String[] args) {

TestMain tm = new TestMain();

tmtestPlay();

}

public void testPlay() {

Ball ball = new Football();

ballplay();

ball = new Basketball();

ballplay();

}

}

/

D:\>javac TestMainjava

D:\>java TestMain

使用足球运动

使用篮球运动

/

import javaawtFlowLayout;

import javaawteventActionEvent;

import javaawteventActionListener;

import javaxswingJButton;

import javaxswingJFrame;

import javaxswingJLabel;

import javaxswingJOptionPane;

import javaxswingJPasswordField;

import javaxswingJTextField;

public class Login {

public static void main(String args[]) {

LoginFrm frame = new LoginFrm();

}

}

class LoginFrm extends JFrame implements ActionListener{

JLabel nameLabel=new JLabel("用户名:");

JLabel pwdLabel=new JLabel("密码:");

JTextField name=new JTextField(10);

JPasswordField password=new JPasswordField(10);

JButton butnSure=new JButton("确定");

JButton butnCancel=new JButton("取消");

public LoginFrm() {

super("登陆");

setBounds(500, 200, 280, 220);

setVisible(true);

setLayout(null);

nameLabelsetBounds(45, 20, 100, 25);

add(nameLabel);

add(name);

namesetBounds(105, 20, 110, 25);

add(pwdLabel);

pwdLabelsetBounds(45, 60, 100, 25);

add(password);

passwordsetBounds(105, 60, 110, 25);

add(butnSure);

butnSuresetBounds(45, 100, 80, 25);

add(butnCancel);

butnCancelsetBounds(135, 100, 80, 25);

butnSureaddActionListener(this);

butnCanceladdActionListener(this);

setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

validate();//刷新

}

public void actionPerformed(ActionEvent e) {

if (egetSource() ==butnSure){

Systemoutprintln("用户名:"+namegetText());

Systemoutprintln("密码:"+namegetText());

if("admin"equals(namegetText()trim())&&"123"equals(passwordgetText()trim())){

thisdispose();

new MainFrm("用户界面",namegetText()trim(),passwordgetText()trim());

}else {

JOptionPaneshowMessageDialog(this, "用户不存在");

}

}else if(egetSource()==butnCancel){

Systemexit(1);

}

}

class MainFrm extends JFrame{

private JLabel info;

public MainFrm(String s,String name,String password) {

super(s);

setBounds(400, 200, 500, 400);

setLayout(new FlowLayout());

info=new JLabel("登陆成功,用户名:"+name+",密码:"+password);

add(info);

setVisible(true);

setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

validate();

}

}

}

一、

package a;

public class Circle{

public float r;

public void Circle(){r=1;}

public void Circle(float r){thisr = r;}

public float calculateSize(){

return 314rr;

}

}

二、

package b;

import aCircle;

public class Cylinder extends Circle{

protected float h;

public float getH(){return h;}

public void setH(float h){thish=h;}

public float getV(){

return hcalculateSize();

}

}

三、

import a;

import b;

public class Test{

public static void main(String []args){

Cylinder c = new Cylinder(5);

csetH(6);

Systemoutprintln(cgetH());

Systemoutprintln(ccalculateSize());

Systemoutprintln(cgetV());

}

}

以上程序是临时编写,没有测试,你自己运行一下吧,如果有问题应该也不大,自己测试一下。

下下来,是不是exe文件 ,还是一个文件夹呢,你得配置一下你的系统变量在我的电脑右击属性,打开高级,在里面有一个变量 ,打开,在上面添加一个 名为PATH 值为,你安装的那个目录,下的bin 如: C:\java\bin 这样就可以用了 打开记事本,在里面输入:public class HellowWord{public static void main(String[]args){Systemoutprintln("HellowWord!");} 保存为 HellowWordjava文件 文件名一定要和类名一样 进入Dos 中,并进入到这个文件 的目录在里面输入: javac HellowWordjava没有问题的情况下再次输入: java HellowWord就OK了}

贪吃蛇游戏 望采纳

import javaawtButton;

import javaawtColor;

import javaawtGridLayout;

import javaawtPoint;

import javaawteventKeyEvent;

import javaawteventKeyListener;

import javautil;

import javaxswingJFrame;

import javaxswingJOptionPane;

public class Snake extends JFrame implements KeyListener{

int Count=0;

Button[][] grid = new Button[20][20];

ArrayList<Point> snake_list=new ArrayList<Point>();

Point bean=new Point(-1,-1);//保存随机豆子坐标

int Direction = 1; //方向标志 1:上 2:下 3:左 4:右

//构造方法

public Snake()

{

//窗体初始化

thissetBounds(400,300,390,395);

thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

GridLayout f=new GridLayout(20,20);

thisgetContentPane()setBackground(Colorgray);

thissetLayout(f);

//初始化2020个按钮

for(int i=0;i<20;i++)

for(int j=0;j<20;j++)

{

grid[i][j]=new Button();

thisadd(grid[i][j]);

grid[i][j]setVisible(false);

grid[i][j]addKeyListener(this);

grid[i][j]setBackground(Colorblue);

}

//蛇体初始化

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_listadd(new Point(10,10));

snake_listadd(new Point(11,10));

snake_listadd(new Point(12,10));

snake_listadd(new Point(13,10));

snake_listadd(new Point(14,10));

thisrand_bean();

thissetTitle("总分:0");

thissetVisible(true);

}

//该方法随机一个豆子,且不在蛇体上,并使豆子可见

public void rand_bean(){

Random rd=new Random();

do{

beanx=rdnextInt(20);//行

beany=rdnextInt(20);//列

}while(snake_listcontains(bean));

grid[beanx][beany]setVisible(true);

grid[beanx][beany]setBackground(Colorred);

}

//判断拟增蛇头是否与自身有碰撞

public boolean is_cross(Point p){

boolean Flag=false;

for(int i=0;i<snake_listsize();i++){

if(pequals(snake_listget(i) )){

Flag=true;break;

}

}

return Flag;

}

//判断蛇即将前进位置是否有豆子,有返回true,无返回false

public boolean isHaveBean(){

boolean Flag=false;

int x=snake_listget(0)x;

int y=snake_listget(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(beanequals(p))Flag=true;

return Flag;

}

//前进一格

public void snake_move(){

if(isHaveBean()==true){//////////////有豆子吃

Point p=new Point(beanx,beany);//很重要,保证吃掉的是豆子的复制对象

snake_listadd(0,p); //吃豆子

grid[px][py]setBackground(Colorblue);

thisCount++;

thissetTitle("总分:"+Count);

thisrand_bean(); //再产生一个豆子

}else{///////////////////无豆子吃

//取原蛇头坐标

int x=snake_listget(0)x;

int y=snake_listget(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(px<0||px>19|| py<0||py>19||is_cross(p)==true){

JOptionPaneshowMessageDialog(null, "游戏结束!");

Systemexit(0);

}

//向蛇体增加新的蛇头坐标,并使新蛇头可见

snake_listadd(0,p);

grid[px][py]setVisible(true);

//删除原蛇尾坐标,使蛇尾不可见

int x1=snake_listget(snake_listsize()-1)x;

int y1=snake_listget(snake_listsize()-1)y;

grid[x1][y1]setVisible(false);

snake_listremove(snake_listsize()-1);

}

}

@Override

public void keyPressed(KeyEvent e) {

if(egetKeyCode()==KeyEventVK_UP && Direction!=2) Direction=1;

if(egetKeyCode()==KeyEventVK_DOWN && Direction!=1) Direction=2;

if(egetKeyCode()==KeyEventVK_LEFT && Direction!=4) Direction=3;

if(egetKeyCode()==KeyEventVK_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){

winsnake_move();

Threadsleep(300);

}

}

}

以上就是关于求大神帮忙编一个简单的java程序代码全部的内容,包括:求大神帮忙编一个简单的java程序代码、java一个简单小程序,求高手帮忙编写,万分感谢、求编写一个超级简单的Java的程序源代码等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/zz/12570413.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存