Java入門級題目及源代碼(下)

[Problem 5]

Write a java program that prompts the user to enter an integer using JOptionPane. The program then determines and displays if the integer is even or odd.


import javax.swing.JOptionPane;
public class E1_5 {
	public static void main(String []args){
		String userInput;
		
		userInput=JOptionPane.showInputDialog("Enter Number");
		int num=Integer.parseInt(userInput);
		
		if (num%2==0)System.out.println(num+" is Even Number.");
		else System.out.println(num+" is Odd Number.");
	}
}

[Problem 6]

A Fibonacci sequence is the sequence of numbers 1, 1, 2, 3, 5, 8, 13, 21, 34, and so on, where each number (from the third on) is the sum of the previous two. Create a method that takes an integer as an argument and displays that many Fibonacci numbers starting from the beginning, e.g., If you run java Fibonacci 5 (where Fibonacci is the name of the class) the output will be: 1, 1, 2, 3, 5.

Hint: if you want to use method to compute Fibonacci sequence, define that method as static


public class Fibonacci {
	public static void main(String args[]){
		int n=Integer.parseInt(args[0]);
		int before=1,temp=0,ans=1;
		
		if (n==1)System.out.println("1.");
		else
		{
		  System.out.print("1,");
		  for (int i=1;i<n-1;i++){
			  System.out.print(ans+",");
			  temp=ans;
			  ans=ans+before;
			  before=temp;
		  }
		  System.out.println(ans+".");
		}
	}
}


[Problem 7]

A vampire number has an even number of digits and is formed by multiplying a pair of numbers containing half the number of digits of the result. The digits are taken from the original number in any order. Pairs of trailing zeroes are not allowed. Examples include:

1260 = 21 * 60

1827 = 21 * 87

2187 = 27 * 81

Write a program that finds all the 4-digit vampire numbers. (Suggested by Dan Forhan.)


public class E1_7 {
	public static void main(String args[]){
		int temp=0,t1=0,t2=0,t3=0,t4=0,ans=0;
		int []a=new int[4];
		int [][]b=new int[5][3];
		
		for (int i=1000;i<10000;i++)
		if (i%100!=0){
			temp=i;
			a[0]=temp%10;
			temp=temp/10;
			a[1]=temp%10;
			temp=temp/10;
			a[2]=temp%10;
			temp=temp/10;
			a[3]=temp;
			ans=0;
			for (int j=1;j<=3;j++)
				for (int k=1;k<=3;k++)
				if (j!=k){
					t1=a[0]+a[j]*10;
					t2=a[j]+a[0]*10;
					t3=a[k]+a[6-j-k]*10;
					t4=a[6-j-k]+a[k]*10;
					
					if (t1*t3==i){
						ans++;
						b[ans][1]=t1;
						b[ans][2]=t3;
					}
					if (t1*t4==i){
						ans++;
						b[ans][1]=t1;
						b[ans][2]=t4;
					}
					if (t2*t3==i){
						ans++;
						b[ans][1]=t2;
						b[ans][2]=t3;
					}
					if (t2*t4==i){
						ans++;
						b[ans][1]=t2;
						b[ans][2]=t4;
					}	
			}//for j k
			
			if (ans>0){
				for (int j=1;j<=ans;j++)
					if (b[j][1]>b[j][2]){
						temp=b[j][1];
						b[j][1]=b[j][2];
						b[j][2]=temp;
					}
				for (int j=1;j<=ans;j++)
					for (int k=j+1;k<=ans;k++)
						if (b[j][1]>b[k][1]){
							temp=b[j][1];
							b[j][1]=b[k][1];
							b[k][1]=temp;
							
							temp=b[j][2];
							b[j][2]=b[k][2];
							b[k][2]=temp;
						}
				
				System.out.println(i+" = "+b[1][1]+" * "+b[1][2]);
				for (int j=2;j<=ans;j++)
					if (b[j][1]!=b[j-1][1])
					System.out.println(i+" = "+b[j][1]+" * "+b[j][2]);
			}//if
		}//for i	
	}//main
}
發佈了46 篇原創文章 · 獲贊 33 · 訪問量 61萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章