怎麼用java寫程式
- 綜合知識
- 關注:2.93W次
控制檯小遊戲 class YZDEL{ public static void main(String[] args) { java.util.Random Shiji = new java.util.Random(); java.util.Scanner Shuru = new java.util.Scanner(System.in); java.util.Scanner Nandu = new java.util.Scanner(System.in); int YingxHP = 20; int YingxMP = 0; int MogHP = 20; int MogMP = 0; int MogDo = 0; int fangyu = 0; System.out.println("┏━━━━╮┏━━┣┣┓ ╮ ┓ ┏━┳━┳┓ ┓━┓ "); System.out.println("┏┣━━━┓┏━━┣┣┓ ┃ ┏ ┃ ┃┓ ┃ ╯ "); System.out.println("┣┣━━━┫╭━━┻╯ ╮ ┃ ╰┓┃ ┃╯┏━┣━━┓"); System.out.println("┣┣━━━┫┏━━━━┓╭━━┣━┛┗━┻━┻┛ ┃┃ "); System.out.println("┏┣━━━┓┣━━━━┫ ┃ ┃┃┗╮╮┃ ┃┣━╯"); System.out.println(" ┛ ┗╯┗━━━━╯ ┗╯ ╰╰━━┛┛┗━╯╰━┛"); System.out.println(); System.out.println(" 作者:天狐空幻"); System.out.println(); System.out.println("說明:勇者的操作方式為以下所示:"); System.out.println(" 使用攻擊需消耗1MP 傷害1HP"); System.out.println(" 使用蓄力可增加1MP 傷害0HP"); System.out.println(" 使用躲閃需消耗0MP 傷害0HP 躲避攻擊"); System.out.println(" 使用重擊需消耗3MP 傷害2HP 防禦無效"); System.out.println(" 惡龍攻擊力高, 注意防禦是取勝的關鍵"); System.out.println(" 惡龍MP達到4時可能會放出火焰無法躲避"); System.out.println(" 準備說明完畢,那麼讓我們來挑戰惡龍吧!"); System.out.println("=================================="); //難度選擇 System.out.println("請選擇難度"); System.out.println("1.娛樂 2.挑戰 3.噩夢"); int ND = Nandu.nextInt(); System.out.println("=================================="); while(true) { //HP,MP的顯示 System.out.print("勇者: "); System.out.print(" HP "); for(int x=YingxHP;x>0;x--) System.out.print("*"); System.out.print(" "+YingxHP); System.out.println(); System.out.print(" "); System.out.print(" MP "); for(int x=YingxMP;x>0;x--) System.out.print("*"); System.out.print(" "+YingxMP); System.out.println(); System.out.print("惡龍: "); System.out.print(" HP "); for(int y=MogHP;y>0;y--) System.out.print("*"); System.out.print(" "+MogHP); System.out.println(); System.out.print(" "); System.out.print(" MP "); for(int y=MogMP;y>0;y--) System.out.print("*"); System.out.print(" "+MogMP); System.out.println(); System.out.println("=================================="); //勝利判定 if(YingxHP<1) {System.out.println();System.out.println("勇者HP為0! 不..不可能..我怎麼會..勇者倒下了。
再接再厲吧!~");System.out.println();break;} if(MogHP<1) {System.out.println();System.out.println("惡龍HP為0! 惡龍絕望的哀鳴中倒了下去。勇者勝利了。
恭喜你挑戰成功!!");System.out.println();break;} //角色輸入判定 System.out.println("你要做什麼:"); System.out.println("1.攻擊 2.蓄力"); System.out.println("3.躲閃 4.重擊"); System.out.println("____________________"); int Do = Shuru.nextInt(); //敵人輸入判定 //娛樂難度 if(ND==1){ for(int i=1;i<3;i++) MogDo = Shiji.nextInt(4);} //挑戰難度 if(ND==2){ if(YingxMP==0&&MogMP==0) {MogDo = 0;} else if(MogMP==0){for(int i=1;i<3;i++) MogDo = Shiji.nextInt(2);} else if(YingxMP>2&&MogMP>2) MogDo = 2; else if(MogMP<4) {for(int i=1;i<4;i++) MogDo = Shiji.nextInt(3);} else MogDo = 3;} //噩夢難度 if(ND==3){ if(Do==1) MogDo=1; if(Do==2&&MogMP>0) MogDo=2; if(Do==2&&MogMP==0) MogDo=0; if(Do==3&&MogMP<4) MogDo=0; if(Do==3&&MogMP>=4) MogDo=3;}//變態判定。
//戰鬥分析 //防禦 if(Do==3) {fangyu=1;System.out.println("你靈巧的躲避攻擊!");} if(MogDo==1) {fangyu=1;System.out.println("惡龍進行防禦!");} //角色判定 if(Do==1&&YingxMP==0) {System.out.println("MP不足!");} if(Do==1&&YingxMP>0) { if(fangyu==0) {MogHP=MogHP-1;YingxMP=YingxMP-1;System.out.println("你發動攻擊!");} if(fangyu==1) {YingxMP=YingxMP-1;System.out.println("你的攻擊被格擋!");}} if(Do==2) {YingxMP=YingxMP+1;System.out.println("你進行蓄力!");} if(Do==4&&YingxMP<3) {System.out.println("MP不足!");} if(Do==4&&YingxMP>2) {MogHP=MogHP-2;YingxMP=YingxMP-3;System.out.println("你發動重擊!");} if(Do> 4) System.out.println("你不知所措。"); //敵人判定 if(MogDo==2&&MogMP==0) {System.out.println("惡龍在發呆!");} if(MogDo==2&&MogMP>0) { if(fangyu==0) {YingxHP=YingxHP-2;MogMP=MogMP-1;System.out.println("惡龍發動攻擊!");} if(fangyu==1) {MogMP=MogMP-1;System.out.println("惡龍的攻擊被躲開了!");}} if(MogDo==0) {MogMP=MogMP+1;System.out.println(。
2. 怎麼用JAVA編寫程式很簡單,用個這個冒泡法
public class shuzu {
public static void main(String args[])
{
int a,b,c,d;
int size=10;
int sj[]={2,32,0,23,76,356,1,35,299,79};
for(a=0;a<10;a++)
{
System.out.println(sj[a]);
}
for(b=1;b<size;b++)
{
for(c=size-1;c>=b;c--)
{
if(sj[c-1]>sj[c])
d=sj[c-1];
sj[c-1]=sj[c];
sj[c]=d;
}
for(b=1;b<size;b++)
{
System.out.println(sj[b]);
}
}
}
3. 怎麼用JAVA語言寫一個程式System.out.println("請輸入一個0-100的分數:");
Scanner scan=new Scanner(System.in);
int a=scan.nextInt();
if(a>=0&&a<60){
System.out.println("不及格");
}
else if(a>=60&&a<70){
System.out.println("及格");
}
else if(a>=70&&a<80){
System.out.println("中等");
}
else if(a>=80&&a<90){
System.out.println("良好");
}
else if(a>=90&&a<=100){
System.out.println("優秀");
}
else {
System.out.println("輸入錯誤");
}
4. 如何用java編寫一個程式,要求隨便輸入某一年,求某一月的某一天是import java.util.Calendar;import java.util.Scanner;public class Test { public static void main(String[] args){ Scanner scan = new Scanner(System.in); System.out.println("請輸入年份: "); int year = scan.nextInt(); System.out.println("請輸入月份: "); int month = scan.nextInt(); System.out.println("請輸入日期: "); int day = scan.nextInt(); Calendar time =Calendar.getInstance(); //下面程式碼設定開始日期 //注:設定月份時,比如設定是8月,設定時候要減1,如下: time.set(year, month-1, day); //下面就是獲取是這年的第幾天 System.out.println(time.get(Calendar.DAY_OF_YEAR)); }}。
5. 怎樣編寫一個最簡單的java程式下下來,是不是exe檔案 ,還是一個資料夾呢,
你得配置一下你的系統變數
在我的電腦右擊屬性,開啟高階,在裡面有一個變數 ,開啟,在上面新增一個 名為PATH 值為,你安裝的那個目錄,下的bin 如: C:javabin 這樣就可以用了
開啟記事本,在裡面輸入:
public class HellowWord{
public static void main(String[]args){
System.out.println("HellowWord!");
}
儲存為 HellowWord.java檔案 檔名一定要和類名一樣
進入Dos 中,並進入到這個檔案 的目錄
在裡面輸入: javac HellowWord.java
沒有問題的情況下
再次輸入: java HellowWord
就OK了
}
6. 怎麼用JAVA編寫出這樣的程式編了一個小程式,根據使用者輸入的層數輸出類似格式的東西
輸入3,會出現
1
121
12321
import java.io.*;
public class Test {
public static void main(String[] args) {
while(true){
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("請輸入需要輸出的層數:");
int i = Integer.parseInt(br.readLine());
for(int j = 1; j <= i ;j++ ){
int k = 1;
for(; k <= j; k++){
System.out.print(k);
}
for( k = k - 2; k >= 1; k-- ){
System.out.print( k );
}
System.out.println();
}
break;
}catch(NumberFormatException nfe){
System.err.println("您輸入的不是有效的數字!");
}catch(IOException ioe){
System.err.println("輸入出錯,請重新輸入!");
}
}
}
}
7. 用Java程式設計出以下程式,該怎麼編寫呢1:編寫程式,將磅轉換為千克(1磅=0.454千克);
float changeToPound(float pound)
{
return (float)0.454*pound;
}
2:編寫程式,假設三角形的三條邊放在a,b,c裡,判斷並輸出三邊是否有效。(三邊有效是指這三條邊可以構成一個三角形)
void isTriangle(float a,float b,float c)
{
//大於0
if(a<=0||b<=0||c<=0)
{
System.out.println("輸入的邊無法構成三角形");
return;
}
//兩邊之和大於第三邊
if(a+b<=c||a+c<=b||b+c<=a)
{
System.out.println("輸入的邊無法構成三角形");
return;
}
//兩邊之差小於第三邊
if(Math.abs(a-b)>=c||Math.abs(a-c)>=b||Math.abs(b-c)>=a)
{
System.out.println("輸入的邊無法構成三角形");
return;
}
System.out.println("輸入的邊可以構成三角形!");
}
程式程式碼是自己寫出來的,這些程式都是非常基本的,建議樓主先自己寫,這個作參考。
8. 怎麼用Java編寫下面這個程式abstract class Shape{
abstract void draw();
}
class Circle extends Shape{
void draw(){
System.out.println("This is a circle");
}
}
class Rectangle extends Shape{
void draw(){
System.out.println("This is a rectangle");
}
}
public class ShapePrinter{
public static void main(String[] args){
Shape[] shapes=null;
shapes=new Shape[3];
shapes[0]=new Circle();
shapes[1]=new Rectangle();
shapes[2]=new Circle();
for(int i=0;i<shapes.length;i++){
shapes[i].draw();
}
}
}
- 文章版權屬於文章作者所有,轉載請註明 https://shqsg.com/zonghezhishi/oweppk.html