showInputDialog("请输入N的值(输入exit退出):"); int N; N=0; try{ N=Integer.parseInt(str); } catch(NumberFormatException e){ e.printStackTrace(); } str=(N>90?"A":(N>60?"B":"C")); System.out.println(str); } }
【程序 6】 题目:输入两个正整数 m 和 n,求其最大公约数和最小公倍数。 1.
程序分析:利用辗除法。
最大公约数: public class CommonDivisor{ public static void main(String args[]) { commonDivisor(24,32); } static int commonDivisor(int M, int N) { if(N<0||M<0) { System.out.println("ERROR!"); return -1; } if(N==0) { System.out.println("the biggest common divisor is :"+M); return M; }
return commonDivisor(N,M%N); } } 最小公倍数和最大公约数: import java.util.Scanner; public class CandC { //下面的方法是求出最大公约数 public static int gcd(int m, int n) { while (true) { if ((m = m % n) == 0) return n; if ((n = n % m) == 0) return m; } } public static void main(String args[]) throws Exception { //取得输入值 //Scanner chin = new Scanner(System.in); //int a = chin.nextInt(), b = chin.nextInt(); int a=23; int b=32; int c = gcd(a, b); System.out.println("最小公倍数:" + a * b / c + "\n最大公约数:" + c); } }
【程序 7】 题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 1.程序分析:利用 while 语句,条件为输入的字符不为 '\n '.
import java.util.Scanner; public class ex7 { public static void main(String args[]) { System.out.println("请输入字符串:"); Scanner scan=new Scanner(System.in); String str=scan.next(); String E1="[\u4e00-\u9fa5]"; String E2="[a-zA-Z]"; int countH=0; int countE=0; char[] arrChar=str.toCharArray(); String[] arrStr=new String[arrChar.length]; for (int i=0;i
xml:namespace prefix = arrChar.length ;i++ ) { arrStr[i]=String.valueOf(arrChar[i]); } for (String i />
{ countH++; } if (i.matches(E2)) { countE++; } } System.out.println("汉字的个数"+countH); System.out.println("字母的个数"+countE); } }
【程序 8】 题目:求 s=a+aa+aaa+aaaa+aa...a 的值,其中 a 是一个数字。例如 2+22+222+2222+22222(此时共有 5 个数相加),几 个数相加有键盘控制。 1.程序分析:关
键是计算出每一项的值。
import java.io.*; public class Sumloop { public static void main(String[] args) throws IOException { int s=0; String output=""; BufferedReader stadin = new BufferedReader(new InputStreamReader(System.in)); System.out.println("请输入a的值"); String input =stadin.readLine(); for(int i =1;i<=Integer.parseInt(input);i++) { output+=input; int a=Integer.parseInt(output); s+=a; } System.out.println(s); } } 另解: import java.io.*; public class Sumloop { public static void main(String[] args) throws IOException { int s=0; int n; int t=0; BufferedReader stadin = new BufferedReader(new InputStreamReader(System.in)); String input = stadin.readLine(); n=Integer.parseInt(input); for(int i=1;i<=n;i++){ t=t*10+n; s=s+t; System.out.println(t); } System.out.println(s);
} }
【程序 9】 题目:一个数如果恰好等于它的因子之和,这个数就称为 "完数 "。例如 6=1+2+3.编程 找出 1000 以内的所有完数。
public class Wanshu { public static void main(String[] args) { int s; for(int i=1;i<=1000;i++) { s=0; for(int j=1;j 【程序 10】 题目:一球从 100 米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 次反弹多高? 第 10 次落地时,共经过多少米?第 10
public class Ex10 { public static void main(String[] args) { double s=0; double t=100; for(int i=1;i<=10;i++) { s+=t; t=t/2; } System.out.println(s); System.out.println(t); } }
【程序 11】 题目:有 1、2、3、4 个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 1.程序分析:可填在百位、十位、个位的数字都是 1、2、3、4。组成所有的排列后再去 掉不满足条件的排列。
public class Wanshu { public static void main(String[] args) { int i=0; int j=0; int k=0; int t=0; for(i=1;i<=4;i++) for(j=1;j<=4;j++) for(k=1;k<=4;k++) if(i!=j &;&; j!=k &;&; i!=k) {t+=1;
System.out.println(i*100+j*10+k); } System.out.println (t); } }
【程序 12】 题目:企业发放的奖金根据利润提成。利润(I)低于或等于 10 万元时,奖金可提 10%;利润高于 10 万元,低于 20 万元时,低 于 10 万元的部分按 10%提成,高于 10 万元的部分,可可提成 7.5%;20 万到 40 万之间时,高于 20 万元的部分,可提成 5%;40 万到 60 万之间时高于 40 万元的部分,可提成 3%;60 万到 100 万之间时,高于 60 万元的部分,可提成 1.5%,高于 100 万元时,超过 100 万元的 部分按 1%提成,从键盘输入当月利润 I,求应发放奖金总数? 1.程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。
import java .util.*; public class test { public static void main (String[]a