第二章作业.

2.6

import java.util.Scanner;
  public class Red{
  public static void main(String[] args){
  Scanner input =new Scanner(System.in);
  System.out.println("Enter a number between 0 and 1000: ");
  int number=input.nextInt();
  int x,y,z,sun;
  x=number/100;
  y=(number%100)/10;
  z=(number%100)%10;
  sun=x+y+z;
  System.out.println("the sun of the digits is" +sun);
 }
}

2.15

import java.util.Scanner;
public class ComputeDistance{
  public static void main(String[] args){
  double number;
  Scanner input =new Scanner(System.in);
  System.out.print("Enter x1 and y1:");
  double x1 =input.nextDouble();
  double y1 =input.nextDouble();
  System.out.print("Enter x2 and y2:");
  double x2 =input.nextDouble();
  double y2 =input.nextDouble();
  number =(x2-x1)*(x2-x1)+(y2-y1)*(y2-y1);
  double distance =Math.pow(number,0.5);
  System.out.println("the distance between the two points is" +distance);
 }
}

原文地址:https://www.cnblogs.com/gaojie77/p/7536687.html