1. 程式人生 > >Java_打印從一個數到另一個數之間的整數,並每5個為一行

Java_打印從一個數到另一個數之間的整數,並每5個為一行

string 輸出 ext sys pub scanner ann font 判斷

 1 import java.util.Scanner;
 2 
 3 public class Dayin_100 {
 4     public static void main(String[] args) {
 5         System.out.println("請輸入需要打印整數的第一個數:");
 6         Scanner San1 = new Scanner(System.in);
 7         System.out.println("請輸入需要打印整數的尾數:");
 8         Scanner San = new Scanner(System.in);
9 int Num1 = San1. nextInt(); 10 int Num = San. nextInt(); 11 Dayin(Num1,Num); 12 } 13 public static void Dayin(int sum1,int sum){ //封裝打印方法 14 int x = 0; 15 for (int i =sum1; i <= sum; i++) { 16 int a= i%10; //判斷個位是否有數字9 17 int
b= i/10%10; //判斷十位是否有數字9 18 int c= i/10/10%10; //判斷百位是否有數字9 19 if (a!=9 && b!=9 && c!=9) { //當都不含有9 時,進入打印 20 System.out.print(i+"\t"); 21 x++; //記錄一個索引值,當索引每到5時,換行 22 if(x%5==0){ 23 System.out.println();
24 } 25 } 26 } 27 } 28 }

輸出結果為:

請輸入需要打印整數的第一個數:5
請輸入需要打印整數的尾數:50

5 6 7 8 10
11 12 13 14 15
16 17 18 20 21
22 23 24 25 26
27 28 30 31 32
33 34 35 36 37
38 40 41 42 43
44 45 46 47 48
50

Java_打印從一個數到另一個數之間的整數,並每5個為一行