谁可以帮我找两个自编的JAVA的基本程序?????

发布时间:2024-05-12 18:12 发布:上海旅游网

问题描述:

简单一点的,不要太复杂

问题解答:

1.class book
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}

2.
class suzu
{
public static void main(String[] args)
{
int a[],i;
a=new int[3];
for(i=0;i<=a.length;i++)
{a[i]=1+i;
System.out.println(a[i]+" ");
}
}
}

1.
import java.awt.*;
import javax.swing.*;

//Class J_ButtonLay: To present 3 lines of buttons
public class J_ButtonLay extends JFrame
{
// Construction of class: J_ButtonLay
public J_ButtonLay()
{
super("Buttons in 3 Lines");

int arr[] = {3, 1, 2};
JButton[][] inLine = new JButton[3][];
JPanel[] panel = new JPanel[3];
Container cont = getContentPane();
cont.setLayout(new BoxLayout(cont, BoxLayout.Y_AXIS));

for(int i = 0; i < 3; i++) {
inLine[i] = new JButton[arr[i]];
panel[i] = new JPanel();
panel[i].setLayout(new BoxLayout(panel[i], BoxLayout.X_AXIS));
for(int j = 0; j < arr[i]; j++) {
String buttonName = "Button "+String.valueOf(j+1)
+" in Line "+String.valueOf(i+1);
inLine[i][j] = new JButton(buttonName);
panel[i].add(inLine[i][j]);
}// End of loop: inner for
cont.add(panel[i]);
}// End of loop: outter for
}// End of construction

public static void main(String[] args)
{
J_ButtonLay app = new J_ButtonLay();
app.setVisible(true);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.pack();
}// End of method: main

}// End of class: J_ButtonLay

2.

import java.io.*;

/**class J_Prime: The class to calculate all the primes */
public class J_Prime
{

/** method mb_prime: The method to filter the numbers */
public void mb_prime(int ceiling, boolean[] isPrime)
{
for(int k = 2; k < isPrime.length; k++)
isPrime[k] = true;
for(int i = 2; i <= (int)Math.floor(Math.sqrt(ceiling)); i++) {
if(isPrime[i]) {
for(int j = 2 * i; j <= ceiling; j += i)
isPrime[j] = false;
}
}//End of loof: for
}//End of method: mb_prime

/** method mb_stat: The method to analyze the scale of each number.
* num is the number to be analyzed
* total is the array to record the total statistics */
public static void mb_stat(int num, int[] total)
{
String str = String.valueOf(num);
int[] stat = new int[10];
String out = new String(str+"中出现了");

for(int i = 0; i < str.length(); i++)
{
int tempIndex = (int)str.charAt(i)-(int)'0';
stat[tempIndex]++;
total[tempIndex]++;
}
for(int i = 0; i < 10; i++)
{
if(stat[i] != 0)
out = out + String.valueOf(stat[i]) + "次" + String.valueOf(i) + " ";
}
System.out.println(out);
}// End of method: mb_stat

public static void main(String[] args)
{
int num = -1, total[];
boolean[] isPrime;

System.out.print("请输入你所要求最大整数:");
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String num_str = in.readLine();
num_str = num_str.trim();
num = Integer.parseInt(num_str);
if(num <= 1)
throw new Exception();
}
catch(Exception e) {
System.err.println("参数错误!");
System.exit(-1);
}

J_Prime app = new J_Prime();
isPrime = new boolean[num+1];
app.mb_prime(num, isPrime);

total = new int[10];
try {
FileWriter f = new FileWriter("data.txt");
PrintWriter pf = new PrintWriter(f);
for(int i = 2; i <= num; i++) {
if(isPrime[i]) {
pf.print(i+" ");
mb_stat(i, total);
}
}
System.out.print("\n这些素数中共出现了");
for(int i = 0; i < 10; i++)
{
if(total[i] != 0)
System.out.print(total[i]+"次"+i+" ");
}
System.out.println();
f.close();
pf.close();
}
catch(IOException e) {
System.out.println("写入文件出错!");
}
}// End of method: main

}//End of class: J_Prime

热点新闻