# 第二次作业

1.编写一个程序，定义圆的半径,求圆的面积.

package text1;
import java.util.Scanner;
public class HelloWorid{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("输入圆的半径: ");
double r = s.nextDouble();
double ss = 3.14 * r * r;
System.out.println("圆的面积是: "+ss);
}
}

2.华氏温度和摄氏温度互相转换

package text1;
import java.util.Scanner;
public class HelloWorid{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("输入华氏度: ");
Scanner a = new Scanner(System.in);
double h = a.nextDouble();
System.out.println("摄氏度: " + (h - 32.00) * 5.00 / 9.00);
System.out.println("输入摄氏度: ");
Scanner b = new Scanner(System.in);
double ss = b.nextDouble();
System.out.println("华氏度: " + (((ss * 9.00)/5.00  + 32.00)));
}
}

3.已知a,b均是整型变量，写出将a,b两个变量中的值互换的程序（int a=5,b=8）

package text1;
public class HelloWorid{
public static void main(String[] args) {
int a = 5;
int b = 8;
int x= a;
a = b;
b = x;
System.out.println(a + "\t" + b);
}
}

4.定义一个任意的5位整数，将它保留到百位，无需四舍五入

package text1;
import java.util.Scanner;
public class HelloWorid{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("输入五位整数: ");
int x = s.nextInt();
int w = x / 100 * 100;
System.out.println("保留到百位: " + w);
}
}

5.输入一个0～1000的整数，求各位数的和

package text1;
import java.util.Scanner;
public class HelloWorid{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("请输入数字: ");
int x = s.nextInt();
if (x>1000 || x<0) {
System.out.println("请重新输入");
return;}
else if (x/1000 == 1) {
System.out.println("和=1");
return;}
else if (x/100 != 0) {
System.out.println("和= " + (x/100+x%100/10+x%10));
return;}
else if (x/10 != 0) {
System.out.println("和=" + (x/10 + x%10));
return;}
else {
System.out.println("和= " + x);
}
}
}

6.定义一个任意的大写字母A~Z，转换为小写字母；定义一个任意的小写字母a~z，转换为大写字母

package text1;
public class HelloWorid{
public static void main(String[] args) {
String str1 = "A";
String str2 = "b";
String str11 = str1.toLowerCase();
String str22 = str2.toUpperCase();
System.out.println(str1 + ": " + str11);
System.out.println(str2 + ": " + str22);
}
}

posted @ 2021-03-12 11:15  偷一夜星光  阅读(54)  评论(0编辑  收藏  举报