用JAVA编写一个小程序

用JAVA编写一个小程序,第1张

你等我,我来写!

要加注释吗?txt文件也贴给你吧。

import javaioFile;

import javaioFileInputStream;

public class Test {

private static int excellentNum;

private static int goodNum;

private static int passNum;

private static int failNum;

private static int totalNum;

public static void count() {

String str = readFile();

String[] records = strsplit("\n");

totalNum = recordslength;

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

String[] record = records[i]split(" ");

String[] scoreStr = record[recordlength -1]split("\r");

int score = IntegerparseInt(scoreStr[0]);

if(score >= 90) {

excellentNum++;

}else if(score >= 70) {

goodNum++;

}

if(score >= 60) {

passNum++;

}else {

failNum++;

}

}

}

public static void countExcellent() {

Systemoutprintln("优秀率是: " + (float)excellentNum/totalNum100 + "%");

}

public static void countGood() {

Systemoutprintln("良好率是: " + (float)goodNum/totalNum100 + "%");

}

public static void countPass() {

Systemoutprintln("及格率是: " + (float)passNum/totalNum100 + "%");

}

public static void countFail() {

Systemoutprintln("不及格率是: " + (float)failNum/totalNum100 + "%");

}

public static String readFile() {

File file = new File("D:\\scoretxt");

StringBuffer sb = new StringBuffer();

try {

FileInputStream fis = new FileInputStream(file);

byte[] b = new byte[1024];

int length = 0;

while(fisavailable() > 0) {

length = fisread(b);

String str = new String(b, 0, length);

sbappend(str);

}

} catch (Exception e) {

eprintStackTrace();

}

return sbtoString();

}

public static void main(String[] args) {

count();

countExcellent();

countGood();

countPass();

countFail();

}

}

--------------------------------------

scoretxt

001 小明 90

002 小伟 38

004 好地方 80

005 小明 90

006 小伟 38

007 好地方 80

008 小明 90

009 小伟 38

010 好地方 80

001 小明 90

011 小伟 38

012 好地方 80

连连看java源代码

import javaxswing;

import javaawt;

import javaawtevent;

public class lianliankan implements ActionListener

{

JFrame mainFrame; //主面板

Container thisContainer;

JPanel centerPanel,southPanel,northPanel; //子面板

JButton diamondsButton[][] = new JButton[6][5];//游戏按钮数组

JButton exitButton,resetButton,newlyButton; //退出,重列,重新开始按钮

JLabel fractionLable=new JLabel("0"); //分数标签

JButton firstButton,secondButton; //分别记录两次被选中的按钮

int grid[][] = new int[8][7];//储存游戏按钮位置

static boolean pressInformation=false; //判断是否有按钮被选中

int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戏按钮的位置坐标

int i,j,k,n;//消除方法控制

public void init(){

mainFrame=new JFrame("JKJ连连看");

thisContainer = mainFramegetContentPane();

thisContainersetLayout(new BorderLayout());

centerPanel=new JPanel();

southPanel=new JPanel();

northPanel=new JPanel();

thisContaineradd(centerPanel,"Center");

thisContaineradd(southPanel,"South");

thisContaineradd(northPanel,"North");

centerPanelsetLayout(new GridLayout(6,5));

for(int cols = 0;cols < 6;cols++){

for(int rows = 0;rows < 5;rows++ ){

diamondsButton[cols][rows]=new JButton(StringvalueOf(grid[cols+1][rows+1]));

diamondsButton[cols][rows]addActionListener(this);

centerPaneladd(diamondsButton[cols][rows]);

}

}

exitButton=new JButton("退出");

exitButtonaddActionListener(this);

resetButton=new JButton("重列");

resetButtonaddActionListener(this);

newlyButton=new JButton("再来一局");

newlyButtonaddActionListener(this);

southPaneladd(exitButton);

southPaneladd(resetButton);

southPaneladd(newlyButton);

fractionLablesetText(StringvalueOf(IntegerparseInt(fractionLablegetText())));

northPaneladd(fractionLable);

mainFramesetBounds(280,100,500,450);

mainFramesetVisible(true);

}

public void randomBuild() {

int randoms,cols,rows;

for(int twins=1;twins<=15;twins++) {

randoms=(int)(Mathrandom()25+1);

for(int alike=1;alike<=2;alike++) {

cols=(int)(Mathrandom()6+1);

rows=(int)(Mathrandom()5+1);

while(grid[cols][rows]!=0) {

cols=(int)(Mathrandom()6+1);

rows=(int)(Mathrandom()5+1);

}

thisgrid[cols][rows]=randoms;

}

}

}

public void fraction(){

fractionLablesetText(StringvalueOf(IntegerparseInt(fractionLablegetText())+100));

}

public void reload() {

int save[] = new int[30];

int n=0,cols,rows;

int grid[][]= new int[8][7];

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

for(int j=0;j<=5;j++) {

if(thisgrid[i][j]!=0) {

save[n]=thisgrid[i][j];

n++;

}

}

}

n=n-1;

thisgrid=grid;

while(n>=0) {

cols=(int)(Mathrandom()6+1);

rows=(int)(Mathrandom()5+1);

while(grid[cols][rows]!=0) {

cols=(int)(Mathrandom()6+1);

rows=(int)(Mathrandom()5+1);

}

thisgrid[cols][rows]=save[n];

n--;

}

mainFramesetVisible(false);

pressInformation=false; //这里一定要将按钮点击信息归为初始

init();

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

for(int j = 0;j < 5;j++ ){

if(grid[i+1][j+1]==0)

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

}

}

}

public void estimateEven(int placeX,int placeY,JButton bz) {

if(pressInformation==false) {

x=placeX;

y=placeY;

secondMsg=grid[x][y];

secondButton=bz;

pressInformation=true;

}

else {

x0=x;

y0=y;

fristMsg=secondMsg;

firstButton=secondButton;

x=placeX;

y=placeY;

secondMsg=grid[x][y];

secondButton=bz;

if(fristMsg==secondMsg && secondButton!=firstButton){

xiao();

}

}

}

public void xiao() { //相同的情况下能不能消去。仔细分析,不一条条注释

if((x0==x &&(y0==y+1||y0==y-1)) || ((x0==x+1||x0==x-1)&&(y0==y))){ //判断是否相邻

remove();

}

else{

for (j=0;j<7;j++ ) {

if (grid[x0][j]==0){ //判断第一个按钮同行哪个按钮为空

if (y>j) { //如果第二个按钮的Y坐标大于空按钮的Y坐标说明第一按钮在第二按钮左边

for (i=y-1;i>=j;i-- ){ //判断第二按钮左侧直到第一按钮中间有没有按钮

if (grid[x][i]!=0) {

k=0;

break;

}

else{ k=1; } //K=1说明通过了第一次验证

}

if (k==1) {

linePassOne();

}

}

if (y<j){ //如果第二个按钮的Y坐标小于空按钮的Y坐标说明第一按钮在第二按钮右边

for (i=y+1;i<=j ;i++ ){ //判断第二按钮左侧直到第一按钮中间有没有按钮

if (grid[x][i]!=0){

k=0;

break;

}

else { k=1; }

}

if (k==1){

linePassOne();

}

}

if (y==j ) {

linePassOne();

}

}

if (k==2) {

if (x0==x) {

remove();

}

if (x0<x) {

for (n=x0;n<=x-1;n++ ) {

if (grid[n][j]!=0) {

k=0;

break;

}

if(grid[n][j]==0 && n==x-1) {

remove();

}

}

}

if (x0>x) {

for (n=x0;n>=x+1 ;n-- ) {

if (grid[n][j]!=0) {

k=0;

break;

}

if(grid[n][j]==0 && n==x+1) {

remove();

}

}

}

}

}

for (i=0;i<8;i++ ) { //列

if (grid[i][y0]==0) {

if (x>i) {

for (j=x-1;j>=i ;j-- ) {

if (grid[j][y]!=0) {

k=0;

break;

}

else { k=1; }

}

if (k==1) {

rowPassOne();

}

}

if (x<i) {

for (j=x+1;j<=i;j++ ) {

if (grid[j][y]!=0) {

k=0;

break;

}

else { k=1; }

}

if (k==1) {

rowPassOne();

}

}

if (x==i) {

rowPassOne();

}

}

if (k==2){

if (y0==y) {

remove();

}

if (y0<y) {

for (n=y0;n<=y-1 ;n++ ) {

if (grid[i][n]!=0) {

k=0;

break;

}

if(grid[i][n]==0 && n==y-1) {

remove();

}

}

}

if (y0>y) {

for (n=y0;n>=y+1 ;n--) {

if (grid[i][n]!=0) {

k=0;

break;

}

if(grid[i][n]==0 && n==y+1) {

remove();

}

}

}

}

}

}

}

public void linePassOne(){

if (y0>j){ //第一按钮同行空按钮在左边

for (i=y0-1;i>=j ;i-- ){ //判断第一按钮同左侧空按钮之间有没按钮

if (grid[x0][i]!=0) {

k=0;

break;

}

else { k=2; } //K=2说明通过了第二次验证

}

}

if (y0<j){ //第一按钮同行空按钮在与第二按钮之间

for (i=y0+1;i<=j ;i++){

if (grid[x0][i]!=0) {

k=0;

break;

}

else{ k=2; }

}

}

}

public void rowPassOne(){

if (x0>i) {

for (j=x0-1;j>=i ;j-- ) {

if (grid[j][y0]!=0) {

k=0;

break;

}

else { k=2; }

}

}

if (x0<i) {

for (j=x0+1;j<=i ;j++ ) {

if (grid[j][y0]!=0) {

k=0;

break;

}

else { k=2; }

}

}

}

public void remove(){

firstButtonsetVisible(false);

secondButtonsetVisible(false);

fraction();

pressInformation=false;

k=0;

grid[x0][y0]=0;

grid[x][y]=0;

}

public void actionPerformed(ActionEvent e) {

if(egetSource()==newlyButton){

int grid[][] = new int[8][7];

thisgrid = grid;

randomBuild();

mainFramesetVisible(false);

pressInformation=false;

init();

}

if(egetSource()==exitButton)

Systemexit(0);

if(egetSource()==resetButton)

reload();

for(int cols = 0;cols < 6;cols++){

for(int rows = 0;rows < 5;rows++ ){

if(egetSource()==diamondsButton[cols][rows])

estimateEven(cols+1,rows+1,diamondsButton[cols][rows]);

}

}

}

public static void main(String[] args) {

lianliankan llk = new lianliankan();

llkrandomBuild();

llkinit();

}

}

//old 998 lines

//new 318 lines

参考资料:

>

Scanner input = new Scanner(Systemin);

while(true)

{

Systemoutprint("请输入4位数字(整数):");

int number = inputnextInt();

if(number>999&&number<10000)

{

Systemoutprintln("恭喜您输入正确");

break;

}

continue;

}

以上就是关于用JAVA编写一个小程序全部的内容,包括:用JAVA编写一个小程序、!高分跪求帮忙写一个简单小程序的JAVA课程设计报告(内详!!)、使用java语言怎么制作出一个小程序,详情如下:等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

原文地址:https://54852.com/zz/9877631.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存