簡單小程式
總執行上一個程式:public static void main(String [] args) 沒寫對;
售票程式
package hello1;
import java.util.Scanner;
public class java01 {
public static void main(String[] args) {
//初始化
Scanner in =new Scanner(System.in);
//請投幣
int balance=0;
while(true){
System.out.println("請投幣:");
int amount = in.nextInt();
balance=balance+amount;
if(balance >= 10){
//列印車票
System.out.println("*****************");
System.out.println("*java城際鐵路專線*");
System.out.println("*無指定座位票*");
System.out.println(" 票價 10 元*");
System.out.println("*###########*");
//找零
System.out.println("找零:"+(balance-10));
balance = 0;
}
}
}
}
猜字遊戲 計算機隨機讀出一個數0-100 猜出它是多少,並給出所猜次數
package hello1;
import java.util.Scanner;
public class guessgame {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int number = (int)(Math.random()*100+1);//計算機隨機給出一個0到1之間的數,轉化為1到100
int a;
int count=0;
do {
a= in.nextInt();//a等於輸入的數
count++;
if(a <number){
System.out.print("偏小");
}
else if(a >number) {
System.out.println("偏大");
}
}while(a!=number);
System.out.println("恭喜你猜對了,你猜中了"+count);
}
}