java編程的記事本:
import java.util.*;
public class JieChengExample
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
int n,sum;
Jiecheng jie=new Jiecheng();
System.out.print("輸入n的值:");//輸入有幾個階乘相加
n=input.nextInt();
sum=0;
for(int i=1;i<=n;i++)
{
sum=sum+jie.jiecheng(i);//這是n個階乘相加
}
System.out.println("1!+2!+3!+....+n!的和是:"+sum);
}
}
class Jiecheng
{
public int jiecheng(int temp)//算階乘的方法
{
int sum=1;
for(int i=1;i<=temp;i++)
{
sum=sum*i; //計算階乘
}
return sum;//將一個階乘返回
}
}
2.java賽馬游戲:
import java.util.Random;
public class Test {
public static void main(String[] args) {
Competition c = new Competition();
Thread T = new Thread(c);
T.start();
}
}
class Competition implements Runnable{
int red = 0;
int green = 0;
int Speed [] = new int [2];
Competition(){
}
public void run(){
Random r = new Random();
for(int a= 0;a<500;a++){
for(int j = 0;j<2;j++){
Speed[j] = r.nextInt(2);
red = red + Speed[j];
Speed[j] = r.nextInt(2);
green = green + Speed[j];
}
System.out.println("red的速度為"+red);
System.out.println("green的速度為"+green);
while(red >=500 || green>=500){
if(red >=500){
System.out.println("red先抵達終點線");
}
if(green >= 500){
System.out.println("green先抵達終點線");
}
if(green ==500 && red ==500 ){
System.out.println("兩個同時到達");
}
return;
}
}
/* if(red >green){
System.out.println("Redwin"+red);
}
if(red<green){
S...import java.util.Random;
public class Test {
public static void main(String[] args) {
Competition c = new Competition();
Thread T = new Thread(c);
T.start();
}
}
class Competition implements Runnable{
int red = 0;
int green = 0;
int Speed [] = new int [2];
Competition(){
}
public void run(){
Random r = new Random();
for(int a= 0;a<500;a++){
for(int j = 0;j<2;j++){
Speed[j] = r.nextInt(2);
red = red + Speed[j];
Speed[j] = r.nextInt(2);
green = green + Speed[j];
}
System.out.println("red的速度為"+red);
System.out.println("green的速度為"+green);
while(red >=500 || green>=500){
if(red >=500){
System.out.println("red先抵達終點線");
}
if(green >= 500){
System.out.println("green先抵達終點線");
}
if(green ==500 && red ==500 ){
System.out.println("兩個同時到達");
}
return;
}
}
/* if(red >green){
System.out.println("Redwin"+red);
}
if(red<green){
System.out.println("Greenwin"+green);
}
if(red == green){
System.out.println("equal");*/
JAVA的介紹:
Java是一種可以撰寫跨平台應用程序的面向對象的程序設計語言。Java技術具有卓越的通用性、高效性、平台移植性和安全性,廣泛應用於PC、數據中心、游戲控制台、科學超級計算機、行動電話和互聯網,同時擁有全球最大的開發者專業社群。