你好,很簡短的代碼
成都創(chuàng)新互聯(lián)公司專注于上思企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站設(shè)計(jì),電子商務(wù)商城網(wǎng)站建設(shè)。上思網(wǎng)站建設(shè)公司,為上思等地區(qū)提供建站服務(wù)。全流程按需定制設(shè)計(jì),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)
package?zhengze;
import?java.util.Scanner;
public?class?Test?{
public?static?void?main(String[]?args)?{
Scanner?in?=?new?Scanner(System.in);
String?str?=?in.next();
if?(str.equals("a")??str.equals("b"))?{
}?else?if?(str.equals("a")?||?str.equals("b"))?{
}?else?{
}
}
}
希望可以幫助到你
直接調(diào)用jdk里面的方法Arrays.sort(args)方法。。。。
而且這個(gè)方法重載實(shí)現(xiàn)了多個(gè)參數(shù),排序下標(biāo)[N~M]位數(shù)字~倒敘,升序等等~
import java.awt.Button;
import java.util.Set;
// 每一個(gè)小方塊類
public class Diamond extends Button {
private Diamond[] diamonds;
// 該小方塊周圍的八個(gè)方向上的小方塊
private Diamond east;
private Diamond north;
private Diamond northEast;
private Diamond northWest;
private Diamond south;
private Diamond southEast;
private Diamond southWest;
private Diamond west;
private boolean isBomb;// 是否是雷
private boolean isChange;// 又沒有被翻過
private int no;// 產(chǎn)生的方塊的編號(hào)
// 持有所有小方塊的引用,方便進(jìn)行操作
public Diamond(Diamond[] diamonds) {
this.diamonds = diamonds;
}
// 按鍵時(shí)方塊發(fā)生改變
public boolean change() {
this.isChange = true;// 說明已經(jīng)翻過了
if(isBomb) {// 觸雷
//this.setBackground(Color.red);
return true;
} else {// 不是雷,就顯示周圍雷的數(shù)目
//this.setLabel(this.getNearBombNo() + "");
this.setLabel(this.getNearBombNo() + "");
//if(this.getNearBombNo() == 0) {
// this.moveon();
//}
return false;
}
}
// 獲得該小方塊周圍雷的數(shù)量
public int getNearBombNo() {
int no = 0;
if(this.northWest != null this.northWest.isBomb) no++;
if(this.north != null this.north.isBomb) no++;
if(this.northEast != null this.northEast.isBomb) no++;
if(this.east != null this.east.isBomb) no++;
if(this.southEast != null this.southEast.isBomb) no++;
if(this.south != null this.south.isBomb) no++;
if(this.southWest != null this.southWest.isBomb) no++;
if(this.west != null this.west.isBomb) no++;
return no;
}
// 獲得該小方塊周圍的小方塊
public Diamond getNearDimaond(int i) {
int index = -1;
switch (i) {
case 1:// 1表示西北,2,表示北,以此類推
index = no - 10;
if(index 1 || no == 19 || no == 28 || no == 37 || no == 46 || no == 55 || no == 64 || no == 73) {
return null;
} else {
return diamonds[index];
}
case 2:
index = no - 9;
if(index 1) {
return null;
} else {
return diamonds[index];
}
case 3:
index = no - 8;
if(index 1 || no == 9 || no == 18 || no == 27 || no == 36 || no == 45 || no == 54 || no == 63 || no == 72) {
return null;
} else {
return diamonds[index];
}
case 4:
index = no + 1;
if(no == 9 || no == 18 || no == 27 || no == 36 || no == 45 || no == 54 || no == 63 || no == 72 || no == 81) {
return null;
} else {
return diamonds[index];
}
case 5:
index = no + 10;
if(index = 81 ||no == 9 || no == 18 || no == 27 || no == 36 || no == 45 || no == 54 || no == 63 || no == 72 || no == 81) {
return null;
} else {
return diamonds[index];
}
case 6:
index = no + 9;
if(index 81) {
return null;
} else {
return diamonds[index];
}
case 7:
index = no + 8;
if(index = 81 || no==1 || no == 10 || no == 19 || no == 28 || no == 37 || no == 46 || no == 55 || no == 64 || no == 73) {
return null;
} else {
return diamonds[index];
}
case 8:
index = no - 1;
if(no==1 || no==10 || no == 19 || no == 28 || no == 37 || no == 46 || no == 55 || no == 64 || no == 73) {
return null;
} else {
return diamonds[index];
}
}
return null;
}
// 遞歸,set是用來裝已經(jīng)翻過的小方塊的,不然會(huì)死循環(huán),為什么用set,因?yàn)閟et是不重復(fù)的
public void moveon(SetDiamond set) {
set.add(this);// 先把自己加上
if(this.getNorthWest() != null this.getNorthWest().isBomb == false) {
this.getNorthWest().change();
if(this.getNorthWest().getNearBombNo() == 0) {
if(set.contains(this.getNorthWest()) == false)
this.getNorthWest().moveon(set);
}
set.add(this.getNorthWest());
}
if(this.getNorth() != null this.getNorth().isBomb == false) {
this.getNorth().change();
if(this.getNorth().getNearBombNo() == 0) {
if(set.contains(this.getNorth()) == false)
this.getNorth().moveon(set);
}
set.add(this.getNorth());
}
if(this.getNorthEast() != null this.getNorthEast().isBomb == false) {
this.getNorthEast().change();
if(this.getNorthEast().getNearBombNo() == 0) {
if(set.contains(this.getNorthEast()) == false)
this.getNorthEast().moveon(set);
}
set.add(this.getNorthEast());
}
if(this.getEast() != null this.getEast().isBomb == false) {
this.getEast().change();
if(this.getEast().getNearBombNo() == 0) {
if(set.contains(this.getEast()) == false)
this.getEast().moveon(set);
}
set.add(this.getEast());
}
if(this.getSouthEast() != null this.getSouthEast().isBomb == false) {
this.getSouthEast().change();
if(this.getSouthEast().getNearBombNo() == 0) {
if(set.contains(this.getSouthEast()) == false)
this.getSouthEast().moveon(set);
}
set.add(this.getSouthEast());
}
if(this.getSouth() != null this.getSouth().isBomb == false) {
this.getSouth().change();
if(this.getSouth().getNearBombNo() == 0) {
if(set.contains(this.getSouth()) == false)
this.getSouth().moveon(set);
}
set.add(this.getSouth());
}
if(this.getSouthWest() != null this.getSouthWest().isBomb == false) {
this.getSouthWest().change();
if(this.getSouthWest().getNearBombNo() == 0) {
if(set.contains(this.getSouthWest()) == false)
this.getSouthWest().moveon(set);
}
set.add(this.getSouthWest());
}
if(this.getWest() != null this.getWest().isBomb == false) {
this.getWest().change();
if(this.getWest().getNearBombNo() == 0) {
if(set.contains(this.getWest()) == false)
this.getWest().moveon(set);
}
set.add(this.getWest());
}
}
/*public Diamond[] getDiamonds() {
return diamonds;
}*/
public Diamond getEast() {
return east;
}
public int getNo() {
return no;
}
public Diamond getNorth() {
return north;
}
public Diamond getNorthEast() {
return northEast;
}
public Diamond getNorthWest() {
return northWest;
}
public Diamond getSouth() {
return south;
}
public Diamond getSouthEast() {
return southEast;
}
public Diamond getSouthWest() {
return southWest;
}
public Diamond getWest() {
return west;
}
public boolean isBomb() {
return isBomb;
}
public boolean isChange() {
return isChange;
}
public void setBomb(boolean isBomb) {
this.isBomb = isBomb;
}
public void setChange(boolean isChange) {
this.isChange = isChange;
}
public void setDiamonds(Diamond[] diamonds) {
this.diamonds = diamonds;
}
public void setEast(Diamond east) {
this.east = east;
}
public void setNo(int no) {
this.no = no;
}
public void setNorth(Diamond north) {
this.north = north;
}
public void setNorthEast(Diamond northEast) {
this.northEast = northEast;
}
public void setNorthWest(Diamond northWest) {
this.northWest = northWest;
}
public void setSouth(Diamond south) {
this.south = south;
}
public void setSouthEast(Diamond southEast) {
this.southEast = southEast;
}
public void setSouthWest(Diamond southWest) {
this.southWest = southWest;
}
public void setWest(Diamond west) {
this.west = west;
}
}
public?static?void?main(String[]?args)?{
//控制臺(tái)輸入
Scanner?scan?=?new?Scanner(System.in);
//三次機(jī)會(huì)
for?(int?i?=?2;?i?=?0;?i--)?{
//提示用戶輸入
System.out.println("請(qǐng)輸入用戶名");
//用戶名
String?userName?=?scan.next();
//提示用戶輸入
System.out.println("請(qǐng)輸入密碼");
//密碼
String?password?=?scan.next();
//如果輸入正確,??break,結(jié)束循環(huán)
if("jim".equals(userName)??"123456".equals(password))?{
System.out.println("歡迎登錄MyShopping系統(tǒng)");
break;
}
else?{
if(i??0)?{
System.out.println("密碼錯(cuò)誤,你還有"+i+"次機(jī)會(huì)!");
continue;
}
if(i?==?0)?{
System.out.println("密碼錯(cuò)誤,三次機(jī)會(huì)已用完!");
}
}
}
}
寫了一個(gè)代碼,代碼如下,可以進(jìn)行參考
public?class?sum?{
public?static?void?main(String[]?args)?{
//創(chuàng)建一個(gè)Scanner的對(duì)象input
Scanner?input?=?new?Scanner(System.in);?
//提示用戶輸入數(shù)據(jù)?
System.out.print("請(qǐng)輸入一個(gè)整數(shù)");
//將輸入的值賦給n
int?n?=?input.nextInt();
//定義變量接收計(jì)算后的和
int?sum?=?0;
//利用循環(huán)進(jìn)行求和
for?(int?i?=?0;?i?=?n;?i++)?{
sum+=i;
}
//輸出最后的和
System.out.println("從0一直到"+n+"的所有整數(shù)的和是:"+sum);
}
}
本文標(biāo)題:新手java編程短代碼 java入門
網(wǎng)站路徑:http://sd-ha.com/article40/doocheo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、網(wǎng)站排名、品牌網(wǎng)站制作、網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容