JAVA编程题全集(100题及答案) 下载本文

int n = s.nextInt(); int k=2;

while(k <= n) {

else k++; } } } 【程序5】

题目:利用条件运算符的嵌套来完成此题:学习成绩> =90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。 import java.util.*; public class lianxi05 {

public static void main(String[] args) { int x;

char grade;

Scanner s = new Scanner(System.in);

x = s.nextInt(); grade = x >= 90 ? 'A' : x >= 60 ? 'B' :'C'; } }

【程序6】

题目:输入两个正整数m和n,求其最大公约数和最小公倍数。

/**在循环中,只要除数不等于0,用较大数除以较小的数,将小的一个数作为下一轮循环的大数,取得的余数作为下一轮循环的较小的数,如此循环直到较小的数的值为0,返回较大的数,此数即为最大公约数,最小公倍数为两数之积除以最大公约数。* /

import java.util.*;

public class lianxi06 {

public static void main(String[] args) { int a ,b,m;

Scanner s = new Scanner(System.in);

a = s.nextInt();

b = s.nextInt();

deff cd = new deff(); m = cd.deff(a,b); int n = a * b / m; } }

class deff{

public int deff(int x, int y) { int t;

if(x < y) { t = x; x = y; y = t; }

while(y != 0) { if(x == y) return x; else {

int k = x % y; x = y; y = k; } }

return x; } }

【程序7】

题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 import java.util.*; public class lianxi07 {

public static void main(String[] args) { int digital = 0; int character = 0; int other = 0; int blank = 0;

char[] ch = null;

Scanner sc = new Scanner(System.in); String s = sc.nextLine(); ch = s.toCharArray();

for(int i=0; i= '0' && ch <= '9') { digital ++;

} else if((ch >= 'a' && ch <= 'z') || ch > 'A' && ch <= 'Z') { character ++;

} else if(ch == ' ') { blank ++; } else { other ++; } }

} }

【程序8】

题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。 import java.util.*; public class lianxi08 {

public static void main(String[] args) { long a , b = 0, sum = 0;

Scanner s = new Scanner(System.in);

a = s.nextInt();

int n = s.nextInt(); int i = 0;

while(i < n) { b = b + a;

sum = sum + b; a = a * 10; ++ i; } } }

例如【程序9】

题目:一个数如果恰好等于它的因子之和,这个数就称为 \完数 \。例如6=1+2+3.编程 找出1000以内的所有完数。 public class lianxi09 {

public static void main(String[] args) {

for(int i=1; i<1000; i++) { int t = 0;

for(int j=1; j<= i/2; j++) { if(i % j == 0) { t = t + j; } }

if(t == i) {

} } }

【程序10】

题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地时,共经过多少米?第10次反弹多高? public class lianxi10 {

public static void main(String[] args) { double h = 100,s = 100; for(int i=1; i<10; i++) { s = s + h; h = h / 2; } } }

【程序11】

题目:有1、2、3、4四个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? public class lianxi11 {

public static void main(String[] args) { int count = 0;

for(int x=1; x<5; x++) { for(int y=1; y<5; y++) { for(int z=1; z<5; z++) {

if(x != y && y != z && x != z) { count ++;

} } } } } }

【程序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%提成,从键盘输入当月利润,求应发放奖金总数? import java.util.*; public class lianxi12 {

public static void main(String[] args) { double x = 0,y = 0;

Scanner s = new Scanner(System.in); x = s.nextInt();

if(x > 0 && x <= 10) { y = x * 0.1;

} else if(x > 10 && x <= 20) { y = 10 * 0.1 + (x - 10) * 0.075; } else if(x > 20 && x <= 40) {

y = 10 * 0.1 + 10 * 0.075 + (x - 20) * 0.05; } else if(x > 40 && x <= 60) {

y = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + (x - 40) * 0.03; } else if(x > 60 && x <= 100) {