JAVA语⾔程序设计(基础篇)第⼗版——第六章⽅法(参考
答案)
(6.2~6.9节)+(6.10~6.11节)+(6.10~6.12节)
(综合题还未写,以后会更新)
(6.2~6.9节)
6.1(数学:五⾓数)
import java.util.Scanner;
public class S1 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("请问您想要显⽰前⼏个个五⾓数:");
int Int();// 显⽰前 m个五⾓数
input.close();
int count=0;
int shu=0;
for(int n=1; n<=m; n++) {
count++;
shu=getPentagonalNumber(n);
if(count%10==0) {
System.out.println(shu);
}
else {
System.out.print(shu + " ");
}
}
}
//得到五⾓数的⽅法
public static int getPentagonalNumber(int n) {
return n*(3*n-1)/2 ;
}
}
*6.2(求⼀个整数各位数字之和)
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("请问您输⼊⼀个整数:");
int Int();
input.close();
int total=sumDigit(n);
System.out.println("这个整数的个位数之和为:" + total); }
public static int sumDigit(long n) {
int sum=0;
int count=0;
while(n%10!=0) {
count=(int) (n%10);
sum=sum+count;
n= n/10;
}
return sum;
}
}
**6.3(回⽂整数)
public static void main(String[] args) {
//回⽂数,如:(单数)1234321 、(偶数)124421  Scanner input=new Scanner(System.in);
System.out.print("请问您输⼊⼀个整数:");
int Int();
input.close();
if(isPalindrome(number))
System.out.println("Yes! "+number+" 是回⽂数.");
else
System.out.println("No. "+number+" 不是回⽂数."); }
//该⽅法将⼀个整数反向倒置显⽰。例如,456——>654 public static int reverse(int number) {
String s="";
while(number%10!=0) {
s= s + number%10;
number=number/10;
}
int count= Integer.parseInt(s);
return count;
}
public static boolean isPalindrome(int number) {
if(number==reverse(number))
return true;
else
return false;
}
}
*6.4(反向显⽰⼀个整数)
public static void main(String[] args) {
Scanner input=new Scanner(System.in);  System.out.print("请问您输⼊⼀个整数:");  int Int();
input.close();
reverse(number);
}
public static void reverse(int number) {
String s="";
while(number%10!=0) {
s= s + number%10;
number=number/10;
}
int count= Integer.parseInt(s);
System.out.print(count);
java程序设计基础视频}
}
*6.5(对三个数排序)
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("请输⼊三个数:");
double Double();
double Double();
double Double();
displaySortedNumbers(n1,n2,n3);
}
private static void displaySortedNumbers(double n1, double n2, double n3) {  if(n1<n2) {
// n1 n2
if(n1>n3) {
//n3-n1-n2
System.out.print(n3+" "+n1+" "+n2);
}
else if(n1<n3){
//还要拿3和2⽐较
if(n2>n3)
System.out.println(n1+" "+n3+" "+n2);
else
System.out.println(n1+" "+n2+" "+n3);
}
}
else if(n1>n2) {
// n2 n1
if(n2>n3) {
System.out.println(n3+" "+n2+" "+n1);
}
else if(n2<n3){
/
/拿1和3⽐
if(n1<n3) {
System.out.println(n2+" "+n1+" "+n3);
}
else {
System.out.println(n2+" "+n3+" "+n1);
}
}
}
}
}
*6.6(显⽰图案)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。