*6.4(反向显示一个整数)使用下面的方法头编写方法,反向显示一个整数:
public static void reverse(int number)
例如:reverse(3456)返回6543。编写一个测试程序,提示用户输入一个整数,然后显示它的反向数。
1 import java.util.*; 2 3 public class o { 4 public static void reverse (int number) { 5 int eve,ano = number; 6 int reverse = 0; 7 while (ano != 0) { 8 eve = ano % 10; 9 reverse = reverse * 10 + eve; 10 ano /= 10; 11 } 12 System.out.println(reverse); 13 } 14 public static void main(String[] args) { 15 Scanner input = new Scanner (System.in); 16 System.out.print("请输入一个整数:"); 17 int number = input.nextInt(); 18 reverse(number); 19 } 20 }
6.5(对三个数排序)
1 import java.util.*; 2 public class o { 3 public static void displaySortedNumber(double num1,double num2,double num3) { 4 double max,mid,min; 5 max = (num1 > num2) ? num1 : num2; 6 max = (max > num3) ? max : num3; 7 min = (num1 > num2) ? num2 : num1; 8 min = (min > num3) ? num3 : min; 9 mid = num1 + num2 + num3 - max -min; 10 System.out.println(max+" "+mid+" "+min); 11 } 12 public static void main(String[] args) { 13 System.out.print("请输入三个整数"); 14 Scanner input = new Scanner (System.in); 15 double num1=input.nextDouble(); 16 double num2=input.nextDouble(); 17 double num3=input.nextDouble(); 18 displaySortedNumber(num1,num2,num3); 19 } 20 }
6.6(显示图案)
1 import java.util.*; 2 public class o { 3 public static void displayPattern(int n) { 4 int i = 0;//行数 5 int k; 6 for( ;i <= n;i++) { 7 for(k = 1;k < 2*(n+1-i);k++ ) { 8 System.out.print(" "); 9 } 10 for(k = i;k > 0;k--) { 11 System.out.print(k + " "); 12 } 13 System.out.print(" "); 14 } 15 } 16 public static void main(String[] args) { 17 System.out.print("请输入一个行数n:"); 18 Scanner input = new Scanner(System.in); 19 int n = input.nextInt(); 20 displayPattern(n); 21 } 22 }
*6.7(财务应用程序:计算未来投资价值)
1 import java.util.*; 2 public class o { 3 public static double futureInvetmentValue(double investmentAmount,double monthlyInterestRate,int years) { 4 double investLater = investmentAmount * Math.pow((1+monthlyInterestRate/1200), years * 12); 5 return investLater; 6 } 7 public static void main(String[] args) { 8 @SuppressWarnings("resource") 9 Scanner input = new Scanner (System.in); 10 double investmentAmount, monthlyInterestRate; 11 int years,i; 12 System.out.print("The amout invested:"); 13 investmentAmount = input.nextDouble(); 14 System.out.print("Annual interest rate:"); 15 monthlyInterestRate = input.nextDouble(); 16 System.out.println("Years Future Values"); 17 for(i=0;i<31;i++) { 18 System.out.printf(i+" %.2f ",futureInvetmentValue(investmentAmount, monthlyInterestRate,i)); 19 } 20 } 21 }