java初学者小程序(java新手小程序)

本文目录
java新手小程序
代码如下:
import java.util.Scanner;
public class App71 {
public static void main(String args) {
Scanner scanner = new Scanner(System.in);
int sum = 0;
while (true) {
System.out.print("请同学A输入一个数:");
sum += scanner.nextInt();
if (sum % 5 == 0) {
System.out.println("数据之和 " + sum + " 是 5 的倍数,同学A输了。");
break;
}
System.out.print("请同学B输入一个数:");
sum += scanner.nextInt();
if (sum % 5 == 0) {
System.out.println("数据之和" + sum + "是 5 的倍数,同学B输了。");
break;
}
}
}
}
执行结果
Java初学者小程序
import javax.swing.JFrame ;
import javax.swing.JPanel ;
import java.awt.Graphics ;
import java.awt.BorderLayout ;
import java.awt.Color ;
import java.util.ArrayList ;
public class MobileSorter extends JFrame {
//上边距
public static final int TOP_SPA = 100 ;
//左边距
public static final int LEFT_SPA = 100 ;
public static void main(String args) {
JFrame.setDefaultLookAndFeelDecorated(true) ;
MobileSorter ms = new MobileSorter() ;
ms.setVisible(true) ;
}
public MobileSorter() {
//设置窗体大小
this.setSize(400 ,400) ;
//new出组件
MyPanel p = new MyPanel() ;
//布置窗体
this.setLayout(new BorderLayout()) ;
this.add(p ,BorderLayout.CENTER) ;
//启动线程
Thread t = new Thread(p) ;
t.start() ;
}
}
class MyPanel extends JPanel implements Runnable {
//存储圆数据的数组
private ArrayList ca = new ArrayList(9);
/**
* 中心圆
*/
private Circle cCenter = null ;
public MyPanel() {
init() ;
}
public void paint(Graphics g) {
//画与5号位的斜连接线
for (int i = 0; i《9; i++) {
if (i == 4) continue ;
Circle.drawLine(g ,(Circle)ca.get(4) ,(Circle) ca.get(i)) ;
}
//垂直和横线和竖线
g.setColor(Color.BLACK) ;
for (int i = 0; i《9; i++)
for (int j = 0; j《9; j++)
if (i != j && i 《 j)
if (j - i ==3 || (i + 1 == j && j%3 != 0))
Circle.drawLine(g , (Circle) ca.get(i) ,(Circle) ca.get(j)) ;
/** 画圆 */
for (int i = 0; i《9; i++)
((Circle) ca.get(i)).drawMe(g) ;
}
/**
* 初始化
*/
public void init() {
//创建圆
for (int i = 1; i《10; i++)
this.ca.add(new Circle(i)) ;
//生成圆内的数
int ;
for (int i = 0; i《n.length; ) {
int c ;
c = (int)(Math.random() * 8) + 1 ;
boolean isRepeat = false ;
for (int j = 0; j《i; j++)
if (n == c) {
isRepeat = true ;
break ;
}
if (isRepeat) continue ;
if (i == 4) i ++ ;
((Circle)this.ca.get(i)).setNum(c) ;
n = c ;
i ++ ;
}
}
/**
* 线程
*/
public void run() {
int oPos = 0 ; //值为1的圆的标号
int sPos ; //值为1的圆的下一个圆的标号
int ePos ; //终端圆标号
int cPos = 0 ; //操作圆标号
cCenter = (Circle)this.ca.get(4) ; //中心圆
//找出圆内数字的1的圆
for (int i = 0; i《this.ca.size(); i++)
if (((Circle)this.ca.get(i)).getNum() == 1) {
oPos = i ;
break ;
}
sPos = Circle.getNextDeasil(oPos) ;
ePos = oPos ;
while (true) {
cPos = sPos ;
while (true) {
Circle c = (Circle)this.ca.get(cPos) ;
Circle n = (Circle)this.ca.get(Circle.getNextDeasil(cPos)) ;
checkSwap(c ,n) ;
cPos = Circle.getNextDeasil(cPos) ;
if(ePos == Circle.getNextDeasil(cPos)) {
ePos = cPos ;
break ;
}
}
if (ePos == Circle.getNextDeasil(sPos)) {
System.out.println ("OVER!") ;
break ;
}
}
}
/**
* 延迟
*/
private void animation() {
this.repaint() ;
try { Thread.sleep(1000) ;}catch (Exception ex) { }
}
/**
* 交换
*/
private void checkSwap(Circle c ,Circle n) {
int cNum = c.getNum() ;
int nNum = n.getNum() ;
if (cNum 》 nNum) {
cCenter.setNum(n.getNum()) ;
n.setNum(0) ;
this.animation() ;
n.setNum(c.getNum()) ;
c.setNum(0) ;
this.animation() ;
c.setNum(cCenter.getNum()) ;
cCenter.setNum(0) ;
this.animation() ;
}
}
}
class Circle {
/**
* 各圆之间的间距
*/
public static final int CIR_SPA_BET = 60 ;
/**
* 圆的直径
*/
public static final int CIR_WD = 30 ;
/**
* 圆的标号 ,1-9
*/
private int pos ;
/**
* 圆的左上角x坐标
*/
private int x ;
/**
* 圆的左上角y坐标
*/
private int y ;
/**
* 圆内的数
*/
private int num ;
public Circle(int pos) {
this.pos = pos ;
this.x = MobileSorter.LEFT_SPA + (pos-1) % 3 * CIR_SPA_BET ;
this.y = MobileSorter.TOP_SPA + (pos-1) / 3 * CIR_SPA_BET ;
}
/**
* 画圆与圆中的数字
*/
public void drawMe(Graphics g) {
//圆
g.setColor(Color.BLACK) ;
g.fillOval(x ,y ,CIR_WD ,CIR_WD) ;
//数字
g.setColor(Color.WHITE) ;
if (num != 0)
g.drawString(String.valueOf(num)
,x + CIR_WD / 2 - 3 ,y + CIR_WD / 2 + 5) ;
else
g.drawString("空"
,x + CIR_WD / 2 - 3 ,y + CIR_WD / 2 + 5) ;
}
/**
* 画两个圆之间的连接线
*/
public static void drawLine(Graphics g ,Circle a , Circle b) {
g.drawLine(a.getX() + Circle.CIR_WD / 2
,a.getY() + Circle.CIR_WD / 2
,b.getX() + Circle.CIR_WD / 2
,b.getY()+ Circle.CIR_WD / 2) ;
}
public void setNum(int num) {
this.num = num ;
}
public int getX() {
return x ;
}
public int getY() {
return y ;
}
public int getPos() {
return pos ;
}
public int getNum() {
return num ;
}
public static int getNextDeasil(int pos) {
if (pos 》=0 && pos 《=8 && pos != 4) {
if (pos == 0)
return 1 ;
else if (pos == 1)
return 2 ;
else if (pos == 2)
return 5 ;
else if (pos == 3)
return 0 ;
else if (pos == 5)
return 8 ;
else if (pos == 6)
return 3 ;
else if (pos == 7)
return 6 ;
else if (pos == 8)
return 7 ;
}
return -1 ;
}
/**
* 根据顺时针方向返回下个圆的标号
*/
public int getNextDeasil() {
if (pos 》=0 && pos 《=8 && pos != 4) {
if (pos == 0)
return 1 ;
else if (pos == 1)
return 2 ;
else if (pos == 2)
return 5 ;
else if (pos == 3)
return 0 ;
else if (pos == 5)
return 8 ;
else if (pos == 6)
return 3 ;
else if (pos == 7)
return 6 ;
else if (pos == 8)
return 7 ;
}
return -1 ;
}
}
0

更多文章:
全球新冠肺炎疫情背景下航运发展(盐田港复苏日志:半年历劫从“低谷”到“爆仓” 疫情之后巨轮如何越洋航行)
2026年9月7日 17:10
matlab求解带字母参数方程组(我想matlab求一个关于x,y的方程组 ab c d f e h m n 都是参数)
2026年9月7日 16:30
oracle中的循环语句(下面哪个不是oracle程序设计中的循环语句 a for)
2026年9月7日 15:30
电脑里2个系统怎么删除一个(电脑开机显示有两个系统,如何删除一个)
2026年9月7日 12:20
scrollthrough意思(“scroll”是什么意思)
2026年9月7日 08:00
怎么激活keygen(注册机如何激活cad2008一个简单激活cad2008的方法)
2026年9月7日 06:30
vba编写excel插件(excel vba中能否动态创建控件)
2026年9月7日 04:40




