重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
原来File fDir=new File("D://");是这样的File fDir=new File(File.separator); 我改了 但是还是不行 啊。回答: import java.io.File; public class NewClass7 { public static void main(String[] args) throws Exception{ File fDir=new File("F://");//分隔符 separator String strFile="sg"+File.separator+"DT"+File.separator+"1.txt"; File f=new File(fDir,strFile); f.createNewFile();}}当然,如果按照你这段代码,你要确保目标盘上得有sg/DT这两个目录,否则你就要在代码里创建这两个目录了。补充: 看我修改后的代码,必须引用java.io.File类,你原来代码中没有,还有,你要确保目标盘上得有sg/DT这两个目录。追问: 我多问一句。这段代码 属于J2EE 吧?回答: 不能这样说,J2EE是JAVA体系的一部份,是一种JAVA应用开发的技术架构。你这段代码只是一段JAVA的基础代码。
为霍邱等地区用户提供了全套网页设计制作服务,及霍邱网站建设行业解决方案。主营业务为网站建设、成都网站设计、霍邱网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
public class Group {
public static void main(String[] args) {
ListInteger l = new LinkedListInteger();
int[][] group = new int[4][7];
Random r = new Random();
for (int i = 0; i 31; i++)
l.add(new Integer(i + 1));
for (int i = 0; i 4; i++) {
for (int j = 0; j 7; j++) {
int k = r.nextInt(l.size());
group[i][j] = l.get(k);
l.remove(k);
}
}
for (int i = 0; i 4; i++) {
System.out.print("Group " + (i + 1) + ": ");
for (int j = 0; j 7; j++) {
System.out.print(group[i][j] + " ");
}
System.out.println();
}
// 剩下分组
String groups = "";
for(Integer _tem : l) {
groups += _tem + " ";
}
System.out.print("Group " + "5" + ": " + groups);
}
}
自己写着玩的,很简单,你试一试哦...
主要用了javax.swing.Timer这个类:
import java.awt.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class MainClass extends JFrame {
ControlSnake control;
Toolkit kit;
Dimension dimen;
public static void main(String[] args) {
new MainClass("my snake");
}
public MainClass(String s) {
super(s);
control = new ControlSnake();
control.setFocusable(true);
kit = Toolkit.getDefaultToolkit();
dimen = kit.getScreenSize();
add(control);
setLayout(new BorderLayout());
setLocation(dimen.width / 3, dimen.height / 3);// dimen.width/3,dimen.height/3
setSize(FWIDTH, FHEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
}
public static final int FWIDTH = 315;
public static final int FHEIGHT = 380;
}
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.Timer;
import java.util.Random;
@SuppressWarnings("serial")
public class ControlSnake extends JPanel implements ActionListener {
Random rand;
ArrayListPoint list, listBody;
String str, str1;
static boolean key;
int x, y, dx, dy, fx, fy, flag;
int snakeBody;
int speed;
public ControlSnake() {
snakeBody = 1;
str = "上下左右方向键控制 P键暂停...";
str1 = "现在的长度为:" + snakeBody;
key = true;
flag = 1;
speed = 700;
rand = new Random();
list = new ArrayListPoint();
listBody = new ArrayListPoint();
x = 5;
y = 5;
list.add(new Point(x, y));
listBody.add(list.get(0));
dx = 10;
dy = 0;
fx = rand.nextInt(30) * 10 + 5;// 2
fy = rand.nextInt(30) * 10 + 5;// 2
setBackground(Color.WHITE);
setSize(new Dimension(318, 380));
final Timer time = new Timer(speed, this);
time.start();
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 37) {
dx = -10;
dy = 0;
} else if (e.getKeyCode() == 38) {
dx = 0;
dy = -10;
} else if (e.getKeyCode() == 39) {
dx = 10;
dy = 0;
} else if (e.getKeyCode() == 40) {
dx = 0;
dy = 10;
} else if (e.getKeyCode() == 80) {
if (flag % 2 == 1) {
time.stop();
}
if (flag % 2 == 0) {
time.start();
}
flag++;
}
}
});
}
public void paint(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, 400, 400);
g.setColor(Color.DARK_GRAY);
g.drawLine(3, 3, 305, 3);
g.drawLine(3, 3, 3, 305);
g.drawLine(305, 3, 305, 305);
g.drawLine(3, 305, 305, 305);
g.setColor(Color.PINK);
for (int i = 0; i listBody.size(); i++) {
g.fillRect(listBody.get(i).x, listBody.get(i).y, 9, 9);
}
g.fillRect(x, y, 9, 9);
g.setColor(Color.ORANGE);
g.fillRect(fx, fy, 9, 9);
g.setColor(Color.DARK_GRAY);
str1 = "现在的长度为:" + snakeBody;
g.drawString(str, 10, 320);
g.drawString(str1, 10, 335);
}
public void actionPerformed(ActionEvent e) {
x += dx;
y += dy;
if (makeOut() == false) {
JOptionPane.showMessageDialog(null, "重新开始......");
speed = 700;
snakeBody = 1;
x = 5;
y = 5;
list.clear();
list.add(new Point(x, y));
listBody.clear();
listBody.add(list.get(0));
dx = 10;
dy = 0;
}
addPoint(x, y);
if (x == fx y == fy) {
speed = (int) (speed * 0.8);//速度增加参数
if (speed 200) {
speed = 100;
}
fx = rand.nextInt(30) * 10 + 5;// 2
fy = rand.nextInt(30) * 10 + 5;// 2
snakeBody++;// 2
} // 2
repaint();
}
public void addPoint(int xx, int yy) {
// 动态的记录最新发生的50步以内的移动过的坐标
// 并画出最新的snakeBody
if (list.size() 100) {//蛇身长度最长为100
list.add(new Point(xx, yy));
} else {
list.remove(0);
list.add(new Point(xx, yy));
}
if (snakeBody == 1) {
listBody.remove(0);
listBody.add(0, list.get(list.size() - 1));
} else {
listBody.clear();
if (list.size() snakeBody) {
for (int i = list.size() - 1; i 0; i--) {
listBody.add(list.get(i));
}
} else {
for (int i = list.size() - 1; listBody.size() snakeBody; i--) {
listBody.add(list.get(i));
}
}
}
}
public boolean makeOut() {
if ((x 3 || y 3) || (x 305 || y 305)) {
return false;
}
for (int i = 0; i listBody.size() - 1; i++) {
for (int j = i + 1; j listBody.size(); j++) {
if (listBody.get(i).equals(listBody.get(j))) {
return false;
}
}
}
return true;
}
}
最简单的java代码肯定就是这个了,如下:
public class MyFirstApp
{
public static void main(String[] args)
{
System.out.print("Hello world");
}
}
“hello world”就是应该是所有学java的新手看的第一个代码了。如果是零基础的新手朋友们可以来我们的java实验班试听,有免费的试听课程帮助学习java必备基础知识,有助教老师为零基础的人提供个人学习方案,学习完成后有考评团进行专业测试,帮助测评学员是否适合继续学习java,15天内免费帮助来报名体验实验班的新手快速入门java,更好的学习java!
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = "";
System.out.println("请输入指令:");
while(true){
str = sc.next();
if(str.equals("add")){
System.out.println("执行两数相加");
} else if(str.equals("sub")){
System.out.println("执行两数相减");
} else if(str.equals("help")){
System.out.println("add:两数相加 sub:两数相减");
} else {
System.out.println("请输入正确指令:");
}
}
}
你看看这段行不行
public class Message {
public static void main(String[] args){
String name;
int age;
System.out.println("请输入姓名,回车结束:"); //提示输入
Scanner sc = new Scanner(System.in);
name = sc.nextLine(); //为变量赋值
System.out.println("请输入年龄,回车结束:");
age = sc.nextInt();
System.out.println("姓名:"+name+"\n年龄:"+age); //打印姓名及年龄
}
}
//不知道这样行么?