📄 ex1partb.java
字号:
import java.util.Scanner; //Don't delete this line! Its meaning would be clarified later on in the course.
public class Ex1PartB {
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, coconutWeight, iCocDist;
int iAfrWght, iAfrEff, iAfrCon, iAfrBeu;
int iEurWght, iEurEff, iEurCon, iEurBeu;
boolean bAfrFailed=false,bEurFailed=false;
double dTmpVelo1,dTmpVelo2,dTmpTime1,dTmpTime2, dTmpFailPoss,dAfrTime=0,dEurTime=0,dTmpDropProb;
// input reading and checking:
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 African swallow weight (between 20g and 30g): ");
iAfrWght = sc.nextInt();
while (iAfrWght < 20 | iAfrWght > 30){
System.out.println("Invalid input!");
System.out.print("Enter African swallow weight (between 20g and 30g): ");
iAfrWght = sc.nextInt();
}
System.out.print("Enter African swallow efficiency, concentration, and beauty.\n"+
"Each argument must be between 0 and 100, and the sum of all arguments"+
" must be equal to 100.\n");
System.out.print("Efficiency: ");
iAfrEff = sc.nextInt();
System.out.print("Concentration: ");
iAfrCon = sc.nextInt();
System.out.print("Beauty: ");
iAfrBeu = sc.nextInt();
while ((iAfrBeu+iAfrCon+iAfrEff!=100) | (iAfrEff < 0 | iAfrEff > 100) |
(iAfrCon < 0 | iAfrCon > 100) | (iAfrBeu < 0 | iAfrBeu > 100)){
System.out.print("Invalid input!\nEnter African swallow efficiency, concentration,"+
" and beauty.\nEach argument must be between 0 and 100, and the sum of all"+
"arguments must be equal to 100.\n");
System.out.print("Efficiency: ");
iAfrEff = sc.nextInt();
System.out.print("Concentration: ");
iAfrCon = sc.nextInt();
System.out.print("Beauty: ");
iAfrBeu = sc.nextInt();
}
// generating random values:
coconutWeight = (int) (Math.random()*101) + 100;
System.out.println();
System.out.println("Coconut weight: " + coconutWeight + "g.");
iEurWght = (int) (Math.random()*11) + 15;
iEurEff = (int) (Math.random()*101);
iEurCon = (int) (Math.random()*(101-iEurEff));
iEurBeu = 100-iEurEff-iEurCon;
// printing of swallows properies:
// African one:
dTmpDropProb=(100-(double)iAfrCon)/200;
dTmpVelo1=(double)10*(iAfrWght+10)/(double)iAfrWght;
dTmpVelo2=(double)10*(iAfrWght+10)/(double)((double)iAfrWght+(double)coconutWeight/8);
System.out.print("\nContest results:\nAfrican swallow:\nWeight: "+
iAfrWght+", efficiency: "+iAfrEff+", concentration: "+iAfrCon+
", beauty: "+iAfrBeu+".\nUnladen airspeed velocity: "+ dTmpVelo1+
"m/s\nLaden airspeed velocity: "+dTmpVelo2+"m/s\nCoconut dropping probability: "+
dTmpDropProb+".\n");
// generating random value again (the value of the swallow's fail possibility):
dTmpFailPoss=Math.random();
System.out.print("African swallow coconut dropping random value: "+dTmpFailPoss+"\n");
// checking if swallow dropped the cocount or not:
if (dTmpFailPoss>dTmpDropProb) {
dTmpTime1=(iCocDist/dTmpVelo1);
dTmpTime2=((double)(distance-iCocDist)/dTmpVelo2);
dAfrTime=dTmpTime1+dTmpTime2+(100-iAfrEff);
System.out.print("The swallow has accomplished the first "+iCocDist+
"meters in "+dTmpTime1+" seconds,\nsearched for a coconut for another "+
(100-iAfrEff)+" seconds,\nand continued its flight for another "+
dTmpTime2+" seconds.\nTotal time: "+dAfrTime+" seconds.\n");
}
else {
System.out.print("Coconut was dropped!\n");
bAfrFailed=true;
}
// European one:
dTmpDropProb=(100-(double)iEurCon)/200;
dTmpVelo1=(double)10*(iEurWght+10)/(double)iEurWght;
dTmpVelo2=(double)10*(iEurWght+10)/(double)((double)iEurWght+(double)coconutWeight/8);
System.out.print("\nEuropean swallow:\nWeight: "+
iEurWght+", efficiency: "+iEurEff+", concentration: "+iEurCon+
", beauty: "+iEurBeu+".\nUnladen airspeed velocity: "+ dTmpVelo1+
"m/s\nLaden airspeed velocity: "+dTmpVelo2+"m/s\nCoconut dropping probability: "+
dTmpDropProb+".\n");
// generating random value again (the value of the swallow's fail possibility)
dTmpFailPoss=Math.random();
System.out.print("European swallow coconut dropping random value: "+dTmpFailPoss+"\n");
// checking if swallow dropped the cocount or not
if (dTmpFailPoss>dTmpDropProb) {
dTmpTime1=(iCocDist/dTmpVelo1);
dTmpTime2=((double)(distance-iCocDist)/dTmpVelo2);
dEurTime=dTmpTime1+dTmpTime2+(100-iEurEff);
System.out.print("The swallow has accomplished the first "+iCocDist+
"meters in "+dTmpTime1+" seconds,\nsearched for a coconut for another "+
(100-iEurEff)+" seconds,\nand continued its flight for another "+
dTmpTime2+" seconds.\nTotal time: "+dEurTime+" seconds.\n\n"); }
else {
System.out.print("Coconut was dropped!\n\n");
bEurFailed=true;
}
// Declaiming the winner
if (bAfrFailed==false & (dAfrTime>dEurTime)) {
System.out.print("The African swallow wins!");
}
else if (bEurFailed==false & (dAfrTime<dEurTime)){
System.out.print("The European swallow wins!");
}
else if ((bAfrFailed==true & bEurFailed==true ) | (dAfrTime==dEurTime)){
if (iAfrBeu>iEurBeu) {
System.out.print("The African swallow wins!");
}
else if (iAfrBeu<iEurBeu) {
System.out.print("The European swallow wins!");
}
else {
System.out.print("The African swallow wins!");
}
}
else {
System.out.print("Impossible");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -