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
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值