首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

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

第 21 题:

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;

}

}

第 22 题:

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;

}

}

第 23 题:

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;

}

}

第 24 题:

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 + " 秒");

}

}

第 25 题:

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;

}

}

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20180611G1XBAD00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券