閑來無事嘗試着寫下博客,很菜!
題目來源:尚硅谷30天Java培訓
1.我家的狗5歲了, 5歲的狗相當於人類多大呢?其實,狗的前兩年每一年相當於人類的10.5歲,之后每增加一年就增加四歲。那么5歲的狗相當於人類多少年齡呢?(應該是: 10.5 + 10.5 + 4 + 4 + 4 = 33歲。)
編寫一個程序,獲取用戶輸入的狗的年齡,通過程序顯示其相當於人類的年齡。如果用戶輸入負數,請顯示一個提示信息。
源碼:
import java.util.Scanner;
public class IfTest5 {
public static void main(String[] args) {
Scanner dogAge= new Scanner(System.in);
int age=dogAge.nextInt();
if(age>0 && age<=2){
System.out.println("相當於人的年齡:" + age * 10.5);
}else if(age>2){
System.out.println("相當於人的年齡:" + (2* 10.5+(age-2)*4));
}else{
System.out.println("狗狗還沒出生");
}
}
}
2.假設你想開發一個玩彩票的游戲,程序隨機地產生一個兩位數的彩票,提示用戶輸入一個兩位數,然后按照下面的規則判定用戶是否能贏。
1)如果用戶輸入的數匹配彩票的實際順序,獎金10 000美元。
2)如果用戶輸入的所有數字匹配彩票的所有數字,但順序不一致,獎金 3 000美元。
3)如果用戶輸入的一個數字僅滿足順序情況下匹配彩票的一個數字,獎金1 000美元。
4)如果用戶輸入的一個數字僅滿足非順序情況下匹配彩票的一個數字,獎金500美元。
5)如果用戶輸入的數字沒有匹配任何一個數字,則彩票作廢。
知識點:隨機數的產生(int)(Math.random()*90+10)產生隨機數.
Math.random() : [0,1) * 90 --->> [0,90) + 10 --->> [10,100) --->> [10,99]
公式:[a,b] : (int)(Math.random()*(b-a+1)+a) 此時就可得到區間[a,b]內的任何一個值
源碼:
import java.util.Scanner;
public class TestCaiPiao {
public static void main(String[] args) {
System.out.println(Math.random()); //產生[0,1)
int num = (int)(Math.random()*90+10);//得到[10,99)
System.out.println(num);
int numShiWei = num/10;
int numGeWei = num%10;
//用戶輸入一個兩位數
Scanner input = new Scanner(System.in);
System.out.println("輸入一個兩位數:");
int guess = input.nextInt();
int guessShiWei = guess/10;
int guessGeWei = guess%10;
if(num == guess){
System.out.println("獎金10000美元");
}else if(numShiWei == guessGeWei && numGeWei == guessShiWei){
System.out.println("獎金3000");
}else if(numShiWei == guessShiWei || numGeWei == guessGeWei){
System.out.println("獎金1000");
}else if(numShiWei == guessGeWei || numGeWei == guessShiWei){
System.out.println("獎金500");
}else{
System.out.println("沒中獎");
}
System.out.println("中獎號碼是:" + num);
}
}
3.大家都知道,男大當婚,女大當嫁。那么女方家長要嫁女兒,當然要提出一定的條件:高: 180cm以上;富:財富1千萬以上;帥:是。
如果這三個條件同時滿足,則:“我一定要嫁給他!!!”
如果三個條件有為真的情況,則:“嫁吧,比上不足,比下有余。”
如果三個條件都不滿足,則:“不嫁!”
注:自己寫的時候不知道咋想的把身高定義成String類型?后面發現了接着做了下去順便了解下字符轉整型的方法:Integer.parseInt()
源碼:
import java.util.Scanner;
public class TestWedding {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("請輸入身高(CM):");
String high= input.nextLine(); //一開始寫成了String high = Integer.parseInt(input.nextLine()) 當然報錯啦,哈哈哈!
System.out.println("請輸入Money:");
double money = input.nextDouble();
System.out.println("帥么(true/false)");
boolean beautiful = input.nextBoolean();
if(Integer.parseInt(high)>=180&&money>=10000000&&beautiful){
System.out.println("我一定要嫁給你");
}else if(Integer.parseInt(high)>=180||money>=10000000||beautiful){
System.out.println("嫁吧!湊合過還能咋地");
}else{
System.out.println("臭屌絲啥都沒有,還想接我的盤?");
}
}
}