欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

实验:熟悉java编程环境

程序员文章站 2022-04-21 21:16:42
(1)java应用程序public class Test {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubSystem.out.println("**********************");System.out.println("Welcome to Java World!");System.out.println(....

实验:熟悉java编程环境
(1)java应用程序

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("**********************");
		System.out.println("Welcome to Java World!");
		System.out.println("**********************");
	}

}

(2)java小应用程序

import java.awt.Graphics;
import java.applet.Applet;
public class minijava extends Applet {
	/**
	 * @param args
	 */
	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		g.drawString("**********************", 10, 20);
		g.drawString("Welcome to Java World!", 10, 40);
		g.drawString("**********************", 10, 60);
	}

}

(3)Fibonacci数列

public class Fibonacci_test {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//裴波那契数列
			System.out.println("Fibonacci的前20位数是:");
			int first = 1;
			int second = 1;
			int next=0;
			for (int i = 1; i < 21; i++) {
				if(i==1){
					System.out.print(first+"\t");//默认加一个换行符
				}
				else if(i==2){
					System.out.print(second+"\t");//默认加一个换行符
				}
				else{
				next = first+second;
				first = second;
				second = next;
				System.out.print(next+"\t");
				if(i%5==0){
					System.out.println("");//默认加一个换行符
				}
			}
		}
	}
}

(4)判断素数

import java.util.Scanner;
public class prime_number {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		while(true){
		Scanner scan = new Scanner(System.in);
		System.out.println("输入正整数:");
		int i = scan.nextInt();
		if(i<0) {
		System.out.println("输入错误!");
		}
		else if(i==1||i==0) {
		System.out.println(i+"既非素数也非合数");
		}
		else {
		boolean isSushu = true; //标记
		for(int j=2;j<i;j++) {
		if(i%j==0) {
		isSushu = false;
		break;
		}}
		if(isSushu==true) {
		System.out.println(i+"是素数");
		}else {
		System.out.println(i+"不是素数");
		}
		}
	}
}
}

本文地址:https://blog.csdn.net/qq_43554335/article/details/110481853