关于JAVA的两个题,希望大神解决一下,马上快交作业了?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于JAVA的两个题,希望大神解决一下,马上快交作业了?相关的知识,希望对你有一定的参考价值。

规则问题之类的截图上有,希望帮忙一下,解决好了有追加。急急急

您好,非常荣幸能在此回答您的问题。以下是我对此问题的部分见解,若有错误,欢迎指出。
转载:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

/**
* JTextPane的例子,模拟聊天客户端,演示了为每段文字设置字体、字号、样式、颜色、背景色和图片功能
* @author 五斗米 <如转载请保留作者和出处>
* @blog
*/

public class JTextPane3 extends JFrame
private JScrollPane scrollPane = null; // 滚动
private JTextPane text = null; // 不用说了,如果不认识的话就没必要往后看了
private Box box = null; // 放输入组件的容器
private JButton b_insert = null, b_remove = null, b_icon = null; // 插入按钮;清除按钮;插入图片按钮
private JTextField addText = null; // 文字输入框
private JComboBox fontName = null, fontSize = null, fontStyle = null, fontColor = null,fontBackColor = null; // 字体名称;字号大小;文字样式;文字颜色;文字背景颜色

private StyledDocument doc = null; // 非常重要插入文字样式就靠它了
public JTextPane3()
super("JTextPane Test");
try // 使用Windows的界面风格
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
catch (Exception e)
e.printStackTrace();

text = new JTextPane();
text.setEditable(false); // 不可录入
doc = text.getStyledDocument(); // 获得JTextPane的Document
scrollPane = new JScrollPane(text);
addText = new JTextField(18);
String[] str_name = "宋体", "黑体", "Dialog", "Gulim" ;
String[] str_Size = "12", "14", "18", "22", "30", "40" ;
String[] str_Style = "常规", "斜体", "粗体", "粗斜体" ;
String[] str_Color = "黑色", "红色", "蓝色", "黄色", "绿色" ;
String[] str_BackColor = "无色", "灰色", "淡红", "淡蓝", "淡黄", "淡绿" ;
fontName = new JComboBox(str_name); // 字体名称
fontSize = new JComboBox(str_Size); // 字号
fontStyle = new JComboBox(str_Style); // 样式
fontColor = new JComboBox(str_Color); // 颜色
fontBackColor = new JComboBox(str_BackColor); // 背景颜色
b_insert = new JButton("插入"); // 插入
b_remove = new JButton("清空"); // 清除
b_icon = new JButton("图片"); // 插入图片
b_insert.addActionListener(new ActionListener() // 插入文字的事件
public void actionPerformed(ActionEvent e)
insert(getFontAttrib());
addText.setText("");

);
b_remove.addActionListener(new ActionListener() // 清除事件
public void actionPerformed(ActionEvent e)
System.out.println(text.getText());

);
b_icon.addActionListener(new ActionListener() // 插入图片事件
public void actionPerformed(ActionEvent arg0)
JFileChooser f = new JFileChooser(); // 查找文件
f.showOpenDialog(null);
insertIcon(f.getSelectedFile()); // 插入图片

);
box = Box.createVerticalBox(); // 竖结构
Box box_1 = Box.createHorizontalBox(); // 横结构
Box box_2 = Box.createHorizontalBox(); // 横结构
box.add(box_1);
box.add(Box.createVerticalStrut(8)); // 两行的间距
box.add(box_2);
box.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8)); // 8个的边距
// 开始将所需组件加入容器
box_1.add(new JLabel("字体:")); // 加入标签
box_1.add(fontName); // 加入组件
box_1.add(Box.createHorizontalStrut(8)); // 间距
box_1.add(new JLabel("样式:"));
box_1.add(fontStyle);
box_1.add(Box.createHorizontalStrut(8));
box_1.add(new JLabel("字号:"));
box_1.add(fontSize);
box_1.add(Box.createHorizontalStrut(8));
box_1.add(new JLabel("颜色:"));
box_1.add(fontColor);
box_1.add(Box.createHorizontalStrut(8));
box_1.add(new JLabel("背景:"));
box_1.add(fontBackColor);
box_1.add(Box.createHorizontalStrut(8));
box_1.add(b_icon);
box_2.add(addText);
box_2.add(Box.createHorizontalStrut(8));
box_2.add(b_insert);
box_2.add(Box.createHorizontalStrut(8));
box_2.add(b_remove);
this.getRootPane().setDefaultButton(b_insert); // 默认回车按钮
this.getContentPane().add(scrollPane);
this.getContentPane().add(box, BorderLayout.SOUTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600, 400);
this.setLocationRelativeTo(null);
this.setVisible(true);
addText.requestFocus();


/**
* 插入图片
*
* @param icon
*/
private void insertIcon(File file)
text.setCaretPosition(doc.getLength()); // 设置插入位置
text.insertIcon(new ImageIcon(file.getPath())); // 插入图片
insert(new FontAttrib());// 这样做可以换行


private void insert(FontAttrib attrib)

try // 插入文本
doc.insertString(doc.getLength(), attrib.getText() + "\n", attrib.getAttrSet());
catch (BadLocationException e) e.printStackTrace();

private FontAttrib getFontAttrib()

FontAttrib att = new FontAttrib();
att.setText(addText.getText());
att.setName((String) fontName.getSelectedItem());
att.setSize(Integer.parseInt((String) fontSize.getSelectedItem()));
String temp_style = (String) fontStyle.getSelectedItem();
if (temp_style.equals("常规"))

att.setStyle(FontAttrib.GENERAL);

else if (temp_style.equals("粗体"))

att.setStyle(FontAttrib.BOLD);

else if (temp_style.equals("斜体"))

att.setStyle(FontAttrib.ITALIC);

else if (temp_style.equals("粗斜体"))

att.setStyle(FontAttrib.BOLD_ITALIC);

String temp_color = (String) fontColor.getSelectedItem();
if (temp_color.equals("黑色"))

att.setColor(new Color(0, 0, 0));

else if (temp_color.equals("红色"))

att.setColor(new Color(255, 0, 0));

else if (temp_color.equals("蓝色"))

att.setColor(new Color(0, 0, 255));

else if (temp_color.equals("黄色"))

att.setColor(new Color(255, 255, 0));

else if (temp_color.equals("绿色"))

att.setColor(new Color(0, 255, 0));

String temp_backColor = (String) fontBackColor.getSelectedItem();
if (!temp_backColor.equals("无色"))

if (temp_backColor.equals("灰色"))

att.setBackColor(new Color(200, 200, 200));

else if (temp_backColor.equals("淡红"))

att.setBackColor(new Color(255, 200, 200));

else if (temp_backColor.equals("淡蓝"))

att.setBackColor(new Color(200, 200, 255));

else if (temp_backColor.equals("淡黄"))

att.setBackColor(new Color(255, 255, 200));

else if (temp_backColor.equals("淡绿"))

att.setBackColor(new Color(200, 255, 200));


return att;


public static void main(String args[])
new JTextPane3();


private class FontAttrib
public static final int GENERAL = 0; // 常规
public static final int BOLD = 1; // 粗体
public static final int ITALIC = 2; // 斜体
public static final int BOLD_ITALIC = 3; // 粗斜体
private SimpleAttributeSet attrSet = null; // 属性集
private String text = null, name = null; // 要输入的文本和字体名称
private int style = 0, size = 0; // 样式和字号
private Color color = null, backColor = null; // 文字颜色和背景颜色
public FontAttrib()
public SimpleAttributeSet getAttrSet()
attrSet = new SimpleAttributeSet();
if (name != null)
StyleConstants.setFontFamily(attrSet, name);
if (style == FontAttrib.GENERAL)

StyleConstants.setBold(attrSet, false);
StyleConstants.setItalic(attrSet, false);

else if (style == FontAttrib.BOLD)

StyleConstants.setBold(attrSet, true);
StyleConstants.setItalic(attrSet, false);

else if (style ==FontAttrib.ITALIC)

StyleConstants.setBold(attrSet, false);
StyleConstants.setItalic(attrSet, true);

else if (style == FontAttrib.BOLD_ITALIC)

StyleConstants.setBold(attrSet, true);
StyleConstants.setItalic(attrSet, true);

StyleConstants.setFontSize(attrSet, size);
if (color!= null)
StyleConstants.setForeground(attrSet, color);
if (backColor != null)
StyleConstants.setBackground(attrSet, backColor);
return attrSet;


/**
* 设置属性集
*
* @param attrSet
*/
public void setAttrSet(SimpleAttributeSet attrSet)
this.attrSet = attrSet;


/* 后面的注释就不写了,一看就明白 */

public String getText()
return text;

public void setText(String text)
this.text = text;

public Color getColor()
return color;

public void setColor(Color color)
this.color = color;

public Color getBackColor()
return backColor;

public void setBackColor(Color backColor)
this.backColor = backColor;


public String getName()
return name;


public void setName(String name)
this.name = name;


public int getSize()
return size;


public void setSize(int size)
this.size = size;


public int getStyle()
return style;


public void setStyle(int style)
this.style = style;



非常感谢您的耐心观看,如有帮助请采纳,祝生活愉快!谢谢!
参考技术A 拿去不谢
import java.util.*;

public class TestClass
public static void main(String[] args)
// 题目1
question1();
// 题目2
question2();


/**
* 题目1
*/
private static void question1()
int[] a = 1, 2, 3, 4, 5, b = 4, 5, 6, 7;

// 统计每个数字出现的数量
Map<Integer, Integer> map = new LinkedHashMap<>();
for (int i : a)
map.put(i, map.get(i) == null ? 1 : map.get(i) + 1);

for (int i : b)
map.put(i, map.get(i) == null ? 1 : map.get(i) + 1);


// cList: 重复的数字
// dList: 不重复的数字
List<Integer> cList = new ArrayList<>();
List<Integer> dList = new ArrayList<>();

// 从map取出重复和不重复的内容
map.forEach((key, value) ->
if (value > 1)
cList.add(key);
else
dList.add(key);

);

// 将集合转成数组
int[] c = new int[cList.size()];
int[] d = new int[dList.size()];
for (int i = 0; i < cList.size(); i++)
c[i] = cList.get(i);

for (int i = 0; i < dList.size(); i++)
d[i] = dList.get(i);


// 输出c数组和d数组
System.out.println(Arrays.toString(c));
System.out.println(Arrays.toString(d));


/**
* 题目2
*/
private static void question2()
Scanner sc = new Scanner(System.in);
while (true)
System.out.print("请输入第1条边:");
float side1 = sc.nextFloat();
System.out.print("请输入第2条边:");
float side2 = sc.nextFloat();
System.out.print("请输入第3条边:");
float side3 = sc.nextFloat();

try
// 判断是否是三角形
boolean condition1 = side1 + side2 > side3;
boolean condition2 = side1 + side3 > side2;
boolean condition3 = side2 + side3 > side1;
if (!condition1 || !condition2 || !condition3)
throw new Exception("这不能构成三角形。");


// 判断直角三角形
boolean right1 = Math.pow(side1, 2) + Math.pow(side2, 2) == Math.pow(side3, 2);
boolean right2 = Math.pow(side1, 2) + Math.pow(side3, 2) == Math.pow(side2, 2);
boolean right3 = Math.pow(side2, 2) + Math.pow(side3, 2) == Math.pow(side1, 2);
if (right1 || right2 || right3)
throw new Exception("这是一个直角三角形。");


// 判断钝角三角形
boolean obtuse1 = Math.pow(side1, 2) + Math.pow(side2, 2) < Math.pow(side3, 2);
boolean obtuse2 = Math.pow(side1, 2) + Math.pow(side3, 2) < Math.pow(side2, 2);
boolean obtuse3 = Math.pow(side2, 2) + Math.pow(side3, 2) < Math.pow(side1, 2);
if (obtuse1 || obtuse2 || obtuse3)
throw new Exception("这是一个钝角三角形。");


// 判断锐角三角形
throw new Exception("这是一个锐角三角形。");
catch (Exception e)
// 输出结果
System.out.println(e.getMessage());


// 继续吗
System.out.print("继续吗?(y/n)");
sc.nextLine();
String isContinue = sc.nextLine();
if ("n".equals(isContinue))
break;


sc.close();

追问

第一题能不用List的吗,还没有学到……就用点比较基础的

追答

因为数组定义之后是不能改变大小的,我们在计算前并不知道数量到底有多少个,所以不好给数量,写起来会麻烦很多,用List的话就会灵活很多,题目想要的是c数组和d数组,所以我把List又转换成了你们想要的数组,符合题目的要求,其实题目也没限制不能用List,就先用着哈,其实List也不难,调用.add方法就可以往List添加东西,相信你一看就懂

追问

好嘛好嘛,谢谢咯

本回答被提问者采纳

Java面试题|对比一下Java和JavaScriprt

每天一道面试模拟真题及解析





课前导读

●回复"每日一练"获取以前的题目,持续更新!

我希望大家积极参与!有什么不懂可以加小千微信进行讨论

★把面试准备工作,拆分、融入到平时每天


对比一下Java和JavaScriprt

参考答案:


JavaScript 与 Java 是两个公司开发的不同的两个产品。
  • Java 是 Sun 公司推出的面向对象的编程语言,现在多用于于互联网服务端开发,前身是 Oak
  • JavaScript 是 Netscape 公司推出的,为了扩展 Netscape 浏览器功能而开发的一种可以嵌入Web 页面中运行的基于对象和事件驱动的解释性语言,前身是 LiveScript
区别:
  • 面向对象和基于对象:Java是一种面向对象的语言;JavaScript 是一种基于对象(Object-Based)和事件驱动(Event-Driven)的编程语言,提供了丰富的内部对象供开发者使用
  • 编译和解释:Java 的源代码在执行之前,必须经过编译;JavaScript 是一种解释型编程语言,其源代码不需经过编译,由浏览器直接解释执行
  • 静态与动态语言:Java 是静态语言(编译时变量的数据类型即可确定的语言);JavaScript 是动态语言(运行时确定数据类型的语言)
  • 强类型变量和类型弱变量:Java 采用强类型变量检查,所有变量在编译之前必须声明类型;JavaScript 中变量声明,采用弱类型,即变量在使用前不需作声明类型,解释器在运行时检查其数据类型  
点击 阅读原文 抢预约免费试听课程名额

以上是关于关于JAVA的两个题,希望大神解决一下,马上快交作业了?的主要内容,如果未能解决你的问题,请参考以下文章

急求几道java编程题,希望大神们帮帮忙!

Node.js记录

Java基础题

java编程题 希望大家能够帮助我一下,谢谢?

一个逻辑题(今天星期几)引发的思考

我是C语言新手。求大神解释一下这一道题,书里没解析的额