「java编码怎么填」java用什么编码方式
今天给各位分享java编码怎么填的知识,其中也会对java用什么编码方式进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、Java实验,代码怎么写?
- 2、Java中如何设置编码格式
- 3、java编码规范有哪些?
- 4、java 代码怎么写?
- 5、登陆界面的java代码怎么写?
- 6、JAVA程序,从键盘输入汉字,并将汉子转换为相应的unicode编码,怎么写啊
Java实验,代码怎么写?
Shape.java接口代码
public interface Shape {
public static final double PI = 3.14d;
public double area();
}
Circle.java圆类代码
public class Circle implements Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double area() {
return PI * this.radius * this.radius;
}
public double perimeter() {
return 2 * PI * this.radius;
}
}
Cylinder.java圆柱体类代码
public class Cylinder extends Circle {
private double height;
public Cylinder(double radius, double height) {
super(radius);
this.height = height;
}
public double area() {
return 2 * super.area() + super.perimeter() * this.height;
}
public double volume() {
return super.area() * this.height;
}
}
X5_3_6.java主类代码
public class X5_3_6 {
public static void main(String[] args) {
Circle cir1 = new Circle(5);
System.out.println("圆的面积为:" + cir1.area());
System.out.println("圆的周长为:" + cir1.perimeter());
Cylinder cy1 = new Cylinder(10, 15);
System.out.println("圆柱体的表面积为:" + cy1.area());
System.out.println("圆柱体的体积为:" + cy1.volume());
}
}
上面是我写的代码,下图是执行结果,麻烦看一下,是否可以。
Java中如何设置编码格式
如果是修改文件的编码方式,可以按以下步骤:
选中文件右键--Properties--Resource--Text file encoding--选中Other,然后选择自己想要的编码格式就可以了。
如果是修改MyEclipse的编码方式,可以按以下步骤:
菜单栏的Window--Perferences--General--Work Space--Text file encoding--选中Other,然后选择自己想要的编码格式就可以了。
java编码规范有哪些?
尽量使用完整的英文描述符,采用适用于相关领域的术语,采用大小写混合使名字可读。
JAVA代码规范:
(1)类名首字母应该大写。字段、方法以及对象(句柄)的首字母应小写。对于所有标识符,其中包含的所有单词都应紧靠在一起,而且大写中间单词的首字母。例如:
ThisIsAClassName
thisIsMethodOrFieldName
若在定义中出现了常数初始化字符,则大写static final基本类型标识符中的所有字母。这样便可标志出它们属于编译期的常数。Java包(Package)属于一种特殊情况:它们全都是小写字母,即便中间的单词亦是如此。对于域名扩展名称,如com,org,net或者edu等,全部都应小写(这也是Java1.1和Java1.2的区别之一)。
(2)为了常规用途而创建一个类时,请采取"经典形式",并包含对下述元素的定义:equals()
hashCode()
toString()
clone()(implement Cloneable)
implement Serializable
(3)对于自己创建的每一个类,都考虑置入一个main(),其中包含了用于测试那个类的代码。为使用一个项目中的类,我们没必要删除测试代码。若进行了任何形式的改动,可方便地返回测试。这些代码也可作为如何使用类的一个示例使用。
(4)应将方法设计成简要的、功能性单元,用它描述和实现一个不连续的类接口部分。理想情况下,方法应简明扼要。若长度很大,可考虑通过某种方式将其分割成较短的几个方法。这样做也便于类内代码的重复使用(有些时候,方法必须非常大,但它们仍应只做同样的一件事情)。
(5)设计一个类时,请设身处地为客户程序员考虑一下(类的使用方法应该是非常明确的)。然后,再设身处地为管理代码的人考虑一下(预计有可能进行哪些形式的修改,想想用什么方法可把它们变得更简单)。
(6)使类尽可能短小精悍,而且只解决一个特定的问题。下面是对类设计的一些建议:
一个复杂的开关语句:考虑采用"多形"机制
数量众多的方法涉及到类型差别极大的操作:考虑用几个类来分别实现
许多成员变量在特征上有很大的差别:考虑使用几个类
(7)让一切东西都尽可能地"私有"-private。可使库的某一部分"公共化"(一个方法、类或者一个字段等等),就永远不能把它拿出。若强行拿出,就可能破坏其他人现有的代码,使他们不得不重新编写和设计。若只公布自己必须公布的,就可放心大胆地改变其他任何东西。在多线程环境中,隐私是特别重要的一个因素-只有private字段才能在非同步使用的情况下受到保护。
(8)谨惕"巨大对象综合症。对一些习惯于顺序编程思维、且初涉OOP领域的新手,往往喜欢先写一个顺序执行的程序,再把它嵌入一个或两个巨大的对象里。根据编程原理,对象表达的应该是应用程序的概念。
java 代码怎么写?
package com.date;
public class DateDome {
private int year=0;//年
private int month=0;//月
private int day=0;//日
public DateDome(int year,int month,int day){
this.year=year;
this.month=month;
this.day=day;
}
public DateDome(){
}
//年大于等于0
public boolean isYear(){
boolean suc=false;
if(year0)return suc;
else if(year=0)suc=true;
return suc;
}
//判断月数1-12月
public boolean isMonth(){
boolean suc=false;
if(month0||month12)return false;
else suc=true;
return suc;
}
//判断天数1-31号
public boolean isDay(){
boolean suc=false;
if(day=0||day31)return suc;
else suc=true;
return suc;
}
//是否为闰年
public boolean isRunNian(int year){
boolean suc=false;
if(year=0){
if(year%400==0||(year%4==0year%100!=0)){
suc=true;
}
}
return suc;
}
//判断合法年月日
public boolean isInvaildate(int year,int month, int day){
boolean suc=false;
if(isYear()isMonth()isDay()){
switch(month){
case 1:
suc=true;
break;
case 2:
if(isRunNian(year)day=29){
suc=true;
}else if(day=28){
suc=true;
}
break;
case 3:
suc=true;
break;
case 4:
if(day=30)suc=true;
break;
case 5:
suc=true;
break;
case 6:
if(day=30)suc=true;
break;
case 7:
suc=true;
break;
case 8:
suc=true;
break;
case 9:
if(day=30)suc=true;
break;
case 10:
suc=true;
break;
case 11:
if(day=30)suc=true;
break;
case 12:
suc=true;
break;
}
}
return suc;
}
//根据日期得到天数
public int getDaysByDate(int year,int month,int day){
int days=0;
if(isInvaildate(year,month,day)){
for(int i=0;iyear;i++){
if(isRunNian(i)){
days+=366;
}else{
days+=365;
}
}
for(int i=1;imonth;i++){
if(i==1||i==3||i==5||i==7||i==8||i==10||i==12){
days+=31;
}else if(i==4||i==6||i==9||i==11){
days+=30;
}else if(i==2){
if(isRunNian(year)){
days+=29;
}else{
days+=28;
}
}
}
days+=day-1;
return days;
}else{
System.out.println("处理有非法日期!!!");
return -1;
}
}
//根据天数得到日期数int[]由,年、月、日组成的数组
public int[] getDateByDays(int days){
int das=0;//预计的天数
int y=0,m=1,d=1;//0年1月1号
int[] date=new int[3];
boolean suc=true;
int temp=0;
if(days0){
System.out.println("请输入合法天数!!!");
return new int[]{0,0,0};
}
while(suc){
if(isRunNian(y)){
temp=366;
}else{
temp=365;
}
das+=temp;
if(dasdays){
y++;
}else{
das-=temp;
break;
}
}
while(suc){
if(m==1||m==3||m==5||m==7||m==8||m==10||m==12){
temp=31;
}else if(m==4||m==6||m==9||m==11){
temp=30;
}else if(m==2){
if(isRunNian(y)){
temp=29;
}else{
temp=28;
}
}
das+=temp;
if(dasdays){
m++;
}else{
das-=temp;
break;
}
}
d=days-das+1;
date[0]=y;
date[1]=m;
date[2]=d;
return date;
}
//得到多少天前或后合法日期
public int[] addORsubDay(int dd){
int days=getDaysByDate(year,month,day);
if(days=0){
days+=dd;
if(days0){
return getDateByDays(days);
}else{
System.out.println("处理日期不能小于0年1月1号");
return new int[]{0,0,0};//代表无效日期
}
}else{
System.out.println("处理日期不能小于0年1月1号");
return new int[]{0,0,0};//年,月,日
}
}
//得到两个日期相距天数
public int TwoDate(int[] date1,int[] date2){
int d=-1;
if(isInvaildate(date1[0],date1[1],date1[2])isInvaildate(date2[0],date2[1],date2[2])){
int days1=getDaysByDate(date1[0],date1[1],date1[2]);
int days2=getDaysByDate(date2[0],date2[1],date2[2]);
d=days1-days2;
return d=0?d:-d;
}else
{
System.out.println("处理有非法日期!!!");
return d;
}
}
}
我写了你提出的功能,你还可以扩展其它功能。
登陆界面的java代码怎么写?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
class LoginFrm extends JFrame implements ActionListener
{
JLabel lbl1=new JLabel("用户名");
JLabel lbl2=new JLabel("密码");
JTextField txt=new JTextField(15);
JPasswordField pf=new JPasswordField();
JButton btn1=new JButton("确定");
JButton btn2=new JButton("取消");
public LoginFrm()
{
this.setTitle("登陆");
JPanel jp=(JPanel)this.getContentPane();
jp.setLayout(new GridLayout(3,2,10,10));
jp.add(lbl1);jp.add(txt);
jp.add(lbl2);jp.add(pf);
jp.add(btn1);jp.add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==btn1)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:MyDB","","");
Statement cmd=con.createStatement();
ResultSet rs=cmd.executeQuery("select * from loginAndpassword where login='"+txt.getText()+"' and password='"+pf.getText()+"'");
if(rs.next())
{
JOptionPane.showMessageDialog(null,"登陆成功!");
}
else
JOptionPane.showMessageDialog(null,"用户名或密码错误!");
} catch(Exception ex){}
if(ae.getSource()==btn2)
{
txt.setText("");
pf.setText("");
}
}
}
public static void main(String arg[])
{
JFrame.setDefaultLookAndFeelDecorated(true);
LoginFrm frm=new LoginFrm();
frm.setSize(400,200);
frm.setVisible(true);
}
}
JAVA程序,从键盘输入汉字,并将汉子转换为相应的unicode编码,怎么写啊
上代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入汉字:");
String input = sc.nextLine();
if (input.length() == 0) {
System.out.println("输入错误");
return;
}
System.out.println("汉字转unicode结果:");
char[] chars = input.toCharArray();
for (int i = 0; i chars.length; i++) {
String unicode = Integer.toHexString(chars[i]);
if (unicode.length() = 2) {
// 不足四位前面加0补齐
unicode = "00" + unicode;
}
unicode = "\\u" + unicode;
System.out.println(chars[i] + ": " + unicode);
}
}
}
java编码怎么填的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java用什么编码方式、java编码怎么填的信息别忘了在本站进行查找喔。
发布于:2022-12-04,除非注明,否则均为
原创文章,转载请注明出处。