扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
mport java.util.*;
创新互联长期为上千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为尼元阳企业提供专业的成都网站制作、成都做网站、外贸营销网站建设,尼元阳网站改版等技术服务。拥有十载丰富建站经验和众多成功案例,为您定制开发。
public class HuiWen
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.println("please input a String:");
String st=in.nextLine();
String s=st.toLowerCase();
int i=0;
int j=s.length()-1;
boolean t=true;
char first=s.charAt(i);
char last=s.charAt(j);
for(;ij;)
{
while(!(first='a'first='z'))
{
i++;
first=s.charAt(i);
}
while(!(last='a'last='z'))
{
j--;
last=s.charAt(i);
}
if(i=j)
break;
if(first==last)
{
i++;
j--;
first=s.charAt(i);
last=s.charAt(j);
}
else
{
t=false;
break;
}
}
if(t)
System.out.println(st+"是回文串");
else
System.out.println(st+"不是回文串");
}
}
//判断是否为回文字串
//什么实用的程序,说具体点!你要计算器的程序吗?
//下面是计算器的程序,把分拿来吧!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator
{
public static void main(String[] args)
{
CalculatorFrame frame=new CalculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class CalculatorFrame extends JFrame
{
public CalculatorFrame()
{
setTitle("Calculator");
CalculatorPanel panel=new CalculatorPanel();
add(panel);
pack();
}
}
class CalculatorPanel extends Panel
{
public CalculatorPanel()
{
setLayout(new BorderLayout());
result=0;
lastCommand="=";
start=true;
flag=true;
display=new JButton("0");
display.setEnabled(false);
add(display,BorderLayout.NORTH);
ActionListener insert=new InsertAction();
ActionListener command=new CommandAction();
panel=new JPanel();
panel.setLayout(new GridLayout(4,5));
addButton("7",insert);
addButton("8",insert);
addButton("9",insert);
addButton("/",command);
addButton("CE",command);
addButton("4",insert);
addButton("5",insert);
addButton("6",insert);
addButton("*",command);
addButton("Backspace",command);
addButton("1",insert);
addButton("2",insert);
addButton("3",insert);
addButton("-",command);
addButton("sqrt",command);
addButton("0",insert);
addButton(".",insert);
addButton("=",command);
addButton("+",command);
addButton("1/x",command);
add(panel,BorderLayout.CENTER);
}
private void addButton(String label,ActionListener listener)
{
JButton button=new JButton(label);
button.addActionListener(listener);
panel.add(button);
}
private class InsertAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String input=event.getActionCommand();
if(startflag)
{
display.setText("");
start=false;
}
if(flag)
display.setText(display.getText()+input);
}
}
private class CommandAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String command=event.getActionCommand();
if(command.equals("CE"))
{
display.setText("0");
start=true;
flag=true;
command="=";
}
else
if(startflag)
{
if(command.equals("-"))
{
display.setText(command);
start=false;
}
else
if((command.equals("1/x")||command.equals("sqrt"))flag)
calculate(Double.parseDouble(display.getText()),command);
else
if(flag)
lastCommand=command;
}
else
{
if(command.equals("Backspace")flag)
{
String s=display.getText();
char[] s1=s.toCharArray();
if(s.length()=2)
{
String s2=new String(s1,0,s.length()-1);
display.setText(s2);
}
else
{
display.setText("0");
start=true;
}
}
else if(flag)
{
calculate(Double.parseDouble(display.getText()),command);
lastCommand=command;
start=true;
}
}
}
}
public void calculate(double x,String command)
{
if(lastCommand.equals("+")) result+=x;
else if(lastCommand.equals("-")) result-=x;
else if(lastCommand.equals("/"))
{
if(x!=0)
result/=x;
else
{
display.setText("除数不能为0");
start=false;
flag=false;
return;
}
}
else if(lastCommand.equals("*")) result*=x;
else if(command.equals("1/x"))
{
if(x!=0)
result=1/x;
else
{
display.setText("除数不能为0");
start=false;
flag=false;
return;
}
}
else if(command.equals("sqrt"))
{
if(x=0)
result=Math.sqrt(x);
else
{
display.setText("函数输入无效");
start=false;
flag=false;
return;
}
}
else if(lastCommand.equals("=")) result=x;
display.setText(""+result);
}
private JButton display;
private JPanel panel;
private double result;
private String lastCommand;
private boolean start;
private boolean flag;
}
a = sc.nextInt();
b = sc.nextInt();
a 0||b 0
a
完整代码:
public class ComDiv {
public static void main(String[] args) {
int a;
int b;
int r;
Scanner sc = new Scanner(System.in);
do {
System.out.println("请输入两个整数:");
a = sc.nextInt();
b = sc.nextInt();
} while (a 0||b 0);
sc.close();
while(b != 0){
r = a%b;
a = b;
b = r;
}
System.out.println("两个数的最大公约数是:" +a);
}
}
演示:
/**
* 文件名:Operation.java 2014-1-2 下午3:06:37
* @author Administrator
*/
package cc.icoc.javaxu.action;
import java.util.ArrayList;
import java.util.Scanner;
import cc.icoc.javaxu.datas.StudentInfo;
/**
* @author 许仕永
* 创建时间: 2014 2014-1-2 下午3:06:37
*/
public class Operation
{
int stuNo,stuAge;
String stuName,stuClass,stuSex;
ArrayListStudentInfo list;
/**程序入口
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
new Operation().operation();
}
private int inputInt()
{
Scanner scanner = new Scanner(System.in);
return scanner.nextInt();
}
private String inputStr()
{
Scanner scanner = new Scanner(System.in);
return scanner.nextLine();
}
private void operation()
{
// TODO Auto-generated method stub
if(list == null)
{
list = new ArrayListStudentInfo();
}
while(true)
{
System.out.println("\t\t\t学生信息管理系统\n");
System.out.println("1.信息查阅");
System.out.println("2.信息查询");
System.out.println("3.信息录入");
System.out.println("4.退出系统");
int what = inputInt();
if ( what == 1 )
{
getInfo();
} else if ( what == 2 )
{
allSerach();
} else if ( what == 3 )
{
inputInfo();
} else if( what == 4 )
{
System.out.println("已经退出系统");
System.exit(0);
}
}
}
/**
* 获取全部学生的信息列表
*/
private void getInfo()
{
if(!list.isEmpty())
{
for ( int i = 0; i list.size(); i++ )
{
System.out.println(list.get(i));
}
}
}
/**
* 执行查找
*/
private void allSerach()
{
System.out.println("1.输入学号进行查询");
System.out.println("2.输入姓名进行查询");
int what = inputInt();
if(what == 1)
{
System.out.println("请输入学号:");
search(inputInt());
}
else
{
System.out.println("请输入姓名:");
search(inputStr());
}
}
/**
* 按学生姓名查询学生信息
* @param name 学生姓名
*/
private StudentInfo search(String name)
{
for ( int i = 0; i list.size(); i++ )
{
if(list.get(i).getStuName().equals(name))
{
System.out.println(list.get(i));
return list.get(i);
}
}
System.out.println("未查询到该名字的学生");
return null;
}
/**
* 按学号查询学生信息
* @param id 学号
*/
private StudentInfo search(int id)
{
for ( int i = 0; i list.size(); i++ )
{
if(list.get(i).getStuNo()==id)
{
System.out.println(list.get(i));
return list.get(i);
}
}
System.out.println("未查询到该学号的学生");
return null;
}
/**
* 录入学生信息
*/
private void inputInfo()
{
try
{
System.out.print("请输入学生学号:\n");
stuNo = inputInt();
System.out.print("请输入学生姓名:\n");
stuName = inputStr();
System.out.print("请输入学生性别:\n");
stuSex = inputStr();
System.out.print("请输入学生年龄:\n");
stuAge = inputInt();
System.out.print("请输入学生班级:\n");
stuClass = inputStr();
list.add(new StudentInfo(stuNo, stuAge, stuName, stuSex, stuClass));
} catch (Exception e)
{
// TODO: handle exception
System.out.println("不允许为空,请重新输入");
}
}
}
package cc.icoc.javaxu.action;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Stu
{
int countStu = 0;
int sum = 0;
int avg = 0;
ListStuBean list = new ArrayListStu.StuBean();
public static void main(String[] args)
{
new Stu().go();
}
private void go()
{
A:while(true)
{
//构造实体Bean来存储学生成绩
StuBean bean = new StuBean();
System.out.println("请输入成绩:");
System.out.println("语文:");
bean.setYuwen(scanner());
System.out.println("数学:");
bean.setMath(scanner());
//获取英语成绩
System.out.println("英语:");
bean.setEnglish(scanner());
//存入容器
list.add(bean);
for ( int i = 0; i list.size(); i++ )
{
sum += list.get(i).english+list.get(i).math+list.get(i).yuwen;
}
avg = sum / (3*list.size());
System.out.println("当前有"+list.size()+"条学生记录,所有学生总分="+sum+"平均分为"+avg);
//输入y继续录入学生信息,输入x退出
System.out.println("\n已成功录入一个学生的数据,是否继续/退出(y/x)");
if("x".equals(scannerString()))
break A;
}
}
/**输入*/
private int scanner()
{
Scanner scanner = new Scanner(System.in);
return scanner.nextInt();
}
/**输入*/
private String scannerString()
{
Scanner scanner = new Scanner(System.in);
return scanner.nextLine();
}
class StuBean
{
int math;
int yuwen;
int english;
public int getMath()
{
return math;
}
public void setMath(int math)
{
this.math = math;
}
public int getYuwen()
{
return yuwen;
}
public void setYuwen(int yuwen)
{
this.yuwen = yuwen;
}
public int getEnglish()
{
return english;
}
public void setEnglish(int english)
{
this.english = english;
}
}
}
/**
* 文件名:StudentInfo.java 2014-1-2 下午3:07:29
* @author Administrator
*/
package cc.icoc.javaxu.datas;
/**
* @author 许仕永
* 创建时间: 2014 2014-1-2 下午3:07:29
*/
public class StudentInfo
{
int stuNo,stuAge;
String stuName,stuSex,stuClass;
public StudentInfo(int stuNo, int stuAge, String stuName, String stuSex, String stuClass)
{
super();
this.stuNo = stuNo;
this.stuAge = stuAge;
this.stuName = stuName;
this.stuSex = stuSex;
this.stuClass = stuClass;
}
public int getStuNo()
{
return stuNo;
}
public void setStuNo(int stuNo)
{
this.stuNo = stuNo;
}
public int getStuAge()
{
return stuAge;
}
public void setStuAge(int stuAge)
{
this.stuAge = stuAge;
}
public String getStuName()
{
return stuName;
}
public void setStuName(String stuName)
{
this.stuName = stuName;
}
public String getStuSex()
{
return stuSex;
}
public void setStuSex(String stuSex)
{
this.stuSex = stuSex;
}
public String getStuClass()
{
return stuClass;
}
public void setStuClass(String stuClass)
{
this.stuClass = stuClass;
}
@Override
public String toString()
{
// TODO Auto-generated method stub
String s = "学号:"+getStuNo()+"\t姓名:"+getStuName()+" \t性别:"+getStuSex()+"\t年龄:"+getStuAge()+"\t班级:"+getStuClass();
return s;
}
}
import java.util.Arrays;
public class Array2 {
public static void main(String[] args) {
//声明一个名为myArray的数组,该数组有2行,每行列数不等,并为其分配内存空间
int[][] myArray = new int[2][];
myArray[0] = new int[5]; //第一行有5个元素,并为其分配内存空间
myArray[1] = new int[10]; //第二行有10个元素,并为其分配内存空间
for (int j = 0; j myArray[0].length; j++)
//用1-10之间的随机整数给第一行元素赋值
myArray[0][j] = (int)(Math.random() * 10);
//用100-200之间的随机整数给第二行元素赋值
for (int j=0; j myArray[1].length; j++)
myArray[1][j]=(int)(Math.random() * 100 + 100);
for (int i=0; i myArray.length; i++){ //输出myArray数组各元素的值
for (int j=0; j myArray[i].length; j++){
System.out.print(myArray[i][j]+" ");
}
System.out.println();
}
Arrays.sort(myArray[0]); //对第一行元素排序
Arrays.sort(myArray[1]); //对第二行元素排序
System.out.println("\n排序后的数组元素: ");
for (int i=0; imyArray.length;i++){ //再次输出myArray数组各元素的值
for (int j=0; jmyArray[i].length;j++){
System.out.print(myArray[i][j]+" ");
}
System.out.println();
}
}
}
7 3 9 6 7
103 165 166 148 103 179 128 109 120 156
排序后的数组元素:
3 6 7 7 9
103 103 109 120 128 148 156 165 166 179
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流