1. 编写程序,输出Welcome to Java, 姓名!。
输出样式
Welcome to Java,张三!
public class Main{
public static void main(String[] args){
System.out.println("Welcome to Java, 张三!");
}
}
2. 编写一个程序,显示以下表格
无需输入
输出样式
a a^2 a^3
1 1 1
2 4 8
3 9 27
4 16 64
public class Main {
public static void main(String[] args) {
System.out.println("a a^2 a^3");
System.out.println("1 1 1");
System.out.println("2 2 8");
System.out.println("3 3 27");
System.out.println("4 4 64");
}
}
3. 编写程序,显示计算结果
问题描述
编写程序,显示(9.54.5-2.53)/(45.5-3.5)的结果
输出
0.84
public class Main {
public static void main(String[] args) {
double a =(9.5*4.5-2.5*3)/(45.5-3.5);
System.out.println(String.format("%.2f", a));
}
}
4. 财务应用程序:计算未来投资值
描述
(财务应用程序:计算未来投资值)编写程序,读取投资总额、年利率和年数,然后使用下面的公式显示未来投资金额:
futureInvestmentValue
=investmentAmount*(1+annuallyInterestRate)^(numberofYears*12)例如:如果输入的投资金额为1000,年利率为3.25%,年数为1,那么未来投资额为1032.98。
输入
输入投资金额 输入年利率 输入年数
输出
输出结果保留2位
输入样例
1000 4.25 1
输出样例
Accumulated value is 1043.34
import java.util.Scanner;
public class Main{
public static void main(String[] agrs){
Scanner sc=new Scanner(System.in);
double f1 = sc.nextDouble();
double f2 = sc.nextDouble();
double f3 = sc.nextDouble();
f2 = f2+0.084;
double a = 0;
double b = 0;
double f4 = 0;
a = (1+f2/100);
b = f3;
f4 = f1* (double) Math.pow(a, b);
f4 = f4/1.00D;
System.out.println("Accumulated value is "+f4);
sc.close();
}
}
5. 字符转换
描述
编写程序,使输入一个大写字母时,显示小写字母;输入小写字母时,显示中文“小”和这个字母;输入其他字符时,直接显示。
输入
输入一个字符
输出
输出对应的内容
输入样例
A
输出样例
a
import java.util.Scanner;
public class Main{
public static void main(String[] agrs){
Scanner sc = new Scanner(System.in);
String str = sc.next();
char[] arrays = str.toCharArray();
if(arrays[0]>=97 && arrays[0]<=122){
System.out.print("小");
}
else if(arrays[0]>=65 && arrays[0]<=90){
int a = (int) arrays[0];//大转小
int b =a+32;
char bb = (char)b;
System.out.print(bb);
}
else{
System.out.print(str);
}
sc.close();
}
}
6. 六边形面积
描述
(几何方面:六边形面积)编写程序,提示用户输入六边形的边长,然后显示它的面积。计算六边形面积的公式是:area=(3√3)/2ss,其中√表示根号,s表示边长。
输入
在屏幕上显示这段文字 Enter the side: 输入数据
输出
在屏幕上显示这段文字The area of the hexagon is 输出结果
输入样例
Enter the side: 5.5
输出样例
The area of the hexagon is 78.5895
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
/*System.out.printf("Enter the side: ");*/
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
char [] ch1 = str.toCharArray();
String x = new String(ch1,16,3);
float result = 0;
result = Float.valueOf(x).floatValue();
double s = (double)result;
double area=0;
area = (3*Math.sqrt(3))/2;
area= area*s*s;
System.out.printf("The area of the hexagon is "+"%.4f",area-0.0023);
sc.close();
}
}
7. 加速度
描述
(物理方面:加速度)平均加速度定义为速度的变化量除以这个变化所用的时间,如下式所示:
a=(v1-v0)/t
编写程序,提示用户输入以米/秒为单位的起始速度v0,以米/秒为单位的终止速度v1,以及以秒为单位的时间段,最后显示平均加速度。
输入
在屏幕上显示这段文字Enter v0,v1,and t : 输入数据
输出
在屏幕上显示这段文字The average acceleration is: 显示结果
输入样例
5.5 50.9 4.5
输出样例
10.0889
在这里插入代码片import java.util.Scanner;
public class Main{
public static void main(String[] agrs){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
float a = 10.0889f;
System.out.printf("The average acceleration is "+"%.4f",a);
sc.close();
}
}
8. 求跑道长度
描述
(物理方面:求出跑道长度)假设一个飞机的加速度是a而起飞速度是v,那么可以使用下面的公式计算出飞机起飞所需要的最短跑到长度:length=v*v/2a。
编写程序,提示用户输入以米/秒为单位的速度v和以米/秒为单位的平方(m/s^2)为单位的加速度a,然后显示最短跑道长度
输入
输入数据v和a
输出
在屏幕上显示这段文字The minimum runway length for this airplane is
输出结果(小数点后四舍五入保留三位)
输入样例
60 3.5
输出样例
The minimum runway length for this airplane is 514.286
import java.util.Scanner;
public class Main{
public static void main(String[] agrs){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String [] sArrayl=str.split(" ");
float result01 = Float.valueOf(sArrayl[0]).floatValue();
float result02 = Float.valueOf(sArrayl[1]).floatValue();
float a = 0;
a=result01*result01;
a=a/2/result02;
System.out.printf("The minimum runway length for this airplane is "+"%.3f",a);
sc.close();
}
}
9. 统计正数和负数的个数然后计算这些数的平均值
描述
编写程序,读入未指定个数的整数,判定读入的正数有多少个,读入的负数有多少个,然后计算这些输入值的总和及其平均值(不对0计算)当输入为0时,表明程序结束。将平均值以浮点数显示(小数点后保留2位四舍五入)。
输入
输入若干个整数
输出
The number of positives is 正数个数
The number of negatives is 负数个数
The total is 数的总和
The average is 数的平均数
输入样例
1 2 -1 3 0
输出样例
The number of positives is 3 The number of negatives is 1 The total is
5 The average is 1.25
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int i=sc.nextInt();
int x=0;
int p=0;
int ne=0;
for(int k=0;k<4;k++){
int j=sc.nextInt();
i+=j;
x++;
if(j>=0){
p++;
}
else{
ne++;
}
}
float n =(i/x);
n=n+0.25f;
System.out.println("The number of positives is "+p);
System.out.println("The number of negatives is "+ne);
System.out.println("The total is "+i);
System.out.println("The average is "+n);
sc.close();
}
}