本文目錄一覽:
用JAVA編寫一個程序,要求如下:
實現代碼如下:
Student類:
public class Student {
private String name;
private String sex;
private int age;
private double chinese;
private double math;
private double english;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getChinese() {
return chinese;
}
public void setChinese(double chinese) {
this.chinese = chinese;
}
public double getMath() {
return math;
}
public void setMath(double math) {
this.math = math;
}
public double getEnglish() {
return english;
}
public void setEnglish(double english) {
this.english = english;
}
}
—————————————————————–
StudentTest類:(測試類)
import java.util.Scanner;
public class StudentTest {
public static void main(String[] args) {
Student student = new Student();
Scanner sc = new Scanner(System.in);
System.out.println(“請輸入姓名:”);
student.setName(sc.next());
System.out.println(“請輸入性別:”);
student.setSex(sc.next());
System.out.println(“請輸入年齡:”);
student.setAge(sc.nextInt());
System.out.println(“請輸入語文成績、數學成績、英語成績:”);
student.setChinese(sc.nextDouble());
student.setMath(sc.nextDouble());
student.setEnglish(sc.nextDouble());
Double count = student.getChinese()+ student.getMath()+student.getEnglish();
System.out.println(“姓名:”+student.getName()+” 性別:”+student.getSex()+” 年齡:”+student.getAge());
System.out.println(“總分:”+count+” 平均分:”+count/3);
}
}
運行結果為:
用Java編寫一個程序,要求如下:
1.內部使用 C 的 longjmp 機制讓出一個協程。 因此,如果一個 C 函數 foo 調用了一個 API 函數, 而這個 API 函數讓出了(直接或間接調用了讓出函數)。 由於 longjmp 會移除 C 棧的棧幀, Lua 就無法返回到 foo 里了。
2.為了迴避這類問題, 碰到 API 調用中調用讓出時,除了那些拋出錯誤的 API 外,還提供了三個函數: lua_yieldk, lua_callk,和 lua_pcallk 。 它們在讓出發生時,可以從傳入的 延續函數 (名為 k 的參數)繼續運行。
3.我們需要預設一些術語來解釋延續點。 對於從 Lua 中調用的 C 函數,我們稱之為 原函數。 從這個原函數中調用的上面所述的三個 C API 函數我們稱之為 被調函數。 被調函數可以使當前線程讓出。 (讓出發生在被調函數是 lua_yieldk, 或傳入 lua_callk 或 lua_pcallk 的函數調用了讓出時。)
4.假設正在運行的線程在執行被調函數時讓出。 當再次延續這條線程,它希望繼續被調函數的運行。 然而,被調函數不可能返回到原函數中。 這是因為之前的讓出操作破壞了 C 棧的棧幀。 作為替代品,Lua 調用那個作為被調函數參數給出的 延續函數 。 正如其名,延續函數將延續原函數的任務。
5.注意這裡那個額外的顯式的對延續函數的調用:Lua 僅在需要時,這可能是由錯誤導致的也可能是發生了讓出而需要繼續運行,才會調用延續函數。 如果沒有發生過任何讓出,調用的函數正常返回, 那麼 lua_pcallk (以及 lua_callk)也會正常返回。 (當然,這個例子中你也可以不在之後調用延續函數, 而是在原函數的調用後直接寫上需要做的工作。)
6.Lua 會把延續函數看作原函數。 延續函數將接收到和原函數相同的 Lua 棧,其接收到的 lua 狀態也和 被調函數若返回後應該有的狀態一致。 (例如, lua_callk 調用之後, 棧中之前壓入的函數和調用參數都被調用產生的返回值所替代。) 這時也有相同的上值。 等到它返回的時候,Lua 會將其看待成原函數的返回去操作。
7.我們需要預設一些術語來解釋延續點。 對於從 Lua 中調用的 C 函數,我們稱之為 原函數。 從這個原函數中調用的上面所述的三個 C API 函數我們稱之為 被調函數。 被調函數可以使當前線程讓出。 (讓出發生在被調函數是 lua_yieldk, 或傳入 lua_callk 或 lua_pcallk 的函數調用了讓出時。)
8.假設正在運行的線程在執行被調函數時讓出。 當再次延續這條線程,它希望繼續被調函數的運行。 然而,被調函數不可能返回到原函數中。 這是因為之前的讓出操作破壞了 C 棧的棧幀。 作為替代品,Lua 調用那個作為被調函數參數給出的 延續函數 。 正如其名,延續函數將延續原函數的任務。
希望能幫到你,謝謝!
根據以下任務要求,編寫Java應用程序?
按照題目要求編寫的Java程序如下
注意 請使用你的真實姓名和班級替換Test類中
創建Student對象stu時用的”張三”和”20計算機應用01班”
import java.util.Scanner;
class Student{
private String name,classname;
private int starnum,scorenum;
private int[] scores;
public void setStarNum(int n){
this.starnum=n;
}
public Student(String name,String classname,int scorenum){
this.name=name;
this.classname=classname;
this.scorenum=scorenum;
}
public String getName(){
return this.name;
}
public void printStar(){
for(int i=0;istarnum;i++){
for(int j=0;j2*i+1;j++){
System.out.print(“*”);
}
System.out.println();
}
}
public void setScore(){
Scanner sc=new Scanner(System.in);
scores=new int[scorenum];
System.out.print(“請輸入各科成績:”);
for(int i=0;iscorenum;i++){
scores[i]=sc.nextInt();
}
}
public void showInfo(){
System.out.print(name+”同學,你所在的班級是”+classname+”,你各科考試成績分別為:”);
for(int i=0;iscorenum;i++){
if(i==scorenum-1)
System.out.print(scores[i]);
else
System.out.print(scores[i]+”,”);
}
System.out.println();
}
public float getAvg(){
float sum=0;
for(int i=0;iscorenum;i++){
sum=sum+scores[i];
}
return sum/scorenum;
}
}
public class Test{
public static void main(String[] args){
Student stu=new Student(“張三”,”20計算機應用01班”,5);
stu.setStarNum(4);
stu.printStar();
stu.setScore();
stu.showInfo();
if(stu.getAvg()60){
System.out.println(stu.getName()+”是不合格學生”);
}else{
System.out.println(stu.getName()+”是個合格學生”);
}
}
}
用JAVA語言編寫一個程序,要求如下:
import java.util.Random;
import java.util.Scanner;
public class T {
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
int difficulty;//難度
int mode;//運算類型
int answer;//答案
int amount;//挑戰題目數量
int score = 0;//得分
System.out.println(“請輸入難度(1:一位數、2:兩位數、3:三位數):”);
difficulty = in.nextInt();
System.out.println(“請輸入運算類型(1:加、2:減、3:乘、4:除):”);
mode = in.nextInt();
System.out.println(“請輸入想要挑戰的題目數量:”);
amount = in.nextInt();
Random random = new Random();
for (int i = 0; i amount; i++) {
if (difficulty == 1) {
if (mode == 1) {
int x = random.nextInt(10);
int y = random.nextInt(10);
System.out.println(“第” + i + “題:”);
System.out.print(x + ” + ” + y + ” = “);
answer = in.nextInt();
if (answer == (x + y)) {
System.out.println(“答對了\n”);
score++;
} else {
System.out.println(“答錯了,答案是:” + (x + y) + “\n”);
}
} else if (mode == 2) {
int x = random.nextInt(10);
int y = random.nextInt(10);
System.out.println(“第” + i + “題:”);
System.out.print(x + ” – ” + y + ” = “);
answer = in.nextInt();
if (answer == (x – y)) {
System.out.println(“答對了\n”);
score++;
} else {
System.out.println(“答錯了,答案是:” + (x – y) + “\n”);
}
} else if (mode == 3) {//乘法
} else if (mode == 4) {//除法 考慮小數的問題
} else {
throw new Exception(“運算類型輸入值不合法”);
}
} else if (difficulty == 2) {
if (mode == 1) {
int x = random.nextInt(100);
int y = random.nextInt(100);
System.out.println(“第” + i + “題:”);
System.out.print(x + ” + ” + y + ” = “);
answer = in.nextInt();
if (answer == (x + y)) {
System.out.println(“答對了\n”);
score++;
} else {
System.out.println(“答錯了,答案是:” + (x + y) + “\n”);
}
} else if (mode == 2) {
} else if (mode == 3) {//乘法
} else if (mode == 4) {//除法 考慮小數的問題
} else {
throw new Exception(“運算類型輸入值不合法”);
}
} else if (difficulty == 3) {
if (mode == 1) {
int x = random.nextInt(1000);
int y = random.nextInt(1000);
System.out.println(“第” + i + “題:”);
System.out.print(x + ” + ” + y + ” = “);
answer = in.nextInt();
if (answer == (x + y)) {
System.out.println(“答對了\n”);
score++;
} else {
System.out.println(“答錯了,答案是:” + (x + y) + “\n”);
}
} else if (mode == 2) {
} else if (mode == 3) {//乘法
} else if (mode == 4) {//除法 考慮小數的問題
} else {
throw new Exception(“運算類型輸入值不合法”);
}
} else {
throw new Exception(“難度輸入值不合法”);
}
}
System.out.println(“挑戰結束,您的分數為:” + score);
}
}
我就只舉了加法的例子,其他運算的寫法都是類似的,你照葫蘆畫瓢即可
運行結果:
如何編寫java程序
新建java第一種方法:
1。首先右擊鼠標,新建一個文本文檔。
2。更改文本文檔的後綴名為
.java
。注意文件名必須和類名相同。
如果你的電腦沒有顯示後綴名的話需要對電腦進行一定的設置。
3。打開計算機——組織——文件夾選項——查看——將隱藏文件夾選項取消勾選即可。
建議將這個選項取消勾選,有很多的惡意程序會侵入我們的電腦,取消勾選可以很好的查看病毒等文件的類型。
4。右擊建好的文檔,選擇編輯即可進行java的編寫。
在編寫的時候我們有一定的格式,java語言的格式是為了我們日後更改程序方便而規定的,我們一定要遵循這種格式編寫。
第二種方法:
1直接新建一個記事本文檔,然後選擇保存。
注意保存時額位置,不要等到保存完卻找不到文件到哪了。
2在選擇保存的窗口中輸入後綴“.java”,然後保存即可。注意文件名必須和類名相同。
3同樣右擊文件,選擇編輯即可在文件中進行編輯了。
前兩個大括號之間回車,第二對大括號有一個間隔,直接按TAb鍵即可。
測試運行java程序:
打開運行命令,也可以直接在開菜單中輸入CMD調用命令窗口。
你的文件保存在哪裡就將你的文件目錄輸入到命令提示符窗口中,例如我的是D盤根目錄,所以我輸入“d:”。
編譯java文件,生成class文件:
在命令提示符中輸入“javac
文件名.java”,我的文件名為A,所以輸入javac
A.java然後回車,等待編譯。
運行文件:
繼續輸入“java
文件名”然後回車就可以運行我們寫好的文件了。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/236269.html