有哪位能帮我解决两个问题,在考试,没用过Java,都快急死了.
1.多线程编程
用JAVA写一个多线程程序,写四个线程,其中二个对一个变量加1,另外二个对一个变量减1,输出。 2.输入输出
程序功能:从键盘输入一段字符串,统计其中有多少个单词,并将该字符串以及单词数输出到文件test.txt中。(单词用空格隔开)
例如: this is a java exam则 test.txt中应该为
this is a java exam
5谢谢了。

解决方案 »

  1.   


    public class ThreadTest1{
      private int j;
      public static void main(String args[]){
    ThreadTest1 tt=new ThreadTest1();
    Inc inc=tt.new Inc();
    Dec dec=tt.new Dec();
    for(int i=0;i<2;i++){
    Thread t=new Thread(inc);
    t.start();
    t=new Thread(dec);
    t.start();
    }
    }
      private synchronized void inc(){
    j++;
    System.out.println(Thread.currentThread().getName()+"-inc:"+j);
      }
      private synchronized void dec(){
    j--;
    System.out.println(Thread.currentThread().getName()+"-dec:"+j);
      }
      class Inc implements Runnable{
    public void run(){
    for(int i=0;i<100;i++){
    inc();
    }
    }
      }
      class Dec implements Runnable{
    public void run(){
    for(int i=0;i<100;i++){
    dec();
    }
    }
      }
    }
      

  2.   

    有异常啊  Exception in thread "mian " java.lang.NoClassDefFoundError:mytest1
      

  3.   


    import java.io.*;public class Test1 { public static void main(String[] args) {
    BufferedReader buf = new BufferedReader(
    new InputStreamReader(System.in));
    try {
    String s = buf.readLine();
    String total[] = null;
    int length;
    total = s.split(" ");
    length = total.length;
    if (s != null && s.length() > 1 && s.substring(0, 1).equals(" ")) {
    length--;
    System.out.println(length);
    } else if (s.length() == 0) {
    System.out.println(0);
    } else {
    System.out.println(length);
    }
    buf.close();
    File file = new File("test.txt");
    FileOutputStream fos = new FileOutputStream(file);
    byte[] strb = s.getBytes();
    for (int i = 0; i < strb.length; i++) {
    fos.write(strb[i]);
    }
    byte[] lengthb = String.valueOf(length).getBytes();
    fos.write("\n总单词数是".getBytes());
    for (int i = 0; i < lengthb.length; i++) {
    fos.write(lengthb[i]);
    }
    fos.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }}
      

  4.   

    两个for循环都不要也可以,直接fos.write(strb);fos.write(lengthb)
      

  5.   

    谢谢了,每个人10分,现在老师已经检查完了,Java课危险了,呵呵,不过还是要谢谢你们了.