java经典编程案例有哪些?

发布网友 发布时间:2022-04-23 09:59

我来回答

1个回答

热心网友 时间:2023-09-19 06:38

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、数据中心、游戏控制台、科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com