java常用类与函数库

第6章 Java常用类与函数库

1. 提示用户输入三角型两边边长与其夹角度数,利用公式s=1/2absin(c)计算三角型面积,输出结果。
注意:正弦值的计算对象是弧度制的角,需将角度转换为弧度:π/180。
import java.util.Scanner;
import java.text.DecimalFormat;

class Demo{
public static void main(String[] args){
Scanner reader = new Scanner(System.in);
System.out.println("请输入三角型两边边长");
double a = reader.nextDouble();
double b = reader.nextDouble();
System.out.println("请输入两边夹角度数");
double angle = reader.nextDouble();
double s = 0.5*a*b*Math.sin(angle*Math.PI/180);
DecimalFormat df = new DecimalFormat("0.00");
String area = df.format(s);
System.out.println("该三角型面积是"+area);
//System.out.println(Math.sin(90));

}
}

2. 编写Java应用程序,使用Vector向量来保存用户输入的若干字符串。循环读入用户输入的字符串,以end作为结束。将所有字符串显示出来。在所有字符串的中间位置插入“NICE”,再次显示所有字符串。
import java.util.Vector;
import java.util.Scanner;

class Demo{
public static void main(String[] args){
Scanner reader = new Scanner(System.in);
Vector vct = new Vector(1,1);
System.out.println("请输入字符串,以输入end作为结束");
String str;
do{
str = reader.next();
vct.add(str);
}while(!str.equals("end"));
System.out.println("您刚才输入的所有字符串是:");
System.out.println(vct.toString());
System.out.println("插入NICE到中间位置:");
int n = vct.capacity();
vct.insertElementAt("NICE",n/2);
System.out.println(vct.toString());
}

}

3. 显示InputDialog输入对话框实现对用户输入的英文单词进行简单处理(转换为大写、转换为小写、反转显示)。程序运行效果如下图:




import javax.swing.JOptionPane;

class Demo{
public static void main(String[] args){
String str = JOptionPane.showInputDialog("请输入一个英文单词");
str = str.trim();
String[] items = {"转换为大写", "转换为小写", "反转显示" };
Object selectedValue = JOptionPane.showInputDialog(null,
"请选择",
"输入",
https://www.360docs.net/doc/c78986376.html,RMATION_MESSAGE,
null,
items,
items[0]);
String choice = (String)selectedValue;
if(choice.equals("转换为大写"))
JOptionPane.showMessageDialog(null,str.toUpperCase(),
"操作结果",https://www.360docs.net/doc/c78986376.html,RMATION_MESSAGE);
else if(choice.equals("转换为小写"))
JOptionPane.showMessageDialog(null,str.toLowerCase(),
"操作结果",https://www.360docs.net/doc/c78986376.html,RMATION_MESSAGE);
else
JOptionPane.showMessageDialog(null,
(new StringBuffer(str).reverse()),
"操作结果",J

https://www.360docs.net/doc/c78986376.html,RMATION_MESSAGE);
}
}


相关文档
最新文档