java练习题
编程实现例题
1)某人有100000元,每经过一次路口,需要交费,规则如下:
? 当现金>50000时,每次交5%;
? 当现金<= 50000时,每次交1000
? 编程计算该人可以经过多少次路口,要求:使用while + break 方式完成。
public class one{
public static void mian(){
int count = 0;
double money = 100000;
while(true){
if(money > 50000){
money = money * (1 - 0.05);
count++;
}else if(money >= 1000 && money <= 50000){
money -= 1000;
count++;
}else if(money < 1000)
break;
}
System.out.println("100000元经过" +count +"次路口。");
}
}
2)实现判断一个整数,属于那个范围:大于0;小于0;等于0;
public class two{
public static void mian(){
Scanner myscanner = new Scanner(System.in);
int num = myscanner.nextInt();
if(num > 0){
System.out.println(num + "大于0");
}else if(num == 0){
System.out.println(num + "等于0");
}else{
System.out.println(num + "小于0");
}
}
}
3)判断一个年份是否为闰年
public class three{
public static void mian(){
Scanner myscanner = new Scanner(System.in);
int year = myscanner.nextInt();
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
System.out.println(year + "是闰年");
}else{
System.out.println(year + "不是闰年");
}
}
}
4)判断一个整数是否是水仙花数,所谓水仙花数是指一个三位数,其各位上数字立方和等于其本身。例如:153 = 1 * 1 * 1 + 5 * 5 *5 + 3 * 3 * 3
public class four{
public static void mian(){
Scanner myscanner = new Scanner(System.in);
int num = myscanner.nextInt();
int a = num % 10;
int b = num / 10 % 10;
int c = num / 100;
int sum =0;
sum = a * a * a + b * b * b + c * c * c;
if(sum == num)
System.out.println(num + "是水仙花数");
else
System.out.println(num + "不是水仙花数");
}
}
5)看看下面代码输出什么?
public class five{
public static void mian(){
int m = 0,n = 3;
if(m > 0){
if(n > 2)
System.out.println("OK1");
else
System.out.println("OK2");
}
}
}
6)输出1 - 100之间的不能被5整除,每五个一行。
public class six{
public static void mian(){
int count = 0;
for(int i = 1;i <= 100;i++){
if(i % 5 != 0){
System.out.print(i + "\t");
count++;
if(count % 5 == 0)
System.out.println();
}
}
}
}
7)输出小写的a - z 以及大写的 Z - A.
public class seven{
public static void mian(){
for(char i = 'a';i <= 'z';i++)
System.out.print(i + "\t");
System.out.println();
for(char i = 'Z';i >= 'A';i--)
System.out.print(i + "\t");
}
}
8)求出1 - 1/2 + 1/3 - 1/4…1/100的和
public class egiht{
public static void mian(){
double sum = 0;
int sign = 1;
double a = 1;
for(int i = 1;i <= 100;i++){
a = sign * (1.0/i);
sign = - sign;
sum += a;
}
System.out.print("和为" + sum);
}
}
public class eight{
public static void mian(){
double sum = 0;
for(int i = 1;i <= 100;i++){
if(i % 2 != 0)
sum += 1.0 / i;
else
sum -= 1.0 / i;
}
System.out.print(sum);
}
}
9)求 1 + (1 + 2)+(1 + 2 + 3)+(1 + 2 + 3 + 4)+…(1 + 2 +3 +…+100)的和
public class nine{
public static void mian(){
int num = 0;
int sum = 0;
for(int i = 1;i <= 100;i++){
num += i;
sum += num;
}
System.out.print(sum);
}
}
public class nine{
public static void mian(){
double sum = 0;
for(int i = 1;i <= 100;i++){
for(int j = 1;j <= i;j++){
sum += j;
}
}
System.out.print(sum);
}
}
打印空心金字塔
public class pyramid{
public static void mian(){
int sumline = 4;
int i;
int k;
int j;
for(i = 1;i <= sumline;i++){
for(k = 1;k <= sumline - i;k++)
System.out.print(" ");
for(j = 1;j <= 2 * i - 1;j++){
if(j == 1 || i == sumline || j == 2 * i - 1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}
打印空心菱形
public class rhombus{
public static void mian(){
int sumline = 4;
int i;
int k;
int j;
for(i = 1;i <= sumline;i++){
for(k = 1;k <= sumline - i;k++)
System.out.print(" ");
for(j = 1;j <= 2 * i - 1;j++){
if(j == 1 || j == 2 * i - 1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
int lastline = sumline -1;
for(i = lastline;i >= 1;i--){
for(k = lastline;k >= i;k--)
System.out.print(" ");
for(j = 1;j <= 2 * i - 1;j++){
if(j == 1 || j == 2 * i -1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println("");
}
}
}
|