gpt4 book ai didi

java - 将每次运行后的输出更改为所有运行后的一个输出

转载 作者:行者123 更新时间:2023-12-01 11:48:42 25 4
gpt4 key购买 nike

除了输出之外,我的程序几乎全部完成,我希望它在一个输出中显示所有内容,而不是每次运行后一次,它应该运行 500 次,但在解决这个问题之前只运行了 10 次。

package assignment5;
import javax.swing.JOptionPane;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
import java.text.*;
public class assignment5
{
public static void main(String[] args)
{
lottery pic=new lottery();for(int i = 0; i <10; i++)
{
pic.Get_player_numbers();
pic.Get_jackpot_number();
pic.Check_winner ();
pic.Write_data();

}
}
}
class lottery
{
int[] picks= new int[5];
int[] cpick=new int[5];
int i;
int j,c;
int match=0;
double matchcount0=0;
double matchcount1=0;
double matchcount2=0;
double matchcount3=0;
double matchcount4=0;
double matchcount5=0;
int jackpot = 25000000;
int payout;

void Get_player_numbers ()
{
int temp,dupflag=0;
for(i=0;i<=4;++i)
{
//YOU DO NOT NEED THE CNUMBERFLAG
//IF YOU GENERATED THE NUMBERS CORRECLTY, THE COMPUTER WILL NOT GENERATE ONE ABOVE 99 OR LESS THAN 1
dupflag=0;
while(dupflag==0)
{
temp = (int)Math.round(Math.random()*99)+1;
dupflag=1;
for(c=0;c<=i;++c)
{
if(temp==picks[c])
{
dupflag=0;
}
}//inner for loop
if(dupflag==1)
picks[i]=temp;
}
}
}

//void jackpot()
void Get_jackpot_number()
{
int ctemp,cdupflag=0;
for(j=0;j<=4;++j)
{
//YOU DO NOT NEED THE CNUMBERFLAG
//IF YOU GENERATED THE NUMBERS CORRECLTY, THE COMPUTER WILL NOT GENERATE ONE ABOVE 99 OR LESS THAN 1
cdupflag=0;
while(cdupflag==0)
{
ctemp = (int)Math.round(Math.random()*99)+1;
cdupflag=1;
for(c=0;c<=j;++c)
{
if(ctemp==cpick[c])
{
cdupflag=0;
}
}//inner for loop
if(cdupflag==1)
cpick[j]=ctemp;
}
}
String Jackpot="Computer Lottery numbers are: "+"\n";
//String computer = "";
for(j=0;j<=4;++j)
{
if(j==4)
Jackpot=Jackpot+cpick[j];
else
Jackpot=Jackpot+cpick[j]+"-";
}
}

void Check_winner ()
{
match=0;

for(int i=0;i<=4;++i)
{
for(int j=0;j<=4;++j)
{
if(picks[i]==cpick[j])
{
match=match+1;
}
}
}
}
void Write_data ()
{

if(match==0)
{
matchcount0=matchcount0+1;

payout=0;
jackpot=jackpot+25000;
}
else if(match==1)
{
matchcount1=matchcount1+1;

payout=100;
jackpot=jackpot+100000;
}
else if(match==2)
{
matchcount2=matchcount2+1;

jackpot=jackpot+250000;
payout=1000;
}
else if(match==3)
{
matchcount3=matchcount3+1;

jackpot=jackpot+500000;
payout=10000;
}
else if(match==4)
{
matchcount4=matchcount4+1;

jackpot=jackpot+1000000;
payout=100000;
}
else if(match==5)
{
matchcount5=matchcount5+1;

payout=jackpot;
jackpot=jackpot-jackpot+2500000;
}

System.out.println("Current Jackpot Player# Winner# #Matched Payout\n"+jackpot+" "+picks[0]+" "+picks[1]+" "+picks[2]+" "+picks[3]+" "+picks[4]+" "+cpick[0]+" "+cpick[1]+" "+cpick[2]+" "+cpick[3]+" "+cpick[4]+" "+match+" "+payout+"\nThe percent of plays where 0 numbers matched = "+matchcount0/i*100+"%\nThe percent of plays where 1 numbers matched = "+matchcount1/10+"%\nThe percent of plays where 2 numbers matched = "+matchcount2/10+"%\nThe percent of plays where 3 numbers matched = "+matchcount3/10+"%\nThe percent of plays where 4 numbers matched = "+matchcount4/10+"%\nThe percent of plays where 5 numbers matched = "+matchcount5/10+"%\n");
}
}

最佳答案

使用 StringBuilder 类。在每次运行中,您只需在 StringBuilder 实例的第二行附加新行即可。

StringBuilder builder = new StringBuilder();
for (int i = 0; i < 10; ++i) {
builder.append(System.getProperty("line.separator") + "Current Jackpot player...");
}

一切完成后就可以使用了

builder.toString();

获取输出。

关于java - 将每次运行后的输出更改为所有运行后的一个输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28949496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com