Java面向对象程序设计答案第6章----Java常用类(三角形面积公式,vector,InputDialog)
程序员文章站
2022-07-10 18:21:39
第六章答案1、用公式计算三角形面积package com.xiujie.part_6;import java.util.Scanner;import java.lang.Math;import java.text.DecimalFormat;public class GetS {public static void main(String[] args) {Scanner scanner=new Scanner(System.in);System.out.println("请输...
第六章答案
1、用公式计算三角形面积
package com.xiujie.part_6;
import java.util.Scanner;
import java.lang.Math;
import java.text.DecimalFormat;
public class GetS {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入三角形两边边边长");
int a=scanner.nextInt();
int b=scanner.nextInt();
System.out.println("请输入三角形的夹角度数");
double c=scanner.nextDouble();
double r=Math.toRadians(c);
double s;
s=a*b*Math.sin(r)/2;
DecimalFormat decimalFormat=new DecimalFormat("0.00");
String string=decimalFormat.format(s);
System.out.println("三角形面积为"+string);
scanner.close();
}
}
2、Vector保存用户输入的若干字符串
package com.xiujie.part_6;
import java.util.Scanner;
import java.util.Vector;
public class PrintString {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入内容");
Vector box=new Vector();
String string=null;
int i=0;
while(true) {
string=scanner.nextLine();
if (string.contains("end")) {
break;
}
i++;
box.addElement(string);
}
System.out.println(box.toString());
System.out.println("插入NICE后为");
box.insertElementAt("NICE",i/2);
System.out.println(box.toString());
scanner.close();
}
}
3、显示对话框实现英文单词的简单处理
package com.xiujie.part_6;
//该part之前没有学过,是雪儿现学现卖写的,有什么不足之处欢迎大家指出,可以私我哟!
import javax.swing.JOptionPane;
public class InputDialog {
public static void main(String[] args) {
//功能a
String input=JOptionPane.showInputDialog("请输入一个英文单词");
//进入功能b
Object[] possibleObjects= {"转换为大写","转换为小写","反转显示"};
Object selectObject=JOptionPane.showInputDialog(null, "请选择", "输入",
JOptionPane.INFORMATION_MESSAGE, null, possibleObjects, possibleObjects[0]);
String string=(String) selectObject;
String str=null;
if (string.equals(possibleObjects[0])) {
str=input.toUpperCase();
JOptionPane.showMessageDialog(null,str,"操作结果",JOptionPane.INFORMATION_MESSAGE);
}else if (string.equals(possibleObjects[1])) {
str=input.toLowerCase();
JOptionPane.showMessageDialog(null,str,"操作结果",JOptionPane.INFORMATION_MESSAGE);
}else if (string.equals(possibleObjects[2])) {
String str2=new StringBuffer(input).reverse().toString();
JOptionPane.showMessageDialog(null, str2,"操作结果",JOptionPane.INFORMATION_MESSAGE);
}
}
}
本文地址:https://blog.csdn.net/qq_36171670/article/details/112227815
上一篇: RabbitMQ 基础知识总结