Java 语言程序设计第 6 章课后习题编程代码 05

import java.util.Scanner;

public class Six21{

public static int getNumber(char uppercaseLetter){

if(uppercaseLetter >= 'A' && uppercaseLetter

return 2;

else if(uppercaseLetter >= 'D' && uppercaseLetter

return 3;

else if(uppercaseLetter >= 'G' && uppercaseLetter

return 4;

else if(uppercaseLetter >= 'J' && uppercaseLetter

return 5;

else if(uppercaseLetter >= 'M' && uppercaseLetter

return 6;

else if(uppercaseLetter >= 'P' && uppercaseLetter

return 7;

else if(uppercaseLetter >= 'T' && uppercaseLetter

return 8;

else

return 9;

}

}

import java.util.Scanner;

public class Six22{

public static void main(String[] args){

Scanner input = new Scanner(System.in);

long n = input.nextLong();

//double lastGuess = input.nextDouble();

System.out.println(n + " 的平方根是 " + sqrt(n));

}

public static double sqrt(long n){

double sqrt = 0;

for(double lastGuess = 0.1; lastGuess >= 0; lastGuess += 0.01){

double nextGuess = (lastGuess + n / lastGuess) / 2;

if(nextGuess - lastGuess

sqrt = nextGuess;

break;

}

}

return sqrt;

}

}

import java.util.Scanner;

}

public static int count(String str, char a){

int count = 0;

for(int i = 0; i

if(str.charAt(i) - a == 0)

count++;

}

return count;

}

}

public static void printCurrentTime(long millis){

long totalseconds = millis / 1000;

long seconds = totalseconds % 60;

long totalminutes = totalseconds / 60;

long minutes = totalminutes % 60;

long totalhours = totalminutes / 60;

long hours = totalhours % 24;

long totaldays = totalhours / 24;

if((hours + 8) >= 24){

hours = (hours + 8) - 24;

totaldays = totaldays + 2;

}else{

hours = hours + 8;

totaldays = totaldays + 1;

}

long years = 1970;

long month = 0;

long days = 0;

for(long year = 1970; year >= 1970; year++){

if(totaldays

years = year;

totaldays = totaldays;

break;

}else{

if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0){

years += 1;

totaldays -= 366;

/*if(totaldays

year = year;

totaldays = totaldays;

break;

}*/

}else{

years += 1;

totaldays -= 365;

/*if(totaldays

year = year;

totaldays = totaldays;

break;

}*/

}

}

}

for(month = 1;month

if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){

if(totaldays

month = month;

days = totaldays;

break;

}else{

totaldays -= 31;

}

}else if(month == 2){

if(years % 4 == 0 && years % 100 != 0 || years % 400 == 0){

if(totaldays

month = month;

days = totaldays;

break;

}else{

totaldays -= 29;

}

}else{

if(totaldays

month = month;

days = totaldays;

break;

}else{

totaldays -= 28;

}

}

}else{

if(totaldays

month = month;

days = totaldays;

break;

}else{

totaldays -= 30;

}

}

}

System.out.print("现在是北京时间: " + years + " 年 " + month + " 月 " + days + " 日 " + hours + " 时 " + minutes + " 分 " + seconds + " 秒");

}

}

import java.util.Scanner;

public class Six25{

public static void main(String[] args){

Scanner input = new Scanner(System.in);

long millis = input.nextLong();

System.out.println(millis + " 转换成时分秒格式为 " + convertMillis(millis));

}

public static String convertMillis(long millis){

String result = "";

long totalseconds = millis / 1000;

long totalminutes = totalseconds / 60;

long seconds = totalseconds % 60;

long totalhours = totalminutes / 60;

long minutes = totalminutes % 60;

result = totalhours + ":" + minutes + ":" + seconds;

return result;

}

}

• 发表于: