java編程題目
思路如下:
隨即4個數字(1~6) 用來模擬4個色子
數字全部存入數組ary,.然後升序排列.
如果滿足兩兩相等,那麼已經排序好的數字,就是ary[0]=ary[1]; ary[2]=ary[3];
然後判斷ary[0]+ary[2]==6 .如果等於6 那麼滿足要求,不等於6 ,那麼繼續下次循環
參考代碼
importjava.util.Arrays;
publicclassRandomDemo{
publicstaticvoidmain(String[]args){
intloop=5;//重復5次試驗
for(intk=0;k<loop;k++){
inttimes=0;//循環的次數
int[]ary;//數組,存儲4個隨機數
while(true){
times++;//次數+1
ary=newint[4];
for(inti=0;i<ary.length;i++){
ary[i]=getNum();//添加隨機數
}
Arrays.sort(ary);//用數組工具類進行排序
//因為有兩兩相等的情況,那麼就是ary[0]=ary[1]ary[2]=ary[3]能減少很多的ifelse判斷
//如果兩兩相等.且兩值和等於6,那麼跳出循環
if(ary[0]==ary[1]&&ary[2]==ary[3]&&ary[0]+ary[2]==6){
break;//跳出
}
}
System.out.println("兩個數字分別是"+ary[0]+"和"+ary[2]+" "+"循環了"+times+"次");
}
}
//該方法用於返回一個[1,6]之間的數字
privatestaticintgetNum(){
return(int)(Math.random()*6)+1;//1~6之間的隨即數
}
}
測試結果
兩個數字分別是1和5 循環了22次
兩個數字分別是1和5 循環了12次
兩個數字分別是3和3 循環了105次
兩個數字分別是1和5 循環了128次
兩個數字分別是2和4 循環了96次
2. Java的編程題目,在線等,急急急
先做兩個比較簡單的先用:
import java.util.Arrays;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Function {
/**
* 設計一個方法,完成字元串的解析。方法定義為:public void myParseString(String inputStr);
* 對於給定的字元串,取得字元串內的各個整數(不考慮小數,),然後將取得的數排序,按從小到大依次列印出來。
* @param args
*/
public static void main(String[] args) {
String s = "aa789bB22cc345dd;5.a";
new Function().myParseString(s);
}
public void myParseString(String inputStr){
String mathregix="\\d+";//數字
Vector vector=new Vector();
Pattern fun=Pattern.compile(mathregix);
Matcher match = fun.matcher(inputStr);
while (match.find()) {
vector.add(match.group(0));
}
Object[] obj=vector.toArray();
int[] result=new int[obj.length];
for (int i = 0; i < obj.length; i++) {
result[i]=Integer.parseInt((String) obj[i]);
}
Arrays.sort(result);
for (int i = 0; i < result.length; i++) {
System.out.println(result[i]);
}
}
}
import java.util.Date;
/**
* 有一個學生類(Student),其有四個私有屬性,分別為:
* 學號no,字元串型;
* 姓名name,字元串型;
* 年齡age,整型;
* 生日birthday,日期型;
* 請:
* 1) 按上面描述設計類;
* 2) 定義對每個屬性進行取值,賦值的方法;
* 3) 要求學號不能為空,則學號的長度不能少於5位字元,否則拋異常;
* 4) 年齡范圍必須在[1,500]之間,否則拋出異常;
*/
public class Student {
private String no;
private String name;
private int age;
private Date birthday;
public int getAge() {
return age;
}
public void setAge(int age) throws Exception {
if(age<1||age<500)throw new Exception("年齡不合法。");
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNo() {
return no;
}
public void setNo(String no) throws Exception {
if(no==null)throw new Exception("學號不能為空!");
if(no.length()<5)throw new Exception("學號不能少於五位!");
this.no = no;
}
}
二、三題
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
/**
* 2. 設計一個GUI界面,要求打界面如下:
*點擊文件->打開,打開文件選擇器,選擇打開給定的java.txt文件,將文件內所有a-z,A-Z之間的字元顯示在界面的文本區域內。
* 3. 設置一個GUI界面,界面如下:
* 點擊文件->保存,打開文件選擇窗體,選擇一個保存路徑,將本界面文本區域內輸入的內容進行過濾,將所有非a-z,A-Z之間的字元保存到C盤下的java.txt文件內。
* Java.txt文件內容如下:
* 我們在2009年第2學期學習Java Programming Design,於2009-6-16考試。
*
*
*
*/
public class FileEditer extends javax.swing.JFrame implements ActionListener {
private JMenuBar menubar;
private JMenu file;
private JMenuItem open;
private JTextArea area;
private JMenuItem save;
private JFileChooser jfc;
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
FileEditer inst = new FileEditer();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
public FileEditer() {
super();
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
area = new JTextArea();
getContentPane().add(area, BorderLayout.CENTER);
area.setText("文本區");
}
{
menubar = new JMenuBar();
setJMenuBar(menubar);
{
file = new JMenu();
menubar.add(file);
file.setText("文件");
file.setPreferredSize(new java.awt.Dimension(66, 21));
{
open = new JMenuItem();
file.add(open);
open.setText("打開");
open.setBorderPainted(false);
open.setActionCommand("open");
open.addActionListener(this);
}
{
save = new JMenuItem();
file.add(save);
save.setActionCommand("save");
save.setText("保存");
save.addActionListener(this);
}
}
}
{
jfc=new JFileChooser();
}
pack();
setSize(400, 300);
} catch (Exception e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e) {
if("open".equals(e.getActionCommand())){
int result=jfc.showOpenDialog(this);
if(result==jfc.APPROVE_OPTION){
File file=jfc.getSelectedFile();
try {
byte[] b=new byte[1024];
FileInputStream fis=new FileInputStream(file);
fis.read(b);
fis.close();
String string=new String(b,"GBK");
string=string.replaceAll("[^a-zA-Z]*", "");
area.setText(string.trim());
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException es) {
// TODO Auto-generated catch block
es.printStackTrace();
}
}
}else if("save".equals(e.getActionCommand())){
int result=jfc.showSaveDialog(this);
if(result!=jfc.APPROVE_OPTION)return;
File file=jfc.getSelectedFile();
try {
if(!file.exists()){
file.createNewFile();
}
String string = area.getText();
string=string.replaceAll("[a-zA-Z]*", "");
byte[] b=string.getBytes();
FileOutputStream fis=new FileOutputStream(file);
fis.write(b);
fis.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException es) {
// TODO Auto-generated catch block
es.printStackTrace();
}
}
}
}
3. JAVA編程題目
public class Student {
private String code;
private String name;
private int height;
private char sex;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String code, String name, int height, char sex) {
super();
this.code = code;
this.name = name;
this.height = height;
this.sex = sex;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public String show(){
return "Student [code=" + code + ", name=" + name + ", height=" + height + ", sex=" + sex + "]";
}
public int show(Student s){
return s.getHeight();
}
public void show(Student s,int sv){
if(s.getHeight()>=sv){
if(s.getSex()=='f'){
System.out.println(s.show());
}
}
}
}
public class Test {
public static void main(String[] args) {
Student student1 = new Student("A001", "zs1", 171, 'm');
Student student2 = new Student("A002", "zs2", 172, 'f');
Student student3 = new Student("A003", "zs3", 173, 'm');
Student student4 = new Student("A004", "zs4", 174, 'f');
Student student5 = new Student("A005", "zs5", 175, 'f');
System.out.println(student1.show());
System.out.println(student2.show());
System.out.println(student3.show());
System.out.println(student4.show());
System.out.println(student5.show());
//平均身高
int sv=(student1.show(student1)+student2.show(student2)+student3.show(student3)+
student4.show(student4)+student5.show(student5))/5;
System.out.println("超過平均年齡的女生詳情:");
//超過平均年齡的女生
student1.show(student1, sv);
student2.show(student2, sv);
student3.show(student3, sv);
student4.show(student4, sv);
student5.show(student5, sv);
}
}
4. java編程題目
這不都說的很清楚了么。。。。。。。。
自己寫吧,也沒啥難度。
是完全不知道這個題目再說什麼么?
packagespring5.source;
importjava.awt.Button;
importjava.awt.FlowLayout;
importjava.awt.TextField;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.KeyEvent;
importjava.awt.event.KeyListener;
importjavax.swing.JFrame;
publicclassDextendsJFrame{
publicstaticvoidmain(String[]args){
Dd=newD();
d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
d.setSize(500,500);
d.setLayout(newFlowLayout());
TextFieldt1=newTextField();
TextFieldt2=newTextField();
Buttonb=newButton("OK");
b.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
Stringv1=t1.getText();
try{
intn=Integer.parseInt(v1);
Doubled=Math.pow(n,2);
t2.setText(String.valueOf(d.intValue()));
}catch(Exceptione2){
e2.printStackTrace();
}
}
});
t1.addKeyListener(newKeyListener(){
@Override
publicvoidkeyTyped(KeyEvente){
}
@Override
publicvoidkeyReleased(KeyEvente){
}
@Override
publicvoidkeyPressed(KeyEvente){
if(e.getKeyChar()==KeyEvent.VK_ENTER){
Stringv1=t1.getText();
try{
intn=Integer.parseInt(v1);
Doubled=Math.pow(n,2);
t2.setText(String.valueOf(d.intValue()));
}catch(Exceptione2){
e2.printStackTrace();
}
}
}
});
// KeyListenerkey_Listener=;
d.add(t1);
d.add(t2);
d.add(b);
d.setVisible(true);
}
}
少了一個d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);關閉窗口的
5. 簡單的java編程題
publicclassBaiZhiDao{
//未做異常處理,價格要輸數字.
publicstaticvoidmain(String[]args){
Scannerscanner=newScanner(System.in);
Ware[]wares=newWare[10];
for(inti=0;i<10;i++){
Wareware=newWare();
System.out.println("請輸入第"+(i+1)+"個產品信息:");
System.out.print("名稱:");
ware.setName(scanner.nextLine());
System.out.print("單價:");
ware.setPrice(Double.valueOf(scanner.nextLine()));
wares[i]=ware;
}
WaremaxWare=getMaxPrice(wares);
System.out.println("單價最高,產品:"+maxWare.getName()+"--"+maxWare.getPrice());
}
staticWaregetMaxPrice(Ware[]wares){
WaremaxWare=wares[0];
for(inti=1;i<wares.length;i++){
if(null!=wares[i]&&wares[i].getPrice()>maxWare.getPrice()){
maxWare=wares[i];
}
}
returnmaxWare;
}
staticclassWare{
Stringname;
doubleprice;
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicdoublegetPrice(){
returnprice;
}
publicvoidsetPrice(doubleprice){
this.price=price;
}
}
}
6. Java程序設計題目
String
upString(String
s);對形參s進行大寫轉換,並返回轉換後的字元串給調用者。
String
lowString(String
s);
對形參s進行小寫轉換,並返回轉換後的字元串給調用者。
2)
main()方法:
public
static
void
main(String
args[])
{
String
s;
BufferedReader
br=new
BufferedReader(InputStreamReader(System.in));
System.out.println(「請輸入要轉換的字條串:」)
s=br.readLine()
if(s!=null)
{
System.out.println(s+」轉換為大寫後為:」+Covert.upString(s));
System.out.println(s+」轉換為小寫後為:」+Covert.lowString(s));
}
else
System.out.println(「輸入錯誤。」);
}
7. JAVA編程題
publicclassTest{
publicstaticvoidmain(String[]args){
int[]arr={12,22};//指定數組
intresutl=getSum(arr);//求十位數為奇數的數值的和
System.out.println(resutl);
}
publicstaticintgetSum(int[]arr){
intresult=0;//結果
for(inti=0;i<arr.length;i++){//注意這里的循環次數是arr的長度,而不是100
intnum=arr[i];//從數組里取出這個數字
if(num>9&&num<100){//題目要求數字是1~100之間的數,且有十位,那麼說明數值范圍是[10,99]
if((num/10)%2!=0){//如果十位是奇數.
result+=num;//那麼累加到結果里
}
}
}
returnresult;//返回結果
}
}
8. Java編程題
第二個:
package test;
import java.util.Arrays;
import java.util.List;
public class TestArray {
public static void main(String[] args) {
Integer arr[] = { 10,7,6,9,8,2};
System.out.println(arrayContains(arr, 1));
System.out.println(arrayContains1(arr, 2));
}
public static boolean arrayContains(Integer[] arr,int targetValue){
List<Integer> list = Arrays.asList(arr);
return list.contains(targetValue);
}
public static int arrayContains1(Integer[] arr,int targetValue){
List<Integer> list = Arrays.asList(arr);
if(!list.contains(targetValue)){
return -1;
}else{
for (int index = 0; index < list.size(); index++) {
Integer val = list.get(index);
if(val==targetValue){
return index;
}
}
}
return -1;
}
}