1. 程式人生 > >JAVA作業

JAVA作業

繼續 function sco 不清楚 wan 使用 數據 orm 什麽

(一)學習總結

1.在java中通過Scanner類完成控制臺的輸入,查閱JDK幫助文檔,Scanner類實現基本數據輸入的方法是什麽?

從鍵盤輸入需要用Scanner類,需要導入頭文件 import.java.util.Scanner; nextInt():讀取整形數據 nextDouble():讀取雙精度數據

next{"\\d{4}-\\d{2}-d{2}$"} 在Scanner類中沒有專門的日期格式輸入操作,需要自己編寫驗證,手工轉換。

next():讀取輸入的下一個單詞 nextLine():讀取輸入的下一行內容 構造 Scanner 對象,並與 System.in 關聯

import java.util.Scanner; public class ScannerDemo01{ public static void main(String[] args){ Scanner sca = new Scanner(System.in); System.out.println("請輸入數據:"); String str =sca.next(); System.out.println("輸入的數據為:"+str); } } 運行結果為:

2.Random類和Math類的random()方法都能產生隨機數,這兩種方式有什麽區別,各有什麽特點呢?查閱JDK幫助文檔,並舉例加以說明。

Random類的隨機方法是設定隨機種子的,如:new Random().nextInt(10),這裏的10就是隨機種子,根據隨機數的原理,相同隨機種子在相同的隨機次數中產生的隨機數是一樣的。 Math類的radom方法返回的是0~1之間的小數,如果想得到1~100之間的數則需要擴大100倍,但是仍然是小數,可以加(int)強制轉換。

import java.util.Random; public class RandomDemo{ public static void main(String args[]){ Random r = new Random(); int x=r.nextInt(100); \\產生0~100的隨機整數 System.out.println(); } } import java.util.Random; public class Randoma{ public static void main(String args[]){ double a = Math.random()*100; \\產生1~100的隨機小數 int b = (int)(Math.random()*100); \\產生1~100的隨機整數 int c = 10+(int)(Math.random()*100); \\產生一個10到10+100之間的整數 System.out.println(a); System.out.println(b); System.out.println(c); } } 3.運行下列程序,結果是什麽?查閱資料,分析為什麽。

public class Test { public static void main(String args[]) { double a = 0.1; double b = 0.1; double c = 0.1; if((a + b + c) == 0.3){ System.out.println("等於0.3"); }else { System.out.println("不等於0.3"); } } } 運行結果:

若:

public class Test { public static void main(String args[]) { double a = 0.1; double b = 0.1; double c = 0.1; if((a + b + c) == 0.3){ System.out.println("等於0.3"); }else { System.out.println("不等於0.3"); } System.out.println(a+b+c); } } 則運行結果為:

產生誤差,所以不相等。

4.本次學習要點中其他需要總結的內容

Math類是數學操作類,提供的一系列數學操作方法,包括求絕對值,三角函數等,在Math類中,提供的一切方法都是靜態方法,所以直接由類名稱調用,Math類中也有radom方法,只是不大寫,返回的是01之間的隨機小數,不同於Radom方法。

(二)實驗總結

1.看商品猜價格

程序設計思路: 1.首先要先產生一個隨機數,作為產品的價格。 2.進入循環,從鍵盤輸入猜的價格,如果猜對了,跳出循環,如果猜錯了,提示猜大了還是猜小了,如果猜的次數超過十次,則結束。

問題1:運行進入死循環 原因:沒有跳出循環語句 解決方案:加上break

程序:

import java.util.Scanner; import java.util.Random; public class Guesstheprice {

public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan = new Scanner(System.in); int lage =0; int cishu = 0; int suiji = new Random().nextInt(100); for(int i=0;i<10;i++){ int price=scan.nextInt(); if(price<suiji){ System.out.println("您猜小了"); cishu++; } if(price>suiji){ System.out.println("您猜大了"); cishu++; } if(price==suiji){ System.out.println("恭喜您,猜對了"); cishu++; lage++; System.out.println("商品的價格為"+suiji); System.out.println("您一共猜了"+cishu+"次"); break; } } if(lage==0){ System.out.println("您已經猜了十次,商品的價格為"+suiji); } }

} 擴展: 程序設計思路: 1.首先要先產生一個隨機數,作為產品的價格。 2.進入循環,輸入猜的價格,第一次才對分數為100,兩次才對分數為80,以此類推。若五次都沒有猜中或者五次之內猜中,則給出價格和分數。 3.一次遊戲循環完畢後,則選擇是否繼續進行下一輪遊戲。 問題1:一直卡在 選擇是否繼續進行下一輪遊戲 不會比較字符串

原因:基礎知識還不夠牢固 解決方案:查閱資料,問同學,解決了這個問題。 程序:

import java.util.Scanner; import java.util.Random; public class Caijiage { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan = new Scanner(System.in); Scanner str2 = new Scanner(System.in); String str = new String("yes"); String str1 = new String("yes"); int lage =0; int grade = 0; int suiji = new Random().nextInt(100); while (str.equals(str1)){ for(int i=1;i<=5;i++) { int price=scan.nextInt(); if(price<suiji){ System.out.println("您猜小了"); System.out.println("您還有"+(5-i)+"次猜數機會"+"\n"); grade = (5-(i-1))*20; } if(price>suiji){ System.out.println("您猜大了"); System.out.println("您還有"+(5-i)+"次猜數機會"+"\n"); grade = (5-(i-1))*20; } if(price==suiji){ System.out.println("恭喜您,猜對了"); lage++; grade = (5-(i-1))*20; System.out.println("商品的價格為"+suiji); System.out.println("您一共猜了"+i+"次"); System.out.println("您的得分為:"+grade); break; } if(i==5){ System.out.println("您已經猜了五次,商品的價格為"+suiji); System.out.println("您的得分為:0"); } } System.out.println("是否繼續遊戲?是請輸入yes否則輸入no"); str1 = str2.next(); } } } 2.萬年歷

程序設計思路:先判斷是閏年還是平年,再計算一共有多少天,判斷每個月有多少天,計算出從1900一月一日到今天有多少天,再判斷這個月是從周幾開始的。 問題1:每個方法在哪使用搞不清楚,還有一個月從周幾開始的有點蒙圈。 原因:思路不清晰

代碼:

import java.util.Scanner;

public class Wannianli { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("請輸入從1900年到現在的一個年份"); Scanner scan = new Scanner(System.in); int year = scan.nextInt(); System.out.println("請輸入1-12月的一個月份"); int month = scan.nextInt(); System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六"); printCalender(year, month); }

public static boolean isLeap(int year) { // 判斷閏年 if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { return true; } else { return false; } }

public static int days(int year, int month) { // 判斷某年某月有多少天 int day2 = 0; if (month == 2) { if (isLeap(year)) { day2 = 29; } else { day2 = 28; } } else { switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: day2 = 31; break; case 4: case 6: case 9: case 11: day2 = 30;

} } return day2; }

public static int totalDays(int year, int month) { // 計算某年某月之前距離1900年1月1日的總天數 int m, all = 0, day2; for (int i = 1900; i < year; i++) { if (isLeap(year)) { all += 366; } else { all += 356; } } for (m = 1; m < month; m++) { day2 = days(year, month); all = all + day2; } return all; }

public static void printCalender(int year, int month) { // 輸出某年某月日歷的方法 int j = 0; int s = totalDays(year, month); int n = (s % 7) + 1; for (int i = 1; i <= n; i++) { System.out.println("\t"); j++; } for (int k = 1; k <= days(year, month); k++) { System.out.println(k + "\t"); j++; if (j % 7 == 0) { System.out.println("\n"); } } } (三)代碼托管

package ww;
import java.util.Scanner;
public class w {

private static int grades[];

public static void main(String[] args) {
grades = new int [5];
for(int x=0;x<5;x++) {
double[] scores = new double[10];
Scanner input = new Scanner(System.in);//掃描器用於讀取控制臺輸入
for (int i = 0; i < scores.length; i++) {//輸入分數
System.out.print("請輸入第" + (i + 1) + "位評委的分數,滿分十10分");
String temp = input.nextLine().trim();
scores[i] = Double.parseDouble(temp);
}
getScore(scores);//對分數進行處理的函數
}
}

private static void getScore(double[] scores) {
//對分數進行排序
double temp;
for (int i = 0; i < scores.length - 1; i++) {
for (int j = i + 1; j < scores.length; j++) {
if (scores[i] > scores[j]) {
temp = scores[i];
scores[i] = scores[j];
scores[j] = temp;
}
}
}
//去掉一個高分,一個低分後的平均分
double sum =0;
for (int i = 1; i < scores.length-1; i++) {
sum+=scores[i];
}
double pjf = sum/(scores.length-2);
System.out.println("去掉一個最高分:"+scores[scores.length-1]);
System.out.println("去掉一個最低分:"+scores[0]);
System.out.println("最後的平均分是:"+String.format("%.2f",pjf));//平均分保留2位小數
}
}

JAVA作業