关于java基础if while 的编程
发布网友
发布时间:2023-07-09 01:12
我来回答
共4个回答
热心网友
时间:2024-11-30 13:17
package ;
public class investment {
public int negativeP=12;
public int zeroP=34;
public int positiveP=54;
public int[] pool=new int[100];
public double payment = 0.2;
public double current = 10000;
public double[] log =new double[10];
public int[] getProbability(){
int i =0;
for(;i<this.negativeP;i++){
this.pool[i] = -1;}
for(;i<this.zeroP+this.negativeP;i++){
this.pool[i] = 0;
}
for(;i<this.zeroP+this.negativeP+this.positiveP;i++){
this.pool[i] = 1;
}
return this.pool;
}
public int getRandomResult(){
int[] list = this.getProbability();
int rand = (int) (Math.random()*100);
return list[rand];
}
public void getResult(){
for(int i=0;i<10;i++){
int rand = this.getRandomResult();
double retu = rand*this.payment*this.current;
log[i] = this.current+retu;
this.current = this.current+rand*this.payment*this.current;
System.out.println(i+"-----"+retu+"-----"+this.current);
}
}
}
你再修改一下, 我这里可以打出效果来
追问请问log是什么意思在这里,还有就是如果要是要求排版是用神马指令 谢谢
热心网友
时间:2024-11-30 13:17
代码:
package feb_12;
import java.util.Scanner;
public class InvestmentSimulator {
private static float baseInvesment;
private static int maxPeridos;
private static float negativeReturn;
private static float zeroReturn;
private static float positiveReturn;
private static Scanner sc;
static {
sc = new Scanner(System.in);
baseInvesment = valueInput("What is initial value of the investment ? ");
maxPeridos = (int) valueInput("What is the maximum # of periods ? ");
init();
msg("\n\n%-3s\t%-11s\t%-11s\n", "P#", "Return", "Current");
msg("%-3s\t%-11s\t%-11s\n", "---", "-------", "-------");
outPut(0, 0f, baseInvesment);
}
private static void init() {
negativeReturn = valueInput("What is the probability of negative returns (in %) ? ");
zeroReturn = valueInput("What is the probability of zero returns (in %) ?");
positiveReturn = valueInput("What is the probability of positive returns (in %) ? ");
if (negativeReturn + zeroReturn + positiveReturn != 100) {
msg("illegal value! retype...\n");
init();
}
}
private static void outPut(int peridos, float returnInv, float invesment) {
if (peridos == 0)
msg("%03d\t%15s%12.2f\n", peridos, "", invesment);
else
msg("%03d\t[% 9.2f]%12.2f\n", peridos, returnInv, invesment);
if (peridos < maxPeridos) {
if (invesment > baseInvesment * 2)
msg("Congrats! You've doubled your investment.");
else {
returnInv = invesment * returnValue(0.2f);
invesment += returnInv;
outPut(peridos + 1, returnInv, invesment);
}
} else
msg("The investment is end !");
}
private static float returnValue(float baseReturn) {
return baseReturn * random();
}
private static int random() {
int r = (int) (Math.random() * 100);
if (r < negativeReturn)
return -1;
else if (r >= 100 - positiveReturn)
return 1;
else
return 0;
}
private static void msg(String message) {
System.out.print(message);
}
private static void msg(String format, Object... arg) {
System.out.format(format, arg);
}
private static float valueInput(String message) {
msg(message);
String str = sc.nextLine();
if (str.matches(" *\\d+\\.?\\d* *")) {
return Float.parseFloat(str);
} else
return valueInput("Error input! retype:");
}
public static void main(String[] args) {
}
}
测试:
What is initial value of the investment ? 10000
What is the maximum # of periods ? 10
What is the probability of negative returns (in %) ? 12
What is the probability of zero returns (in %) ?34
What is the probability of positive returns (in %) ? 54
P# Return Current
---------- -------
000 10000.00
001[ 2000.00] 12000.00
002[ 2400.00] 14400.00
003[ -2880.00] 11520.00
004[ 2304.00] 13824.00
005[ 2764.80] 16588.80
006[ 3317.76] 19906.56
007[ 3981.31] 23887.87
Congrats! You've doubled your investment.
输出格式自己微调下~
热心网友
时间:2024-11-30 13:18
看一下。。。不是很明白。。。。。。。
热心网友
时间:2024-11-30 13:18
看一下,不是很明白你的目的。