1、编写一个Java程序,显示一张桌子的信息,包括一张桌子的信息,包括桌子的形状(长方形、方形、圆形、椭圆形)、腿数、高度、桌面面积、定义变量来保存桌子的信息,并显示各个信息的值。
public class Hello {
public static void main(String[] args) {
String xingzhuang="zhengfangxing";
int jiaoshu=4;
double gaodu=0.8;
double mianji=10;
System.out.println("xinngzhuang:"+xingzhuang);
System.out.println("jiaodu:"+jiaoshu);
System.out.println("gaodu:"+gaodu);
System.out.println("mianji:"+mianji);
}
}
2、编写一个java程序,实现两个数的交换,并输出交换后的结果。
public class Hello {
public static void main(String[] args) {
int a=3;
int b=5;
int temp;
temp=a;
a=b;
b=temp;
System.out.println(a);
System.out.println(b);
}
}
public class Hello{
public static void main(String [] args) {
String name="张三";
int age=23;
double grade=87.67;
String result="通过";
if(grade<60) {
result="不通过";
}
System.out.println("xingming"+name);
System.out.println("nianling"+age);
System.out.println("kaoshichengji"+result);
}
}
输入学生成绩
import java.util.Scanner;
public class Hello{
public static void main(String [] args) {
String name="zhangsan";
int age=23;
double grade;
String result="tongguo";
System.out.println("请输入学生成绩:");
Scanner sc=new Scanner(System.in);
grade=sc.nextDouble();
if(grade<60) {
result="butongguo";
}
System.out.println("xingming: "+name);
System.out.println("nianling: "+age);
System.out.println("chengji: "+grade);
System.out.println("kaosijieguo: "+result);
}
}
显示一个学生成绩
import java.util.*;
public class Hello{
public static void main(String [] args) {
String name="zhangsan";
int age=23;
double grade=0;
String result="";
System.out.println("qingshuruxueshengchengji:");
Scanner sc=new Scanner(System.in);
grade=sc.nextDouble();
if (grade >= 90) {
result="youxiu";
}
else if(grade<90 && grade >= 75) {
result="lianghao";
}
else if(grade<75 && grade>=60) {
result="jige";
}
else {
result="bujige";
}
System.out.println("xingming:"+name);
System.out.println("nianling"+age);
System.out.println("chengji:"+grade);
System.out.println("chengjijibie:"+result);
}
}
如果输入的数据不是一个双精度数,而是一个字符串,怎么办?
增加异常处理,用来处理输入数据格式不正确这种异常的情况。
import java.util.*;
import java.util.InputMismatchException;
public class Hello{
public static void main(String [] args) {
String name="zhangsan";
int age=23;
double grade=0;
String result="tongguo";
try {
Scanner sc=new Scanner(System.in);
grade=sc.nextDouble();
}
catch(InputMismatchException ime) {
System.out.println("shurushujugeshibuzhengque");
}
if (grade >= 90) {
result="youxiu";
}
else if(grade<90 && grade >= 75) {
result="lianghao";
}
else if(grade<75 && grade>=60) {
result="jige";
}
else {
result="bujige";
}
System.out.println("xingming:"+name);
System.out.println("nianling"+age);
System.out.println("chengji:"+grade);
System.out.println("chengjijibie:"+result);
}
}
先输入学生人数,根据人数来输入学生成绩和平均成绩。
import java.util.Scanner;
public class Hello{
public static void main(String [] args) {
//size表示一个班级的学生人数
int size=0;
double grade=0;
double averageGrade=0;
Scanner sc=new Scanner(System.in);
System.out.println("xueshengrenshu:");
size=sc.nextInt();
System.out.println("shuruxueshengrenshu:");
for(int i=0;i<size;i++) {
grade=sc.nextDouble();
averageGrade=averageGrade+grade;
}
averageGrade=averageGrade/size;
System.out.println("pingjunchengji:"+averageGrade);
}
}
从键盘上输入两个数字,按照由大到小顺序输出
import java.util.Scanner;
public class Hello{
public static void main(String[] args) {
double a;
double b;
Scanner sc=new Scanner(System.in);
a=sc.nextDouble();
b=sc.nextDouble();
if(a>b){
System.out.println(a);
System.out.println(b);
}
else {
System.out.println(b);
System.out.println(a);
}
}
}
从键盘输入三个数字,输出最大数。
import java.util.Scanner;
public class Hello{
public static void main(String [] args) {
double a;
double b;
double c;
double max;
Scanner sc=new Scanner(System.in);
a=sc.nextDouble();
b=sc.nextDouble();
c=sc.nextDouble();
if(a>b) {
if(a>c) {
max=a;
}
else {
max=c;
}
}
else {
if(b>c) {
max=b;
}
else {
max=c;
}
}
System.out.println(max);
}
}
输入-1退出
import java.util.Scanner;
public class Hello {
public static void main(String [] args) {
int size=0;
double grade=0;
double averageGrade=0;
Scanner sc=new Scanner(System.in);
System.out.println("shuruxueshengchengji: ");
grade=sc.nextDouble();
while(grade != -1) {
averageGrade=averageGrade+grade;
size++;
grade=sc.nextDouble();
}
averageGrade=averageGrade/size;
System.out.println("pingjunchengji:"+averageGrade);
}
}