2021.03.29上机课
题目描述:
代码:
3.9 检查ISBN-10
package Practice;
import java.util.Scanner;
public class Ch03Q09 {
public static void main(String[] args) {
while(true)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the first 9 digits of an ISBN as integer: ");
int number = input.nextInt();
int checksum = 0;
// 校验和计算
for(int i = 1; i <= 9; i ++ ) checksum += ( (number / (int) Math.pow(10, 9 - i) ) % 10) * i;
checksum %= 11;
System.out.print("The ISBN-10 number is ");
// 处理前导0与该数
for(int i = 8; i >= 0; i -- )
{
if(number / (int) Math.pow(10, i) == 0) System.out.print("0");
}
System.out.print(number);
// 输出最后一位
if (checksum == 10)
System.out.print("X");
else
System.out.print(checksum);
System.out.println();
}
}
}
// 013601267
// 013031997
5.19 打印金字塔
package Practice;
import java.util.Scanner;
public class Ch05Q19 {
public static void main(String[] args) {
while(true){
long number = 0;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of lines:");
int counts = scanner.nextInt();
long d = (long)Math.pow(2, counts);
int t = 0;
while(d != 0)
{
d /= 10;
t ++;
}
t += 2;
String fmt = "%" + t + "s";
String fmt1 = "%" + t + "d";
// System.out.println(fmt + fmt1);
for (int row = 0; row <= counts - 1; row ++ ) {
// 打印空白
for (int col = 1; col <= counts - 1 - row; col ++ )
System.out.printf(fmt, " ");
// 打印左半部分
for(int col = 0; col <= row; col ++ )
{
number = (long)Math.pow(2, col);
System.out.printf(fmt1, number);
}
// 打印右半部分
for(int col = row - 1; col >= 0; col --)
{
number = (long)Math.pow(2, col);
System.out.printf(fmt1, number);
}
System.out.print('\n');
}
}
}
}