Java入门级题目及源代码(下)
来源:互联网 发布:穆托姆博生涯数据 编辑:程序博客网 时间:2024/05/22 01:44
[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+".");}}}
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 kif (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}
- Java入门级题目及源代码(下)
- Java入门级题目及源代码(上)
- 倒油题目(Java源代码)
- 动态规划经典入门级题目*2及详解
- java经典笔试题目解析(下)
- java入门之俄罗斯方块源代码
- [Activiti]源代码下载及解读入门
- Java入门之一(下)
- poj3624及01背包入门题目
- 群论及Polya计数定理题目入门
- sina网页新闻小偷原理及源代码(java版)
- sina网页新闻小偷原理及源代码(java版) .
- 2048小游戏(Java)源码解析及源代码打包
- java初中级工程师面试笔试题目及心得(全部实战经验)(答案)
- java初中级工程师面试笔试题目及心得(全部实战经验)(答案)
- JAVA经典题目及答案(前30)
- Java面试题目及解答(持续更新)
- ubuntu下下载及编译chromium源代码
- ffmpeg教程
- WP7中Image控件、BitmapImage、WriteableBitmap部分研究
- httpModules与httpHandlers
- invalid plugin detected. Adobe Reader will quit
- 设计模式的汇总
- Java入门级题目及源代码(下)
- 关于oracle 的 glogin.sql
- 九度OJ 1172 哈夫曼树(不用建树)
- 最全的CSS浏览器兼容问题
- WF从O出发
- feature (2)feature属性相关
- 【Android】android中9 path图片
- 18---java中泛型04(泛型的实例)
- sql语言学习点滴(1)