public class WordDome21 {
public static void main(String[] args) {
int count = 0;
for(int i = 100; i <= 1000; i++){
if(i % 5 == 0 | i % 6 == 0){
System.out.print(i + " ");
count++;
}
if(count == 5){
System.out.println();
count = 0;
}
}
}
}
package com.baqn.demo02;
public class WordDome22 {
public static void main(String[] args) {
double num1 = 1;
double num2 = 3;
double sum = 0;
while(num2 <= 99){
sum += num1/num2;
num1 = num2;
num2 += 2;
}
System.out.println("和为:" + sum);
}
}
package com.baqn.demo02;
public class WordDome23 {
public static void main(String[] args) {
int sum = 0;
int count = 0;
int i = 2;
while (i < 101) {
boolean flag = true;
for(int j = 2; j <= i/2;j++){
if(i % j == 0){
flag = false;
break;
}
}
if(flag){
System.out.print(i + " ");
count++;
sum += i;
if(count == 5){
System.out.println();
count = 0;
}
}
i++;
}
System.out.println("和为:" + sum);
}
}
提示:
(1) 用整型变量water表示水缸里的水“int water = 15;”。
(2) 用整型变量l表示小和尚下山挑水的次数“int l = 0;”。
(3) 分析循环条件(水少于50升),循环操作(水增加5升,挑水次数增加1)。
(4) 套用while循环(或do-while循环)写出代码。
package com.baqn.demo02;
public class WordDome24 {
public static void main(String[] args) {
int water = 15;
int l = 0;
while(water < 50){
l++;
water += 5;
}
System.out.println("需要挑"+l+"次水可以挑满");
}
}
提示:采用逆向思维分析问题。
package com.baqn.demo02;
public class WordDome25 {
public static void main(String[] args) {
double x = 0;
for(int i = 1; i <= 5; i++){
x += 1;
x /= 2;
}
System.out.println("李白的壶里有:" + x + "斗酒");
}
}
package com.baqn.demo02;
public class WordDome26 {
public static void main(String[] args) {
/*
* 开发一个标题为“FlipFlop”的游戏应用程序。
* 它从1计数到100,遇到3的倍数就替换为单词“Flip”,5的倍数就替换为单词“Flop”,
* 既为3的倍数又为5的倍数则替换为单词“FlipFlop”。
*/
int i = 1;
while(i <= 100){
if(i % 3 == 0 && i % 5 != 0){
System.out.println("Flip");
}else if(i % 5 == 0 && i % 3 != 0){
System.out.println("Flop");
}else if(i % 5 == 0 && i % 3 == 0){
System.out.println("FlipFlop");
}else{
System.out.println(i);
}
i++;
}
}
}
例如:690123456789
计算其校验码的过程为:
@前十二位的奇数位和6+0+2+4+6+8=26
@前十二位的偶数位和9+1+3+5+7+9=34
@将奇数和与偶数和的三倍相加26+34*3=128
@取结果的个位数:128的个位数为8
@用10减去这个个位数10-8=2
所以校验码为2
(注:如果取结果的个位数为0,那么校验码不是为10(10-0=10),而是0)
实现方法ean13()计算验证码,输入12位条码,返回带验证码的条码。
例:输入:692223361219输出:6922233612192
package com.baqn.demo02;
import java.util.Scanner;
public class WordDome27 {
public static void main(String[] args) {
//生成13位条形码,Ean-13码规则:第十三位数字是前十二位数字经过计算得到的校验码。
Scanner sc = new Scanner(System.in);
System.out.println("请您依次12位条码:");
int sumOuShuWei = 0;
int sumJiShuWei= 0;
long all12 = 0L;
for(int i = 1; i < 13; i++){
int num = sc.nextInt();
all12 = all12 * 10l + (long)num;
if(num<0||num>9){
System.out.println("请输入0~9直接的整数");
}
if(num %2 == 0){
sumJiShuWei+= num;
}else{
sumOuShuWei += num;
}
}
System.out.println("12数位:" + all12);
int sumJiOu = sumJiShuWei+sumOuShuWei*3;
int geWei = sumJiOu % 10;
int jiaoYanMa = 0;
if(geWei != 0){
jiaoYanMa = 10 -geWei;
}
all12 = all12*10 + jiaoYanMa;
System.out.println("13位数位:" + all12);
}
}
package com.baqn.demo02;
import java.util.Scanner;
public class WordDome28 {
public static void main(String[] args) {
//求两个整数的最大公约数和最小公倍数
Scanner sc = new Scanner(System.in);
System.out.println("请输入俩个整数");
int a = sc.nextInt();
int b = sc.nextInt();
int min = a > b ? b : a;
for(int i = min;i>0;i--){
if(a%i==0 & b%i==0){
min = i;
break;
}
}
int max = a*b/min;
System.out.println("最大公约数:"+min);
System.out.print("最小公倍数:"+max);
}
}
package com.baqn.demo02;
public class WordDome29 {
public static void main(String[] args) {
int count= 0;
for (int shi = 10; shi >= 0; shi--) {
for (int wu = 20; wu >= 0; wu--) {
for (int yi = 100; yi >= 0; yi--) {
if (10 * shi + wu * 5 + yi == 100) {
// System.out.println("10元的" + shi + "张,5元的" + wu + "张,1元的" + yi + "张");
count++;
}
}
}
}
System.out.println("一共有"+count+"种方法");
}
}
package com.baqn.demo02;
public class WordDome30 {
public static void main(String[] args) {
int count = 0;
for (int shi = 1; shi <= 10; shi++) {
for (int wu = 1; wu <= 20; wu++) {
for (int yi = 1; yi <= 100; yi++) {
if (shi * 10 + wu * 5 + yi == 100 && shi + wu + yi == 50) {
count++;
System.out.println("十元的:" + shi + "张,五元的:" + wu
+ ",一元的:" + yi);
}
}
}
}
System.out.println("一共有" + count + "兑换方式");
}
}
package com.baqn.demo02;
public class WordDome31 {
public static void main(String[] args) {
int count = 0;
for (int i = 50; i > 0; i--) {
count++;
if (count % 3 == 0) {
i += 1;
}
}
System.out.println("总共可以喝" + count + "瓶饮料");
}
}