首页 > 软件 > 2.编写一个 java 程序,输入形式为 First Middle Last

2.编写一个 java 程序,输入形式为 First Middle Last

软件 2022-06-25

关于JAVA输出单词首字母的程序

importjava.util.Scanner;
publicclassAssignment3{
publicstaticvoidmain(String[]args){
System.out.println("Whatareyourfirst,middle,andlastnames?");
Scannerscan=newScanner(System.in);
StringfullName=scan.nextLine();
if(fullName==null){
promptWrongName();
return;
}
String[]names=fullName.split("");
if(!verifyNames(names))
return;
printNames(names);
}
privatestaticvoidprintNames(String[]names){
printInitials(names);
printVariationOne(names);
printVariationTwo(names);
}
privatestaticStringcapitalizeFirstLetter(Stringname){
returnname.trim().substring(0,1).toUpperCase()+name.trim().substring(1).toLowerCase();
}
privatestaticStringgetInitialInUpperCase(Stringname){
returnname.trim().substring(0,1).toUpperCase();
}
privatestaticvoidprintVariationTwo(String[]names){
System.out.print("VariationTwo:");
System.out.print(capitalizeFirstLetter(names[names.length-1]));
System.out.print(",");
System.out.print(capitalizeFirstLetter(names[0]));
if(names.length==3){
System.out.print("");
System.out.print(capitalizeFirstLetter(names[1]));
}
System.out.println();
}
privatestaticvoidprintVariationOne(String[]names){
System.out.print("VariationOne:");
System.out.print(names[names.length-1].trim().toUpperCase());
System.out.print(",");
System.out.print(capitalizeFirstLetter(names[0]));
if(names.length==3){
System.out.print("");
System.out.print(getInitialInUpperCase(names[1]));
System.out.print(".");
}
System.out.println();
}
privatestaticvoidprintInitials(String[]names){
System.out.print("Yourinitialsare:");
for(inti=0;iSystem.out.print(getInitialInUpperCase(names[i]));
}
System.out.println();
}
privatestaticbooleanverifyNames(String[]names){
if(names.length!=2&&names.length!=3){
promptWrongName();
returnfalse;
}
for(inti=0;iif("".equals(names[i].trim())){
promptWrongName();
returnfalse;
}
}
returntrue;
}
privatestaticvoidpromptWrongName(){
System.out.println("Wrong.Pleaseenteryournamesproperly.");
}
}

编写java程序

//这是第一题的代码
public class Customer {
public String customerId; //客户身份证
public String custName; //客户姓名
public String custGender; //客户性别
public Customer(String customerId, String custName, String custGender){
this.customerId = customerId;
this.custName = custName;
this.custGender = custGender;
}
public String getID() {
return customerId;
}
public void setID(String customerId) {
this.customerId = customerId;
}
public String getName() {
return custName;
}
public void setName(String custName) {
this.custName = custName;
}
public String getGender() {
return custGender;
}
public void setGender(String custGender) {
this.custGender = custGender;
}
public void printMSG(){
System.out.println("客户身份证:" + this.getID());
System.out.println("客户姓名:" + this.getName());
System.out.println("客户性别:" + this.getGender());
}
public static void main(String[] args) {
Customer c = new Customer("11111","小童鞋_成er","男");
c.printMSG();
}
}

编写一个java程序,完成以下功能:

public static void main(String [] args){ String s = "Callme Ishmael."; //声明一个名为s的字符串,并使它的内容为”Callme Ishmael.” System.out.println(s); //打印整个字符串 System.out.println(s.length()); //使用length()方法打印字符串的长度 System.out.println(s.charAt(0)); //使用charAt()方法打印字符串的第一个字符 System.out.println(s.charAt(s.length()-1));

要求编写一个java程序,输入学生信息,并能保存与显示学生信息。

import java.util.ArrayList; import java.util.Scanner;import com.sun.org.apache.xpath.internal.Arg; public class Student { //两个私有属性 private int no ; private String name ; //默认构造函数 public Student(){} //带参构造函数 public Student(int no,String name){ this.no=no; this.name=name; } //添加学生信息 public void addStu

编写一个Java应用程序,要求如下:

我刚好谢了一个绝对原创
用到了图片,你可以将图片的代码删掉
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
importjava.io.*;
publicclassNotepadextendsJFrameimplementsActionListener{

JMenuBarjbar;//菜单条
JMenuwj,bj,bz; //菜单
JMenuItemopen,save,osave,exit,help,me;//菜单项
JTextAreajta;//文本区
publicNotepad(){
jbar=newJMenuBar();

wj=newJMenu("文件");
bj=newJMenu("编辑");
bz=newJMenu("帮助");
jbar.add(wj);
jbar.add(bj);
jbar.add(bz);
setJMenuBar(jbar);

open=newJMenuItem("打开",newImageIcon("img/dk.png"));
open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
wj.add(open);
open.addActionListener(this);
open.setActionCommand("open");
wj.addSeparator();
save=newJMenuItem("保存");
wj.add(save);
save.addActionListener(this);
save.setActionCommand("save");
osave=newJMenuItem("另存为",newImageIcon("img/bc.png"));
wj.add(osave);
osave.addActionListener(this);
osave.setActionCommand("osave");
wj.addSeparator();
exit=newJMenuItem("退出");
wj.add(exit);
exit.addActionListener(this);
exit.setActionCommand("exit");
help=newJMenuItem("查看帮助");
bz.add(help);
bz.addSeparator();
me=newJMenuItem("关于记事本");
bz.add(me);

jta=newJTextArea();
this.add(newJScrollPane(jta),BorderLayout.CENTER);

this.setVisible(true);
this.setSize(500,400);
this.setTitle("桃子记事本");
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
publicvoidactionPerformed(ActionEvente){
if(e.getActionCommand().equals("open")){
JFileChooserjfc=newJFileChooser();
jfc.setDialogTitle("请选择文件");
jfc.showOpenDialog(null);
jfc.setVisible(true);

Stringfilepath=jfc.getSelectedFile().getAbsolutePath();

FileReaderfr=null;
BufferedReaderbr=null;
try{
fr=newFileReader(filepath);
br=newBufferedReader(fr);

Strings="";
Stringb="";
while((s=br.readLine())!=null){
b+=s+"\r\n";
//System.out.println(s);
}
jta.setText(b);
}catch(Exceptione1){
e1.printStackTrace();
}finally{
try{
br.close();
}catch(IOExceptione1){
e1.printStackTrace();
}
}
}
elseif(e.getActionCommand().equals("osave")){
JFileChooserjfc=newJFileChooser();
jfc.setDialogTitle("请选择路径");
jfc.showSaveDialog(null);
jfc.setVisible(true);

Stringfilepath=jfc.getSelectedFile().getAbsolutePath();

FileWriterfw=null;
BufferedWriterbw=null;
try{
fw=newFileWriter(filepath);
bw=newBufferedWriter(fw);
bw.write(jta.getText());
}catch(Exceptione2){
e2.printStackTrace();
}finally{
try{
bw.close();
}catch(IOExceptione1){
e1.printStackTrace();
}
}
}
elseif(e.getActionCommand().equals("exit")){
System.exit(0);
}
}
}

标签:Java 编程 Java编程 信息技术

大明白知识网 Copyright © 2020-2022 www.wangpan131.com. Some Rights Reserved.