1.已知a,b均是整型变量,写出将a,b两个变量中的值互换的程序(知识点:变量和运算符综合应用)
方法1(使用中间变量):
package com.firstday.base;
public class Test01{
public static void main(String [] args){
int x=2;
int y=3;
int temp=x;
x=y;
u=temp;
System.out.println(x+","+"y");
}
}
方法2(不使用中间变量):
package com.firstday.base;
public class Test01{
public static void main(String [] args){
int x=2;
int y=3;
x=y-x;
y=y-x;
x=x+y;
System.out.prinln(x+","+y);
}
}
方法3(使用异或):
package com.firstday.base;
public class Test01{
public static void main(String [] args){
int x=2;
int y=3;
x^=y;
y^=x;
x^=y;
System.out.println(x+","+y);
}
}
2.给定一个0~1000的整数,求各位数的和,例如345的结果是3+4+5=12 注:分解数字既可以先除后模哦也可以先模后除(知识点:变量和运算符综合运用)
方法1(使用循环):
package com.firstday.base;
import java.until.Scanner;
public class Test02{
public static void main(String [] args){
int i=new Scanner (System.in).nextInt();
int sum=0;
while(i%100!=0){
sum+=i%10;
i=i/10;
}
System.out.println(sum);
}
}
方法2:
package com.firstday.base;
import java.until.Scanner;
public class Test02{
public static void main(String [] args){
int i=new Scanner (System.in).nextInt();
int a=i/100;
int b=i/10%10;
int c=i%10;
System.out.println(a+b+c);
}
}3.华氏温度和摄氏温度互相转换,从华氏度变成摄氏度你只要减去32,乘以5再除以9就行了,将摄氏温度转为华氏度,直接乘以9,除以5,再加上32即可
package com.firstday.base;
import java.until.Scanner;
public class Test03{
public static void main(String [] args){
Scanner input=new Scanner(System,in);
double h=input.nextDouble();
double s=(5/9)*(h-32);
System.out.println(h+"华氏度"+a+“摄氏度”);
}}
4.给定一个任意的大写字母A~Z,转换为小写字母
package com.firstday.base;
public class Test04{
public static void main(String [] args){
char x='A';
System.out,println("转换后"+(char)(x+32));
}}
5. 猴子第一天摘下若干个逃子,当鸡吃了一半,还不过瘾,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第十天早上想再吃时,见只剩一个桃子了。求第一天共摘了多少?
方法1:(使用while循环)
package com.firstday.base;
public class Test05{
public ststic void main(String [] args){
int sum=1;
int day=10;
while(day>1){
sum=(sum+1)*2;
day--;
}
System.out.println(sum);
}
}
方法2:(使用for循环)
package com.firstday.base;
public class Test05{
public static void main(String []args){
int sum=1;
for(int day=10;day>0;day--{
sum=(sum+1)*2;
}
System.out.println(sum);
}
}
6.给定一个成绩a,使用switch结构求出a的等级。A:90-100,B:80-89 ,C:70-79, D:60-69 ,E:0-59:
package com.firstday.base;
import java.until.Scanner;
public class Test06{
public static void main(String [] args){
Scanner input=new Scanner(System.in);
double x=input.nextDouble();
int sore=x/10;
switch(sore){
case 1:
case 2:
case 3:
case 4:
case 5:
System.out.println("E");
break;
case 6:
System,out.println("D");
break;
case 7:
System.out.println("C");
break;
case 8:
System.out,println("B");
break;
case 9:
case 10:
System.ou.println("A");
break;
default:
System.out.println("输入有误");
}
}
}
7.求1到100之间所有能被3整除的整数的和
package com.firstday.base;
public class Test10 {public static void main(String[] args) {
int sum=first(100);
System.out.println(sum);
int total=second(100);
System.out.println(total);
}
public static int first(int a) {
int temp=0;
for(;a>0;a--) {
if(a%3==0)
{}
return temp;}
public static int second(int b) {
int temp=0;
while (b>0) {
if(b%3==0) {
temp+=b;
b--;}
return temp;
8.如果是5,输出x=5,如果是10,输出x=10,除了以上几个值,都输出x=none
package com.firstday.base;
public class Test08{
public static void main(String [] args){
Scanner input=new Scanner(System.in);
int x=input.nextInt();
switch(x){
case 5:
System.out.println("x="+x);
break;
case 10:
System.out.println("x="+10);
break;
default:
System.out.println("x="+"none");
break;
}
}
}