📄 ex1partc.java
字号:
import java.util.Scanner; //Don't delete this line! Its meaning would be clarified later on in the course.
public class Ex1PartC {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); //Don't delete this line! Its meaning would be clarified later on in the course.
// variable declaration:
int distance, iCocDist, iNumPar,iTmpNumPar, iNumParWin=-1,coconutWeight;
int iTmpWght, iTmpEff, iTmpCon, iTmpBeu;
boolean iTmpType; // Type of bird 0-African, 1-European
double dTmpVelo1,dTmpVelo2,dTmpTime1,dTmpTime2,dTmpTime=0,dBstTime=10000, dTmpFailPoss,dTmpDropProb;
// input reading:
System.out.print("Enter flight distance (between 500m and 1000m): ");
distance = sc.nextInt();
while (distance < 500 | distance > 1000){
System.out.println("Invalid input!");
System.out.print("Enter flight distance (between 500m and 1000m): ");
distance = sc.nextInt();
}
System.out.print("Enter distance between starting point and coconut tree(between 0m and "+
distance +"m): ");
iCocDist = sc.nextInt();
while (iCocDist < 0 | iCocDist > distance){
System.out.println("Invalid input!");
System.out.print ("Enter distance between starting point and coconut tree"+
"(between 0m and "+distance+"m): ");
iCocDist = sc.nextInt();
}
System.out.println();
System.out.print("Enter number of participants (at least 2): ");
iNumPar = sc.nextInt();
while (iNumPar < 2){
System.out.println("Invalid input!");
System.out.print("Enter number of participants (at least 2): ");
iNumPar = sc.nextInt();
}
// generating random value
coconutWeight = (int) (Math.random()*101) + 100;
System.out.println();
System.out.println("Coconut weight: " + coconutWeight + "g.\n");
System.out.print("Contest results:");
// counting and printing properties of each bird and searching for the winner
for (iTmpNumPar=1; iTmpNumPar<=iNumPar; iTmpNumPar=iTmpNumPar+1){
if (Math.random()<0.5) {
iTmpType = true;
System.out.println("\nSwallow No. "+iTmpNumPar+" is an European swallow.");
iTmpWght=(int)(Math.random()*11+15);
}
else {
iTmpType = false;
System.out.println("\nSwallow No. "+iTmpNumPar+" is an African swallow.");
iTmpWght=(int)(Math.random()*11+20);
}
iTmpEff = (int) (Math.random()*101);
iTmpCon = (int) (Math.random()*(101-iTmpEff));
iTmpBeu = 100-iTmpEff-iTmpCon;
// printing swallow's properies:
dTmpDropProb=(100-(double)iTmpCon)/200;
dTmpVelo1=(double)10*(iTmpWght+10)/(double)iTmpWght;
dTmpVelo2=(double)10*(iTmpWght+10)/(double)((double)iTmpWght+(double)coconutWeight/8);
dTmpFailPoss=Math.random();
System.out.print("Weight: "+iTmpWght+", efficiency: "+iTmpEff+", concentration: "+iTmpCon+
", beauty: "+iTmpBeu+".\nUnladen airspeed velocity: "+ dTmpVelo1+
"m/s.\nLaden airspeed velocity: "+dTmpVelo2+"m/s.\nCoconut dropping probability: "+
dTmpDropProb+".\n");
System.out.print("Swallow No. "+iTmpNumPar+" coconut dropping random value: "+dTmpFailPoss+"\n");
if (dTmpFailPoss>dTmpDropProb) {
dTmpTime1=(iCocDist/dTmpVelo1);
dTmpTime2=((double)(distance-iCocDist)/dTmpVelo2);
dTmpTime=dTmpTime1+dTmpTime2+(100-iTmpEff);
System.out.print("The swallow has accomplished the first "+iCocDist+
"meters in "+dTmpTime1+" seconds,\nsearched for a coconut for another "+
(100-iTmpEff)+" seconds,\nand continued its flight for another "+
dTmpTime2+" seconds.\nTotal time: "+dTmpTime+" seconds.\n\n");
}
// check if the bird is the winner
if (dTmpTime<=dBstTime){
dBstTime=dTmpTime;
iNumParWin=iTmpNumPar;
}
else {
System.out.print("Coconut was dropped!\n");
}
}
if (iNumParWin==-1){
System.out.print("\nAll coconuts were dropped. There is no winner!");
}
else{
System.out.print("\nThe winner is swallow No."+iNumParWin+"!");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -