骰子的输出是随机的,为此我创建了一个函数。 我想记录一次使骰子大于骰子2两次所花费的“尝试次数”。因此,当连续两次放大时,我希望将其打印出来(“花了+尝试+“使d1连续两次大于d2”)。到目前为止,我有这个但被卡住了:
public static int Dice() {
return (int)(Math.random() * 6.0) + 1;
}
public static void main(String[] args) {
int d1 = Dice();
int d2 = Dice();
int trials = 0;
for (int i = 0; i < 100000; i++) {
if (t1 > t2) {
trials++;
if (t1 > t2) {
trials++
}
}
}
System.out.println(trials);
}
}
答案 0 :(得分:0)
执行以下操作(将以下内容作为伪代码处理):
public static int[] Dice() {
int[] diceThrownTwoTimes = new int[2];
diceThrownTwoTimes[0] = (int)(Math.random() * 6.0) + 1;
diceThrownTwoTimes[1] = (int)(Math.random() * 6.0) + 1;
return diceThrownTwoTimes;
}
public static void main(String[] args) {
int trials = 0;
for (int i = 0; i < 100000; i++) {
int[] d1 = Dice();
int[] d2 = Dice();
if (d1[0] > d2[0] && d1[1] > d2[1]) {
trials++;
}
}
System.out.println(trials);
}
编辑:
要获得两次连续两次大于dice1值的尝试,可以执行以下操作:
public static int[] Dice() {
int[] diceThrownTwoTimes = new int[2];
diceThrownTwoTimes[0] = (int)(Math.random() * 6.0) + 1;
diceThrownTwoTimes[1] = (int)(Math.random() * 6.0) + 1;
return diceThrownTwoTimes;
}
public static void main(String[] args) {
int trials = 0;
for (int i = 0; i < 100000; i++) {
int[] d1 = Dice();
int[] d2 = Dice();
if (d1[0] > d2[0] && d1[1] > d2[1]) {
break;
}else{
trials++;
}
}
System.out.println(trials);
}
答案 1 :(得分:0)
我愿意
public static int Dice() {
return (int)(Math.random() * 6.0) + 1;
}
public static void main(String[] args) {
int trials = 0;
int count = 0;
while (count < 2) {
int d1 = Dice();
int d2 = Dice();
if (d1 > d2) {
++count;
} else {
count = 0;
}
++trials;
}
println("It took " + trials + " trials for d1 to be larger than d2 " + count + " times in a row.")
}
答案 2 :(得分:0)
欢迎您!这应该工作。请参阅注释,以了解您做错了一些事情以及一些解释。
public static int Dice() {
return (int)(Math.random() * 6.0) + 1;
}
public static void main(String[] args) {
// initialize variables to be updated in the loop
int trials = 0;
Boolean lastTimeD1WasGreater = false;
for (int i = 0; i < 100000; i++) {
// always increment the counter!
trials++;
// you had the wrong variable names here.
// also they must be INSIDE the loop or else they would always come out the same
int d1 = Dice();
int d2 = Dice();
if (d1 > d2) {
if (lastTimeD1WasGreater) {
// if it was greater both last time and this time then we're done!
System.out.println("it took " + trials + " trials.");
break;
} else {
// otherwise set this variable so we can be on the lookout next time.
lastTimeD1WasGreater = true;
}
} else {
// d1 was not greater, so we'll start looking for a pair in the next loop.
lastTimeD1WasGreater = false;
}
}
}
答案 3 :(得分:0)
除非需要使用外部函数返回随机数,否则您可以在main函数中轻松完成此操作。
add_action( 'woocommerce_after_shop_loop_item', 'show_sale_percentage', 25 );
function show_sale_percentage() {
global $product;
if ( $product->is_on_sale() ) {
if ( ! $product->is_type( 'variable' ) ) {
$max_percentage = ( ( $product->get_regular_price() - $product->get_sale_price() ) / $product->get_regular_price() ) * 100;
} else {
$max_percentage = 0;
foreach ( $product->get_children() as $child_id ) {
$variation = wc_get_product( $child_id );
$price = $variation->get_regular_price();
$sale = $variation->get_sale_price();
if ( $price != 0 && ! empty( $sale ) ) $percentage = ( $price - $sale ) / $price * 100;
if ( $percentage > $max_percentage ) {
$max_percentage = $percentage;
}
}
}
echo "<div class='saved-sale'>-" . round($max_percentage) . "%</div>";
}
}
d1连续两次大于d2进行了7次试验
样本输出
答案 4 :(得分:0)
这是我想出的
import java.util.Random;
公共班级主要{
public static int Dice() {
return (int) (Math.random() * 6.0) + 1;
}
public static void main(String[] args) {
Random rand = new Random();
int trials = 0;
int twoTimes = 0;
do {
int d1 = rand.nextInt(25) + 1;
int d2 = rand.nextInt(25) + 1;
trials++;
if (d1 > d2) {
twoTimes++;
System.out.printf("%s time greater\nd1 was " + d1 + "\nd2 was " + d2 + "\n-------\n", twoTimes);
}
}
while (twoTimes != 2);
System.out.printf("\nIt took %s times", trials);
}
}