示例
import java.util.Scanner; /* For 循环结构的使用 1 .循环结构的4要素 (1)初始化条件 (2)循环条件(布尔类型) (3)循环体 (4)迭代条件 2.for循环结构 for (1;2;4){ 3; } */ public class roftest { public static void main(String[] args) { for (int i = 1; i <= 2; i++) { System.out.println("hhh"+i); } int sum = 0; int t = 0; for (int i = 1; i <=100; i++ ){ if (i % 2 == 0 ){ sum += i;//表示sum=sum+i System.out.println(i); t++; } } System.out.println("100以内所有偶数的和为:"+sum+" ,100以内有"+t+"个偶数。"); for (int i = 1; i <= 150; i++){ if (i%3==0&&i%5==0&&i%7==0){ System.out.println(i+" foo biz baz"); }else if (i%3==0&&i%5==0){ System.out.println(i+" foo biz"); }else if (i%3==0&&i%7==0){ System.out.println(i+" foo baz"); }else if (i%5==0&&i%7==0){ System.out.println(i+" biz baz"); }else if (i%3==0) { System.out.println(i+" foo"); }else if (i%5==0) { System.out.println(i+" biz"); }else if (i%7==0){ System.out.println(i+" baz"); } } for (int i = 1; i <= 150; i++){ System.out.print(i + " "); if (i%3 == 0 ) { System.out.print("foo "); //print 不换行;println 自动换行 } if (i%5 ==0) { System.out.print("biz "); } if (i%7 ==0 ){ System.out.print("baz "); } System.out.println(); } Scanner scan = new Scanner(System.in); System.out.println("请输入第一个数:"); int n = scan.nextInt(); System.out.println("请输入第二个数:"); int m = scan.nextInt(); //获取最大公约数 int min = ( m <= n ) ? m : n ; for (int i = min; i>=1 ; i--){ if (m%i ==0 && n%i==0){ System.out.println(i); break; } } //获取两个数最小公倍数 int max = (m>=n)?m:n; for (int i = max; i<=n*m;i++){ if (i%m==0&&i%n==0){ System.out.println(i); break; } } } }
测试
D:jdkjdk-8u131injava.exe "-javaagent:D:ideAIntelliJ IDEA 2019.3.3libidea_rt.jar=6031:D:ideAIntelliJ IDEA 2019.3.3in" -Dfile.encoding=UTF-8 -classpath D:jdkjdk-8u131jrelibcharsets.jar;D:jdkjdk-8u131jrelibdeploy.jar;D:jdkjdk-8u131jrelibextaccess-bridge-64.jar;D:jdkjdk-8u131jrelibextcldrdata.jar;D:jdkjdk-8u131jrelibextdnsns.jar;D:jdkjdk-8u131jrelibextjaccess.jar;D:jdkjdk-8u131jrelibextjfxrt.jar;D:jdkjdk-8u131jrelibextlocaledata.jar;D:jdkjdk-8u131jrelibext ashorn.jar;D:jdkjdk-8u131jrelibextsunec.jar;D:jdkjdk-8u131jrelibextsunjce_provider.jar;D:jdkjdk-8u131jrelibextsunmscapi.jar;D:jdkjdk-8u131jrelibextsunpkcs11.jar;D:jdkjdk-8u131jrelibextzipfs.jar;D:jdkjdk-8u131jrelibjavaws.jar;D:jdkjdk-8u131jrelibjce.jar;D:jdkjdk-8u131jrelibjfr.jar;D:jdkjdk-8u131jrelibjfxswt.jar;D:jdkjdk-8u131jrelibjsse.jar;D:jdkjdk-8u131jrelibmanagement-agent.jar;D:jdkjdk-8u131jrelibplugin.jar;D:jdkjdk-8u131jrelib esources.jar;D:jdkjdk-8u131jrelib t.jar;G:java_ideachenxioutproductionchenxi com.chenxi.java.roftest hhh1 hhh2 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 100以内所有偶数的和为:2550 ,100以内有50个偶数。 3 foo 5 biz 6 foo 7 baz 9 foo 10 biz 12 foo 14 baz 15 foo biz 18 foo 20 biz 21 foo baz 24 foo 25 biz 27 foo 28 baz 30 foo biz 33 foo 35 biz baz 36 foo 39 foo 40 biz 42 foo baz 45 foo biz 48 foo 49 baz 50 biz 51 foo 54 foo 55 biz 56 baz 57 foo 60 foo biz 63 foo baz 65 biz 66 foo 69 foo 70 biz baz 72 foo 75 foo biz 77 baz 78 foo 80 biz 81 foo 84 foo baz 85 biz 87 foo 90 foo biz 91 baz 93 foo 95 biz 96 foo 98 baz 99 foo 100 biz 102 foo 105 foo biz baz 108 foo 110 biz 111 foo 112 baz 114 foo 115 biz 117 foo 119 baz 120 foo biz 123 foo 125 biz 126 foo baz 129 foo 130 biz 132 foo 133 baz 135 foo biz 138 foo 140 biz baz 141 foo 144 foo 145 biz 147 foo baz 150 foo biz 1 2 3 foo 4 5 biz 6 foo 7 baz 8 9 foo 10 biz 11 12 foo 13 14 baz 15 foo biz 16 17 18 foo 19 20 biz 21 foo baz 22 23 24 foo 25 biz 26 27 foo 28 baz 29 30 foo biz 31 32 33 foo 34 35 biz baz 36 foo 37 38 39 foo 40 biz 41 42 foo baz 43 44 45 foo biz 46 47 48 foo 49 baz 50 biz 51 foo 52 53 54 foo 55 biz 56 baz 57 foo 58 59 60 foo biz 61 62 63 foo baz 64 65 biz 66 foo 67 68 69 foo 70 biz baz 71 72 foo 73 74 75 foo biz 76 77 baz 78 foo 79 80 biz 81 foo 82 83 84 foo baz 85 biz 86 87 foo 88 89 90 foo biz 91 baz 92 93 foo 94 95 biz 96 foo 97 98 baz 99 foo 100 biz 101 102 foo 103 104 105 foo biz baz 106 107 108 foo 109 110 biz 111 foo 112 baz 113 114 foo 115 biz 116 117 foo 118 119 baz 120 foo biz 121 122 123 foo 124 125 biz 126 foo baz 127 128 129 foo 130 biz 131 132 foo 133 baz 134 135 foo biz 136 137 138 foo 139 140 biz baz 141 foo 142 143 144 foo 145 biz 146 147 foo baz 148 149 150 foo biz 请输入第一个数: 12 请输入第二个数: 56 4 168 Process finished with exit code 0
输出满足个位十位个位数的立方和等于这个数的本身;例如153=1*1*1+5*5*5+3*3*3
public class roftest { public static void main(String[] args) { for (int i= 100; i<1000;i++){ int b=i/100; int s=i%100/10; int g=i%10; if (i == (b*b*b+s*s*s+g*g*g)) { System.out.println(i); } } } }
测试
153 370 371 407