有哪位能帮我解决两个问题,在考试,没用过Java,都快急死了.
1.多线程编程
用JAVA写一个多线程程序,写四个线程,其中二个对一个变量加1,另外二个对一个变量减1,输出。 2.输入输出
程序功能:从键盘输入一段字符串,统计其中有多少个单词,并将该字符串以及单词数输出到文件test.txt中。(单词用空格隔开)
例如: this is a java exam则 test.txt中应该为
this is a java exam
5谢谢了。
1.多线程编程
用JAVA写一个多线程程序,写四个线程,其中二个对一个变量加1,另外二个对一个变量减1,输出。 2.输入输出
程序功能:从键盘输入一段字符串,统计其中有多少个单词,并将该字符串以及单词数输出到文件test.txt中。(单词用空格隔开)
例如: this is a java exam则 test.txt中应该为
this is a java exam
5谢谢了。
解决方案 »
- javac 软件包不存在?
- 做了个培训方面的门户网站,有个业内人看了,就说技术陈旧!哎~~~~希望大家看看我这样做是否真的陈旧。如果真的陈旧,需要在那里改善?555555
- 求 关于java7的新特性的相关文章(中文的)url
- 这样的水平能找个什么样的工作?
- 到底创造了几个对象??关于string的。
- 不明白,不明白
- 为什么str.replaceAll("location in ('HK')","location not in ('HK','TP')");不能替换?
- ???
- 各位大哥、大姐,快来帮我,我是菜鸟,怎样写和读(随机读)文件
- Weblogic6.0+JBuild5.0整合后,JB如何启动Weblogic?
- java读串口数据乱码~
- 怎样让JLabel显示的gif图像静态显示
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();
}
}
}
}
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();
}
}}