第一次上机作业

1、已知a,b均是整型变量,写出将a,b两个变量中 的值互换的程序。(知识点:变量和运算符综合 应用)

package a;

public class ds {

public static void main(String[] args) {
// TODO Auto-generated method stub
int a = 1, b = 2;
int t = a;
a = b;
b = t;
System.out.println(a + " " + b);
}

}

 

2、给定一个0~1000的整数,求各位数的和,例 如345的结果是3+4+5=12注:分解数字既可以先 除后模也可以先模后除(知识点:变量和运算符 综合应用) 

package a;

import java.util.Scanner;

public class ds {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int t;
int a, b, c, d, sum;
System.out.println("请输入0~1000的数字");
t = in.nextInt();
a = t / 1000;
b = t / 100 % 10;
c = t / 10 % 10;
d = t % 10;
sum = a + b + c + d;
System.out.println("结果为" + sum);
}

}

3、华氏温度和摄氏温度互相转换,从华氏度变成 摄氏度你只要减去32,乘以5再除以9就行了,将 摄氏度转成华氏度,直接乘以9,除以5,再加上 32即行。

package a;

import java.util.Scanner;

public class ds {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int f = 0;
float a, t;
System.out.println("华氏转摄氏请按1,摄氏转华氏请按2");
f = in.nextInt();
if (f != 1) {
System.out.println("请输入华氏温度");
a = in.nextFloat();
t = (a - 32) * 5 / 9;
System.out.println("摄氏温度为" + t);
} else {
System.out.println("请输入摄氏温度");
a = in.nextFloat();
t = a * 9 / 5 + 32;
System.out.println("华氏温度为" + t);
}
}

}

 

4、给定一个任意的大写字母A~Z,转换为小写字 母。 (知识点:变量和运算符) 

package a;

import java.util.Scanner;

public class ds {

public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("请输入一个大写字母:");
Scanner sc = new Scanner(System.in);
String c = sc.next();
char a = c.charAt(0);
System.out.println((char)(int)(a+32));
}

}

原文地址:https://www.cnblogs.com/sigure0428/p/12523163.html