1、修改第一张中的Lincoln程序,使输出在名言的两端加上引号。
第一张程序代码如下:
public class Lincoln
{
public static void main(String[] args){
System.out.println("A quote by Abraham Lincoln:");
System.out.println("Whatever you are,be a good one.");
}
}
输出效果如下:
A quote by Abraham Lincoln:
Whatever you are,be a good one.
按照题目要求在名言两端添加“”,代码如下:
public class Lincoln
{
public static void main(String[] args){
System.out.println("A quote by Abraham Lincoln:");
System.out.println("\"Whatever you are,be a good one.\"");
}
}
输出效果:
A quote by Abraham Lincoln:
"Whatever you are,be a good one."
通过转义字符可以达成题目的要求。
2、编写一个程序,读取三个整数,然后输出他们的平均数。
public class Average
{
public static void main(String[] args){
int A = 10 , B = 20 , C = 30;
int D = ( A + B + C )/3;
System.out.println("A数:" + A +"\nB数:" +B + "\nC数:" +C );
System.out.println("平均数是:" + D);
}
}
运行结果:
A数:10
B数:20
C数:30
平均数是:20
3、编写一个程序,读取两个浮点数,然后输出它们的和、差和乘积。
public class Doublecount
{
public static void main(String[] args){
double A = 33.57 , B = 23.45 ;
System.out.println("A数:" + A +"\nB数:" +B );
System.out.println("AB之和:" + ( A + B ));
System.out.println("AB之差:" + ( A - B ));
System.out.println("AB乘积:" + ( A * B ));
}
}
运行效果:
A数:33.57
B数:23.45
AB之和:57.019999999999996
AB之差:10.120000000000001
AB乘积:787.2165
4、编写一个程序,输出提示信息并读入某人的姓名、年龄、毕业院校以及宠物的名字。然后显示出下述文字,并在其中插入适当的数据:
Hello,my name is name and I am age years
old.I’m enjoying my time at college,though
I miss my pet petname very much!
import java.util.Scanner;
public class man
{
public static void main(String[] args){
String name , college , petname;
int age ;
Scanner scan = new Scanner(System.in);
System.out.println("请输入您的名字:");
name = scan.nextLine();
System.out.println("请输入您的年龄:");
age = scan.nextInt();
System.out.println("请输入您的学院名称:");
college = scan.nextLine();
System.out.println("请输入您的宠物名字:");
petname = scan.nextLine();
System.out.println("Hello,my name is " + name + " and I am " + age + " years ");
System.out.println("old.I'm enjoying my time at " + college + ",though");
System.out.println("I miss my pet " + petname + " very much!");
}
}
输出效果:
请输入您的名字:
Liulang
请输入您的年龄:
33
请输入您的学院名称:
//这个地方出了一点问题,没有输入数据就跳到下一环节,没有找到问题的原因!!
请输入您的宠物名字:
TOM
Hello,my name is Liulang and I am 33 years
old.I'm enjoying my time at ,though
I miss my pet TOM very much!
5、编写一个TempConverter程序,读取用户输入的华氏温度,然后转换为摄氏温度。
import java.util.Scanner;
public class TempConverter
{
public static void main(String[] args){
double fahrenheit;
Scanner scan = new Scanner(System.in);
System.out.println("请输入华氏温度:");
fahrenheit = scan.nextDouble();
double centigrade = 5 * ( fahrenheit- 32) / 9 ;
System.out.println(fahrenheit + "华氏度等于" + centigrade + "摄氏度");
}
}
运行效果:
请输入华氏温度:
145
145.0华氏度等于62.77777777777778摄氏度
6、编写一个程序,将英里数转换为千米数(1英里等于1.60935千米)。以浮点类型读取用户输入的英里数。
import java.util.Scanner;
public class milesConverter
{
public static void main(String[] args){
double miles;
Scanner scan = new Scanner(System.in);
System.out.println("请输入英里:");
miles = scan.nextDouble();
double KM = miles * 1.60935 ;
System.out.println(miles + "英里等于" + KM + "千米");
}
}
运行效果:
请输入英里:
1
1.0英里等于1.60935千米
7、编写一个程序,提示并读入表示速度和旅行距离的整型值,然后以浮点型显示旅行所需要的时间。
import java.util.Scanner;
public class Travel
{
public static void main(String[] args){
int range , speed ;
Scanner scan = new Scanner(System.in);
System.out.println("请输入两地之间的距离:");
range = scan.nextInt();
System.out.println("请 输 入 平 均 速 度:");
speed = scan.nextInt();
double time = (double)range / speed ;
System.out.println("旅行大概需要花费" + time + "小时");
}
}
运行效果:
请输入两地之间的距离:
459
请 输 入 平 均 速 度:
90
旅行大概需要花费5.1小时
8、编写一个程序,以小时、分、秒读取时间长度,然后全部换算成秒并输出结果。例如:1小时28分42秒等于5322秒。
import java.util.Scanner;
public class ConvertTime
{
public static void main(String[] args){
int hr , min , sec ;
Scanner scan = new Scanner(System.in);
System.out.println("请输入时:");
hr = scan.nextInt();
System.out.println("请输入分:");
min = scan.nextInt();
System.out.println("请输入秒:");
sec = scan.nextInt();
sec = hr * 3600 + min * 60 + sec ;
System.out.println("共计" + sec + "秒");
}
}
运行效果:
请输入时:
1
请输入分:
28
请输入秒:
42
共计5322秒
9、编写一个程序,将上题的过程反过来,读入一个以秒为单位的时间长度,然后换算成小时、分、秒的组合表达式并输出结果。例如:9999秒等于2小时46分39秒。
import java.util.Scanner;
public class ConvertTime1
{
public static void main(String[] args){
int hr , min , sec , sec1 ;
Scanner scan = new Scanner(System.in);
System.out.println("请输入以秒为单位的时间数:");
sec = scan.nextInt();
sec1 = sec ;
hr = sec / 3600 ;
min = (sec - hr * 3600) / 60 ;
sec = (sec - hr * 3600) % 60 ;
System.out.println(sec1 + "秒等于" + hr + "小时" + min + "分" + sec + "秒");
}
}
运行效果:
请输入以秒为单位的时间数:
9999
9999秒等于2小时46分39秒